我的Pygame蛇游戏陷入无限循环?

时间:2012-06-28 17:36:27

标签: python pygame

在过去的几个月里,我一直在努力学习python,我决定进行蛇形游戏,以进一步提高我的知识水平。我不知道出了什么问题,但它在我的输出中被卡住了。我尝试过多种方法,但都没有取得成功。这是我的代码:

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 个答案:

答案 0 :(得分:2)

以下内容会缩小“移动列表”的大小

def Update_Past_Moves():
    past_moves.popleft()

所以,当你试图打印蛇时,最终将无法画出:

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

这就是为什么,你所看到的只是蛇的'头',@字符

答案 1 :(得分:0)

无限循环似乎没问题,因为您正在使用while 1:

你正在打印头部总是一个单元格的位置,这就是你在第二行得到@的原因:

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

if location != 'O':行无法生成,因为location是一对坐标而不是char。