Reputation: 350
I've recently started using pygame and I'm following TheNewBostons's youtube tutorial. Here's my main game loop:
def game_loop():
global direction
global tdirection
lead_x=display_width/2
lead_y=display_height/2
block_size=10
change_x=10
change_y=0
game_exit=False
GameOver=False
main_menu=False
snakelist=[]
snakelength=1
applethickness=20
RandAppleX=round(random.randrange(0, display_width-applethickness )/10.0)*10.0
RandAppleY=round(random.randrange(0, display_height-applethickness )/10.0)*10.0
while not game_exit:
while GameOver==True:
game_display.fill(white)
message_screen('You lost, Press Q to quit or C to retry.', red)
pygame.display.update()
for event in pygame.event.get():
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_q:
loss.play()
game_exit=True
GameOver= False
elif event.key==pygame.K_c:
direction='right'
game_loop()
if event.type==pygame.QUIT:
game_exit=True
GameOver= False
for event in pygame.event.get():
if event.type==pygame.QUIT:
game_exit=True
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_a:
change_x=-block_size
direction='left'
change_x=-block_size
change_y=0
elif event.key==pygame.K_d:
direction='right'
change_x=block_size
change_y=0
elif event.key==pygame.K_w:
direction='up'
change_y=-block_size
change_x=0
elif event.key==pygame.K_s:
direction='down'
change_y=block_size
change_x=0
elif event.key==pygame.K_ESCAPE:
pause()
if lead_x>=display_width or lead_x<0 or lead_y>=display_height or lead_y<0:
loss.play()
GameOver=True
lead_x+=change_x
lead_y+=change_y
clock.tick(FPS)
snakehead=[]
snakehead.append(lead_x)
snakehead.append(lead_y)
snakelist.append(snakehead)
if len(snakelist)>snakelength:
del snakelist[0]
for segment in snakelist[:-1]:
if segment==snakehead:
loss.play()
GameOver=True
game_display.fill(white)
game_display.blit(aimg, (RandAppleX, RandAppleY))
snake(snakelist, block_size)
score(snakelength-1)
if lead_x>= RandAppleX and lead_x<= RandAppleX+applethickness and lead_y<= RandAppleY+applethickness and lead_y>= RandAppleY:
RandAppleX=round(random.randrange(0, display_width-applethickness )/10.0)*10.0
RandAppleY=round(random.randrange(0, display_height-applethickness )/10.0)*10.0
snakelength+=1
effect.play()
pygame.display.update()
pygame.quit()
quit()
Now I need to prevent the snake from eating itself when its moving in a certain direction. For example then its moving to the right, pressing the A button on my keyboard would instantly make it eat itself and if its moving up pressing the S button would do the same thing. Is there a way to prevent this from happening?
Upvotes: 4
Views: 414
Reputation: 146
While changing the direction, you can just check if the direction is the opposite to the current direction and, if that's the case, not set the new direction. This fixes the problem.
Upvotes: 0
Reputation: 1926
An easy solution could be to prevent the snake from backing into itself by checking that the direction it is moving in is not opposite the requested direction. This would involve a few extra checks. Something like this:
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_a and direction != 'right':
change_x=-block_size
direction='left'
change_x=-block_size
change_y=0
elif event.key==pygame.K_d and direction != 'left':
direction='right'
change_x=block_size
change_y=0
elif event.key==pygame.K_w and direction != 'down':
direction='up'
change_y=-block_size
change_x=0
elif event.key==pygame.K_s and direction != 'right':
direction='down'
change_y=block_size
change_x=0
Upvotes: 2