2012-06-28 41 views
2

我在過去的幾個月中一直在嘗試學習python,並且決定讓一個貪吃蛇遊戲去進一步提高我的知識。我不知道什麼是錯誤的,但它被困在我的循環輸出中。我嘗試過很多東西,但都沒有成功。這裏是我的代碼:我的Pygame蛇遊戲得到卡在一個無限循環?

import pygame, sys 
from pygame.locals import * 
from collections import deque 

pygame.init() 

clock = pygame.time.Clock() 

background = [ 
    ['++++++++++++++++++++++++++++++'], 
    ['+       +'], 
    ['+       +'], 
    ['+       +'], 
    ['+       +'], 
    ['+       +'], 
    ['+       +'], 
    ['+       +'], 
    ['+       +'], 
    ['+       +'], 
    ['+       +'], 
    ['+       +'], 
    ['+       +'], 
    ['+       +'], 
    ['++++++++++++++++++++++++++++++']] 



screen_surface = background 
y, x = 7, 14 
location = (y, x) 

snake_head = '@' 
snake_body = 'x' 
direction = 'left' 
past_moves = deque([(7, 15), (7, 16), (7, 17), (7, 18)]) 

def Clear_ScreenSurface(): 
    screen_surface = background 

def Draw_ScreenSurface(): 
    for i in range(15): 
     a = screen_surface[i][:] 
     if i == 14: 
      return 
     print a 


def Update_Past_Moves(): 
    past_moves.popleft() 

def Print_Snake_Body(): 
    for i in range(len(past_moves)): 
     a1 = past_moves[i][0] - 1 
     a2 = past_moves[i][1] - 1 
     screen_surface[a1][a2:(a2 + 1)] = snake_body 

def Print_Snake_Head(): 
    screen_surface[location[0]][location[1]:(location[1] + 1)] = snake_head 

def Check_Collision(): 
    if location[1] == 0 or location[1] == 29: 
     pass 
    if location[0] == 0 or location[0] == 14: 
     pass 
    for a in range(len(past_moves)): 
     a = a - 1 
     if location[0] == past_moves[a][0] and location[1] == past_moves[a][1]: 
     pass 

def main(): 

    direction = 'left' 
    y, x = 7, 14 
    location = (y, x) 
    while 1: 
     Print_Snake_Head() 
     Print_Snake_Body() 
     Draw_ScreenSurface() 
     Clear_ScreenSurface() 
     past_moves.append(location) 

     for event in pygame.event.get(): 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_UP: 
        direction = 'up' 
       elif event.key == pygame.K_LEFT: 
        direction = 'left' 
       elif event.key == pygame.K_DOWN: 
        direction = 'down' 
       elif event.key == pygame.K_RIGHT: 
        direction = 'right' 

     if direction == 'up': 
      location = (y - 1, x) 
      y, x = location[0], location[1] 
     if direction == 'left': 
      location = (y, x - 1) 
      y, x = location[0], location[1] 
     if direction == 'down': 
      location = (y + 1, x) 
      y, x = location[0], location[1] 
     if direction == 'right': 
      location = (y, x + 1) 
      y, x = location[0], location[1] 

     if location != 'O': 
      Update_Past_Moves() 

     Check_Collision() 
     clock.tick(30) 

main() 

這是輸出:

['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x',  'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x',  'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x',  'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'] 
['+       +', '@', '@', '@', '@', '@', '@', '@', '@', '@', '@',  '@', '@', '@', '@'] 
['+       +'] 
['+       +'] 
['+       +'] 
['+       +'] 
['+       +'] 
['+       +'] 
['++++++++++++++++++++++++++++++'] 
['+       +'] 
['+       +'] 
['+       +'] 
['+       +'] 


Traceback (most recent call last): 
    File "C:\Users\Coding\Python Programming\Snake Game\snake_game.py", line 112, in <module> 
    main() 
    File "C:\Users\Coding\Python Programming\Snake Game\snake_game.py", line 78, in main 
    Draw_ScreenSurface() 
    File "C:\Users\Coding\Python Programming\Snake Game\snake_game.py", line 45, in Draw_ScreenSurface 
    print a 
KeyboardInterrupt 

我必須按CTRL-C(窗口)來結束它,並重復這個輸出,我不想垃圾郵件我的問題與不必要的代碼。提前致謝。

+2

無關的提示:不要這樣做:'爲我在範圍內(len(past_moves)):a1 = past_moves [i] [0] - 1'。這樣做:'對於past_moves中的past_move:a1 = past_move [0] - 1'。沒有必要爲了迭代而創建一個數字列表 - 而是重複原始序列。 – Kylotan

回答

2

下降低了您的舉動名單「

def Update_Past_Moves(): 
    past_moves.popleft() 

所以,當您嘗試打印蛇,終於將有什麼可借鑑的大小:

def Print_Snake_Body(): 
    for i in range(len(past_moves)): 
     # No moves left 

這就是爲什麼,所有你看到的是蛇的'頭',@字符

0

無限循環似乎沒問題,因爲你正在使用while 1:

您的打印頭總是一個單元的位置下來,這就是爲什麼你@第二排:

def Print_Snake_Head(): 
    screen_surface[location[0]][location[1]:(location[1] + 1)] = snake_head 

if location != 'O':好好嘗試一下做SENCE,因爲位置是一對座標不一個字符。