gpt4 book ai didi

python - 怎样才能让蛇继续移动呢?

转载 作者:行者123 更新时间:2023-12-01 09:27:11 25 4
gpt4 key购买 nike

作为一名新程序员,我正在 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()

最佳答案

看来你的移动算法是错误的,对于 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`

关于python - 怎样才能让蛇继续移动呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50287124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com