2017-10-09 61 views
1

我正在學習使用Python 3.6的PyGame模塊,並且在獲取知識的旅程中,我決定創建一個Snake遊戲。我的遊戲代碼在下面,除了蛇和蘋果不對齊,並且如果它們不對齊,吃蘋果是非常困難的,一切似乎都很好。 (他們關閉的只是幾個像素是否有幫助)蛇和蘋果不對齊

# Imports 
import pygame 
import random 

# Colors 
white = (255, 255, 255) 
black = (0, 0, 0) 
red = (255, 0, 0) 
green = (0, 255, 0) 

# Setup 
pygame_init = pygame.init() 
dis_width = 1000 
dis_height = 600 
window = pygame.display.set_mode((dis_width, dis_height)) 
pygame.display.set_caption("Slither") 
window.fill(white) 
fps = 10 
clock = pygame.time.Clock() 
size = 25 
apple = [[0, 0]] 
snake = [[0, 0]] 
x_step, y_step = 0, 0 
apples_ate = 0 

# Game Loop 
main_menu = True 
play_game = False 
death_screen = False 
while True: 
    # Main Menu 
    while main_menu: 
     # Setup 
     snake = [[(dis_width - 200)/2, dis_height/2, size]] 
     apple = [[random.randrange(0, dis_width - 200 - size), random.randrange(0, dis_height - size), size]] 

     # Message 
     x_step, y_step = 0, 0 
     window.fill(black, rect=[0, 0, dis_width, dis_height]) 
     font = pygame.font.SysFont(None, 50) 
     window.blit(font.render("Press 'Space' To Continue", True, green), [100, 200]) 
     pygame.display.update() 

     # Event Handler 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 
      elif event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_SPACE: 
        main_menu = False 
        play_game = True 

    # Play Menu 
    while play_game: 

     # Event Handler 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 
      elif event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_UP: 
        y_step = -size 
        x_step = 0 
       elif event.key == pygame.K_DOWN: 
        y_step = size 
        x_step = 0 
       elif event.key == pygame.K_LEFT: 
        x_step = -size 
        y_step = 0 
       elif event.key == pygame.K_RIGHT: 
        x_step = size 
        y_step = 0 

     # Boundries 
     if snake[0][0] + x_step < 0 or snake[0][0] + x_step > dis_width - size - 200: 
      play_game = False 
      death_screen = True 
     elif snake[0][1] + y_step < 0 or snake[0][1] + y_step > dis_height - size: 
      play_game = False 
      death_screen = True 
     else: 
      snake[0][0] += x_step 
      snake[0][1] += y_step 

     # Apple Test 
     if apple[0][0] == snake[0][0]: 
      apple = [[random.randrange(0, dis_width - 200 - size), 
random.randrange(0, dis_height - size), size]] 
      apples_ate += 1 

     # Draws 
     window.fill(white, rect=[dis_width - 200, 0, 200, dis_height]) 
     window.fill(black, rect=[0, 0, dis_width - 200, dis_height]) 
     font = pygame.font.SysFont(None, 50) 
     window.blit(font.render("SCORE:", True, black), [dis_width - 200, 0]) 
     window.blit(font.render(str(apples_ate), True, black), [dis_width - 200, 50]) 
     pygame.draw.rect(window, red, [apple[0][0], apple[0][1], size, size]) 
     pygame.draw.rect(window, green, [snake[0][0], snake[0][1], size, size]) 
     pygame.display.update() 
     clock.tick(fps) 

    # Death Screen 
    while death_screen: 
     # Message 
     window.fill(black, rect=[0, 0, dis_width - 200, dis_height]) 
     font = pygame.font.SysFont(None, 50) 
     window.blit(font.render("You died.", True, red), [100, 100]) 
     window.blit(font.render("Press 'A' To Play Again.", True, red), [100, 200]) 
     window.blit(font.render("Press 'Q' To Quit", True, red), [100, 300]) 
     pygame.display.update() 

     # Event Handler 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 
      elif event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_a: 
        main_menu = True 
        death_screen = False 
       elif event.key == pygame.K_q: 
        pygame.quit() 
        quit() 

回答

1

你的錯誤是在apple = [[random.randrange(0, dis_width - 200 - size), random.randrange(0, dis_height - size), size]]。蘋果的位置可以是(0,800)以內的任何東西。所以它可以是12 835 125,你的蛇在(400,300)這是(dis_width/2-200,dis_height)。而你的指定x_step +/- 25,大小這是蛇的運動。
所以你的蛇的位置以25的遞減或遞增移動。0,25,50,.....,375,400,425,.....,750,775。如果你的蘋果位置是125或275 [125%25 = 0],那就沒有問題了。
但是,如果你的蘋果位置是122興旺... if apple[0][0] == snake[0][0]:你的蛇可以從來沒有吃他最喜歡的水果。 解決方法是在你的randrange函數中添加一步apple = [[random.randrange(0, dis_width - 200 - size,size), random.randrange(0, dis_height - size,size), size]]這將會在 0,25,50,.....,725,750,775內生成隨機數(如果它生成解決方案,我不知道它也會生成800)。

還有其他的小錯誤if apple[0][0] == snake[0][0]:你必須匹配x和y的位置編輯也是if apple[0][0] == snake[0][0] and apple[0][1] == snake[0][1]: