I was working on the basic game "Snake" in Python using "pygame". When I move around at the start and I am alive, my arrow keys are working fine. When I die, then I can't use any key and I can't close the window by pressing the X button on the top right of the window. The only way to terminate it is by pressing Ctrl-x
in the console, and that way it doesn't close.
When I debugged it, my console says that my q
and c
values are 59
, 248
accordingly but the pygame. K_q
and pygame.K_c
have the values 113
, 99.
Does anybody knows the reason? My code when I die is the following:
while game_close == True:
self.dis.fill(colors("blue"))
self.message("You Lost! Press C-Play Again or Q-Quit", colors("red"))
self.Your_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
elif event.key == pygame.K_c:
game_close = False
self.gameLoop()
If anybody has any clue why this happened it will be useful. It is my first time writing any quest so sorry for not having the best description or there is any duplicate( I have searched and found nothing that works for me)
edit: This is the whole script:
game_over = False
game_close = False
x1 = self.dis_width / 2
y1 = self.dis_height / 2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
foodx = round(random.randrange(0, self.dis_width - self.snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, self.dis_height - self.snake_block) / 10.0) * 10.0
flagex=True
flagey=True
while not game_over:
while game_close :
self.dis.fill(colors.blue())
self.message("You Lost! Press C-Play Again or Q-Quit", colors.red())
self.Your_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_close = True
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
elif event.key == pygame.K_c:
game_close = False
self.gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and flagey==True:
flagey = False
flagex = True
x1_change = -self.snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT and flagey==True:
flagey = False
flagex = True
x1_change = self.snake_block
y1_change = 0
elif event.key == pygame.K_UP and flagex==True:
flagey = True
flagex = False
y1_change = -self.snake_block
x1_change = 0
elif event.key == pygame.K_DOWN and flagex==True:
flagey = True
flagex = False
y1_change = self.snake_block
x1_change = 0
if x1 >= self.dis_width or x1 < 0 or y1 >= self.dis_height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
self.dis.fill(colors.blue())
pygame.draw.rect(self.dis, colors.green(), [foodx, foody, self.snake_block, self.snake_block])
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List) > Length_of_snake:
del snake_List[0]
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
self.our_snake(self.snake_block, snake_List)
self.Your_score(Length_of_snake - 1,)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, self.dis_width - self.snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, self.dis_height - self.snake_block) / 10.0) * 10.0
Length_of_snake += 1
self.clock.tick(self.snake_speed)
pygame.quit()
quit()