Reputation: 23
So I am currently mid way through coding snake, in an attempt at learning pygame. I am quite new to coding so apologies if I have explained this badly. I have a game loop which is active when running = True, when the snake hits a boundary, running becomes False, ending the game loop and switches to a different while loop that says game over. I will of course add more to this screen later down the line. I am just currently trying to figure out how to make it when I click r, within this second loop, running becomes true again, triggering the game loop once again to effectively restart this game. I would really appreciate any suggestions, thank you!
import pygame
pygame.init()
display_x = 400
display_y = 300
display = pygame.display.set_mode((display_x, display_y))
pygame.display.set_caption("Ned's snake game")
x1 = display_x / 2
y1 = display_y / 2
x1_change = 0
y1_change = 0
clock = pygame.time.Clock()
fps = 45
game_over_font = pygame.font.Font("freesansbold.ttf", 16)
def game_over():
over_text = game_over_font.render("GAME OVER", True, (200, 0, 0))
display.blit(over_text, ((display_x / 2) - 54, (display_y / 2) - 16))
running = True
while running is True:
print("thing")
display.fill((50, 200, 20))
pygame.draw.rect(display, (0, 0, 255), [x1, y1, 10, 10])
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -5
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = 5
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -5
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = 5
x1_change = 0
elif event.key == pygame.K_ESCAPE:
pygame.quit()
x1 += x1_change
y1 += y1_change
if x1 <= 0 or x1 >= display_x or y1 <= 0 or y1 >= display_y:
running = False
end_screen = True
clock.tick(fps)
pygame.display.update()
while end_screen is True:
display.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
elif event.key == pygame.K_r:
running = True
end_screen = False
game_over()
pygame.display.update() ```
Upvotes: 2
Views: 188
Reputation: 901
One way to approach this would be to put your entire while running == True
block into a def
. Doing so will push a number of your variables out of scope. To get around that you can move most of them inside of the def
and use the global
keyword to access the rest, as I've shown in the code.
I've called that def playGame()
, and since you've put that block inside of playGame()
you'll have to execute the statement playGame()
to kickstart it, as you can see in the code. When the while
in playGame()
goes false it then falls through to the while end_screen == True
block which then checks for that r
key and, if it finds it, re-runs playGame()
.
This is a very quick and dirty fix to get you to where you want to be quickly, and I suspect that this has probably resulted in one or two bugs being created, but it's a start. If I were to offer you a constructive criticism it would be that you do not have a proper game loop in your program. If I get time later this evening I will add more commentary on that.
import pygame
pygame.init()
display_x = 400
display_y = 300
display = pygame.display.set_mode((display_x, display_y))
pygame.display.set_caption("Ned's snake game")
clock = pygame.time.Clock()
fps = 45
game_over_font = pygame.font.Font("freesansbold.ttf", 16)
def game_over():
over_text = game_over_font.render("GAME OVER", True, (200, 0, 0))
display.blit(over_text, ((display_x / 2) - 54, (display_y / 2) - 16))
def playGame():
global display_x, display_y, end_screen #"globals" are variables defined outside of the def block
x1 = display_x / 2 #moved these inside the def block
y1 = display_y / 2
x1_change = 0
y1_change = 0
#just moved this inside of a def block
running = True
while running is True:
#print("thing")
display.fill((50, 200, 20))
pygame.draw.rect(display, (0, 0, 255), [x1, y1, 10, 10])
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -5
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = 5
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -5
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = 5
x1_change = 0
elif event.key == pygame.K_ESCAPE:
pygame.quit()
x1 += x1_change
y1 += y1_change
if x1 <= 0 or x1 >= display_x or y1 <= 0 or y1 >= display_y:
running = False
end_screen = True
clock.tick(fps)
pygame.display.update()
playGame() #kickstart the game
while end_screen is True:
display.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
elif event.key == pygame.K_r:
running = True
end_screen = False
playGame() #restart the game
game_over()
pygame.display.update()
pygame.quit()
Upvotes: 2