问题描述
我最近开始使用 pygame,并且正在关注 TheNewBostons 的 youtube 教程.这是我的主要游戏循环:
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()
现在我需要防止蛇在向某个方向移动时吃掉自己.例如,然后它向右移动,按我键盘上的 A 按钮会立即让它自己吃,如果它向上移动,按 S 按钮会做同样的事情.有没有办法防止这种情况发生?
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?
推荐答案
一个简单的解决方案可能是通过检查蛇移动的方向是否与请求的方向相反来防止它倒退.这将涉及一些额外的检查.像这样:
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
这篇关于Pygame 蛇吃自己的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!