首页 > 解决方案 > 怎样才能让蛇继续移动?

问题描述

作为一名新程序员,我正在 pygame 中编写蛇游戏代码。我选择使用元组列表并通过按键盘上的键来移动它们,但是在移动时存在一些问题。当我按下一个键时,我不知道如何让它继续移动。我尝试使用 event.get() 而不是 key.pressed() 并使用 while 循环,但它仍然无法正常工作......

import pygame
pygame.init()

w, h = 500, 500
screen = pygame.display.set_mode((w, h))

c = pygame.time.Clock()


running = True

screen.fill((255,255,255))

x1 , x2 , x3 = 80 , 100 , 120
y1 , y2 , y3 = 80 , 80 , 80
rc = 10
speed = 20
climax = [ (x1 , y1 ) ,  (x2 , y2),  (x3 , y3)]

snake_color = (0,255,0)


while running:   
   c.tick(20)
   screen.fill((0, 0, 0))

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False
    if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
        running = False

pygame.draw.circle(screen , snake_color , (climax[0]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[1]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[-1]) , rc , 0)

key = pygame.key.get_pressed()
if key[pygame.K_LEFT] :
    del climax[2]
    climax.insert(2 , climax[1])
    climax.insert(1 ,  climax[0])
    climax.insert(0 , (climax[1][0]-speed , climax[1][1]))

pygame.draw.circle(screen , snake_color , (climax[0]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[1]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[2]) , rc , 0)

if key[pygame.K_UP]:
    del climax[2]
    climax.insert(2 ,  climax[1])
    climax.insert(1 ,  climax[0])
    climax.insert(0 , (climax[0][0] , climax[0][1]-speed))

pygame.draw.circle(screen , snake_color , (climax[0]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[1]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[2]) , rc , 0)

if key[pygame.K_DOWN]:
    del climax[2]
    climax.insert(2 ,  climax[1])
    climax.insert(1 ,  climax[0])
    climax.insert(0 , (climax[0][0] , climax[0][1]+speed))

pygame.draw.circle(screen , snake_color , (climax[0]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[1]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[2]) , rc , 0)

if key[pygame.K_RIGHT]:
    del climax[0]
    climax.insert(0 ,  climax[1])
    climax.insert(1 ,  climax[2])
    climax.insert(2 , (climax[1][0]+speed , climax[1][1]))

pygame.draw.circle(screen , snake_color , (climax[0]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[1]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[2]) , rc , 0)

pygame.display.update()
pygame.quit()
quit()          

标签: pythonpython-3.xeventspygamekeyboard-events

解决方案


看来你的移动算法是错误的,对于 pygame.event.get(),它会

调用后清空事件队列。下面的代码应该可以工作

import pygame
pygame.init()

w, h = 500, 500
screen = pygame.display.set_mode((w, h))

c = pygame.time.Clock()

pygame.key.set_repeat(100)


running = True

screen.fill((255, 255, 255))

x1 , x2 , x3 = 70 , 90 , 110
y1 , y2 , y3 = 90 , 90 , 90
rc = 10
speed = 20
climax = [ (x1 , y1 ) ,  (x2 , y2),  (x3 , y3)]

snake_color = (0,255,0)
head = len(climax)-1

def move_forward(climax, head, movedir):

    if head == 0:

        for i in range(len(climax)-1, 0, -1):
            print('{} --> {}'.format(climax[i], climax[i-1]))
            climax[i] = climax[i-1]

    elif head == len(climax)-1:
        for i in range(len(climax)-1):
            print('{} --> {}'.format(climax[i+1], climax[i]))
            climax[i] = climax[i+1]

    if movedir == 'left':
        climax[head] = (climax[head][0]-speed, climax[head][1])
    elif movedir == 'right':

        climax[head] = (climax[head][0]+speed, climax[head][1])
    elif movedir == 'up':
        climax[head] = (climax[head][0], climax[head][1]-speed)
    elif movedir == 'down':
        climax[head] = (climax[head][0], climax[head][1]+speed)

    print(climax)

def draw_circle(screen, snake_color, climax):
    pygame.draw.circle(screen , snake_color , (climax[0]) , rc , 0)
    pygame.draw.circle(screen , snake_color , (climax[1]) , rc , 0)
    pygame.draw.circle(screen , snake_color , (climax[2]) , rc , 0)

while running:   
    c.tick(20)
    screen.fill((0, 0, 0))

    pygame.draw.circle(screen , snake_color , (climax[0]) , rc , 0)
    pygame.draw.circle(screen , snake_color , (climax[1]) , rc , 0)
    pygame.draw.circle(screen , snake_color , (climax[-1]) , rc , 0)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_ESCAPE:
                running = False
            elif event.key == pygame.K_LEFT:
                if climax[0][0] < climax[-1][0]:
                    head = 0
                elif climax[0][0] > climax[-1][0]:
                    head = len(climax)-1

                print('head is {}'.format(head))

                if climax[head][0] > rc:

                    move_forward(climax, head, 'left')

                    draw_circle(screen, snake_color, climax)

                    break
            elif event.key == pygame.K_UP:
                if climax[0][1] < climax[-1][1]:
                    head = 0
                elif climax[0][1] > climax[-1][1]:
                    head = len(climax)-1

                print('head is {}'.format(head))

                if climax[head][1] > rc:

                    move_forward(climax, head, 'up')

                    draw_circle(screen, snake_color, climax)
                    break

            elif event.key == pygame.K_DOWN:
                if climax[0][1] > climax[-1][1]:
                    head = 0
                elif climax[0][1] < climax[-1][1]:
                    head = len(climax)-1

                print('head is {}'.format(head))

                if climax[head][1] < h-rc:
                    move_forward(climax, head, 'down')

                    draw_circle(screen, snake_color, climax)
                    break

            elif event.key == pygame.K_RIGHT:
                if climax[0][0] > climax[-1][0]:
                    head = 0
                elif climax[0][0] < climax[-1][0]:
                    head = len(climax)-1

                print('head is {}'.format(head))

                if climax[head][0] < w-rc:
                    move_forward(climax, head, 'right')

                    draw_circle(screen, snake_color, climax)
                    break


    pygame.display.update()
pygame.quit()
quit()    `enter code here`

推荐阅读