Pygame (Python Game Development) Tutorial - 42 - Pause and Game Over Non-Clearing

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Just to start I love this content, helped me a lot, like you said in the beginning if we press the opposite key of the direction the game ends, i made a simple solution, i know its nothing much but i hope i helps someone xD


for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and direction != "right":
direction = "left"
lead_x_change = -block_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT and direction != "left":
direction = "right"
lead_x_change = block_size
lead_y_change = 0

elif event.key == pygame.K_UP and direction != "down":
direction = "up"
lead_y_change = -block_size
lead_x_change = 0
elif event.key == pygame.K_DOWN and direction != "up":
direction = "down"
lead_y_change = block_size
lead_x_change = 0
elif event.key == pygame.K_p:
pause()

tomasmadurago
Автор

Can I report a little bug? the apple can appear under the snake :)

UmbertoDOvidio
Автор

Any idea why there is lag if you press the arrow keys(up/down/left/right) while your in pause, and then press c to unpause?

samlouiscohen
Автор

I AM proud of my snake game, u r gud guy u!

kristian
Автор

I have constructed a code without gameLoop() function (got an error and decided to keep coding without it). Also fixed left-right self-eating weakness. This game is a tribute to Kanako-sama 🐍:

import pygame
import time
import random

pygame.init()

white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 164, 0)
blue = (0, 0, 120)

display_width = 800
display_height = 600

block_size = 20
fps = 10

icon =

gameDisplay = pygame.display.set_mode((display_width, display_height))



clock = pygame.time.Clock()

image =
font_small = pygame.font.SysFont("comicsanms", 25)
font_med = pygame.font.SysFont("comicsanms", 50)
font_large = pygame.font.SysFont("comicsanms", 70)
apple_img =

direction = "right"
appleT = 30

gk = {"gameExit": True, "gameOver": False, "intro": True, "pause": False}
lead_x = display_width / 2
lead_y = display_height / 2
lead_x_change = 10
lead_y_change = 0

snake_list = []
snake_length = 1

def text_objects(text, color, size):
if size == "small":
text_surface = font_med.render(text, True, color)
elif size == "medium":
text_surface = font_med.render(text, True, color)
if size == "large":
text_surface = font_large.render(text, True, color)
return text_surface, text_surface.get_rect()

def screen_message(msg, color, y_displace=0, size="small"):
text_surface, text_rect = text_objects(msg, color, size)
text_rect.center = (display_width / 2), (display_height / 2) + y_displace
gameDisplay.blit(text_surface, text_rect)

def rand_apple():
rand_apple_x = round(random.randint(0, display_width - appleT))
rand_apple_y = round(random.randint(0, display_height - appleT))

return rand_apple_x, rand_apple_y

rand_apple_x, rand_apple_y = rand_apple()

def score_bar(score):
text = font_small.render("Score: " + str(score), True, black)
gameDisplay.blit(text, [0, 0])

def snake(block_size, snake_list):
if direction == "right":
snake_head = pygame.transform.rotate(image, 270)
gameDisplay.blit(snake_head, (snake_list[-1][0], snake_list[-1][1]))
elif direction == "left":
snake_head = pygame.transform.rotate(image, 90)
gameDisplay.blit(snake_head, (snake_list[-1][0], snake_list[-1][1]))
elif direction == "up":
snake_head = pygame.transform.rotate(image, 0)
gameDisplay.blit(snake_head, (snake_list[-1][0], snake_list[-1][1]))
elif direction == "down":
snake_head = pygame.transform.rotate(image, 180)
gameDisplay.blit(snake_head, (snake_list[-1][0], snake_list[-1][1]))
for xy in snake_list[:-1]:
pygame.draw.rect(gameDisplay, green, [xy[0], xy[1], block_size, block_size])

while gk['intro']:
gameDisplay.fill(white)
screen_message("Snake followers are unstoppable!", green, -100, size="large")
screen_message("Gather faith points for Kanako-sama", black, -30)
screen_message("More faith = more Kanako blessing", black, 10)
screen_message("Don't eat yourself, or Kanako-sama curse you", black, 50)
screen_message("Play C to play, ESC to pause, or O to quit", black, 180)
pygame.display.update()
clock.tick(15)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
gk['gameExit'] = False
gk['intro'] = False
if event.key == pygame.K_o:
pygame.quit()
quit()

while not gk['gameExit']:

while gk['pause']:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
gk['pause'] = False
time.sleep(0.1)
pygame.display.update()
elif event.key == pygame.K_o:
gk['pause'] = False
gk['gameExit'] = True
pygame.quit()
quit()
clock.tick(5)

while gk['gameOver']:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gk['gameExit'] = True
gk['gameOver'] = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_o:
gk['gameExit'] = True
gk['gameOver'] = False
if event.key == pygame.K_c:
gk['gameOver'] = False
gk['gameExit'] = False
lead_x = display_width / 2
lead_y = display_height / 2
direction = "right"
lead_x_change = 10
lead_y_change = 0
rand_apple_x, rand_apple_y = rand_apple()
snake_list.clear()
snake_length = 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
gk['gameExit'] = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and not (direction == "right" and snake_length > 2):
direction = "left"
lead_x_change = -block_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT and not (direction == "left" and snake_length > 2):
direction = "right"
lead_x_change = block_size
lead_y_change = 0
elif event.key == pygame.K_UP and not (direction == "down" and snake_length > 2):
direction = "up"
lead_y_change = -block_size
lead_x_change = 0
elif event.key == pygame.K_DOWN and not (direction == "up" and snake_length > 2):
direction = "down"
lead_y_change = block_size
lead_x_change = 0
elif event.key == pygame.K_ESCAPE:
gk['pause'] = True
if lead_x >= display_width or lead_x <= 0 or lead_y <= 0 or lead_y >= display_height:
gk['gameOver'] = True

lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(white)
snake_head = [lead_x, lead_y]

gameDisplay.blit(apple_img, (rand_apple_x, rand_apple_y))
if len(snake_list) > snake_length:
del snake_list[0]
for eachSegment in snake_list[:-1]:
if eachSegment == snake_head:
gk['gameOver'] = True
snake(block_size, snake_list)
score_bar(snake_length - 1)
pygame.display.update()

if gk['gameOver']:
screen_message("Game over", red, -50, size="large")
screen_message("Press C to play again or O to quit", black, 50, size="small")
pygame.display.update()
if gk['pause']:
screen_message("Paused", black, -100, size="large")
screen_message("Press C to continue, O to quit", black, 25)
pygame.display.update()

if rand_apple_x < lead_x < rand_apple_x + appleT or rand_apple_x < lead_x + block_size < rand_apple_x + appleT:
if rand_apple_y < lead_y < rand_apple_y + appleT or rand_apple_y < lead_y + block_size < rand_apple_y + appleT:
snake_length += 1
rand_apple_x, rand_apple_y = rand_apple()
print(rand_apple())

clock.tick(fps)

blandon
Автор

When paused, my screen doesnt show snake, apple and score ?

prashantsihag
Автор

I love your tutorials, but.... :)

I do not know to which pygame tutorial I should refer to so I just comment it here. I followed all tutorials and also created a snake game till tut41 so far. However there is one problem which I do not know how to solve.

An example of a problem: In the original snake game, the snake cannot directly move to the left while moving to the right direction. I created my own if statements to solve this problem, but this problem still exist if I press two arrow keys at the same time or if I shortly press up or down and directly the left key. I really would like to know if anyone could solve this problem. Hope you understand my problem.

e.g.: snake moves to the right direction. Now I press up and directly left before the snake moves. Snake runs into itself and I die. This problem exists for all four directions :(

My code for the movements during gameloop:

for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT:
if direction == block_size:
direction = 'right'
lead_x_change = block_size
lead_y_change = 0
else:
direction = 'left'
lead_x_change = -block_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
if lead_x_change == - block_size:
direction = 'left'
lead_x_change = - block_size
lead_y_change = 0
else:
direction = 'right'
lead_x_change = block_size
lead_y_change = 0
elif event.key == pygame.K_UP:
if lead_y_change == block_size:
direction = 'down'
lead_y_change = block_size
lead_x_change = 0
else:
direction = 'up'
lead_y_change = -block_size
lead_x_change = 0
elif event.key == pygame.K_DOWN:
if lead_y_change == - block_size:
direction = 'up'
lead_y_change = - block_size
lead_x_change = 0
else:
direction = 'down'
lead_y_change = block_size
lead_x_change = 0



Works quite well but, as I mentioned if you press two arrow keys and one is to the negative direction of the current movement, you will die. Especially at low FPS

HelloGermanCom