Python 怎样才能使蛇继续移动?
作为一名新程序员,我正在pygame中编写一个蛇游戏代码。我选择使用元组列表并通过按键盘上的键来移动它们,但在移动过程中遇到了一些问题。我不知道如何使它继续移动,当我按下一个键。我尝试使用event.get()代替key.pressed()并使用while循环,但仍然不起作用Python 怎样才能使蛇继续移动?,python,python-3.x,events,pygame,keyboard-events,Python,Python 3.x,Events,Pygame,Keyboard Events,作为一名新程序员,我正在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()
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`
导入pygame
pygame.init()
w、 h=500500
screen=pygame.display.set_模式((w,h))
c=pygame.time.Clock()
pygame.key.set_重复(100)
运行=真
屏幕填充((255、255、255))
x1,x2,x3=70,90,110
y1,y2,y3=90,90,90
rc=10
速度=20
高潮=[(x1,y1),(x2,y2),(x3,y3)]
蛇色=(0255,0)
头=镜头(高潮)-1
def向前移动(高潮、头部、移动方向):
如果head==0:
对于范围内的i(len(climax)-1,0,-1):
打印(“{}-->{}.”格式(climax[i],climax[i-1]))
顶极
elif head==len(climax)-1:
对于范围内的i(len(climax)-1):
打印(“{}-->{}.”格式(climax[i+1],climax[i]))
顶极[i]=顶极[i+1]
如果movedir==“左”:
高潮[头]=(高潮[头][0]-速度,高潮[头][1])
elif movedir==“右”:
高潮[头]=(高潮[头][0]+速度,高潮[头][1])
elif movedir==“向上”:
高潮[头]=(高潮[头][0],高潮[头][1]-速度)
elif movedir==“向下”:
高潮[头]=(高潮[头][0],高潮[头][1]+速度)
印刷(高潮)
def画圈(屏幕、蛇色、高潮):
pygame.draw.circle(屏幕,蛇的颜色,(高潮[0]),rc,0)
pygame.draw.circle(屏幕,蛇色,(高潮[1]),rc,0)
pygame.draw.circle(屏幕,蛇的颜色,(高潮[2]),rc,0)
运行时:
c、 勾选(20)
屏幕填充((0,0,0))
pygame.draw.circle(屏幕,蛇的颜色,(高潮[0]),rc,0)
pygame.draw.circle(屏幕,蛇色,(高潮[1]),rc,0)
pygame.draw.circle(屏幕,蛇色,(高潮[-1]),rc,0)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_退出:
运行=错误
elif event.key==pygame.K_左:
如果climax[0][0]climax[-1][0]:
头=镜头(高潮)-1
打印('head为{}'。格式(head))
如果climax[head][0]>rc:
向前移动(高潮,头部,“左”)
画圆圈(屏幕、蛇色、高潮)
打破
elif event.key==pygame.K_UP:
如果climax[0][1]climax[-1][1]:
头=镜头(高潮)-1
打印('head为{}'。格式(head))
如果climax[head][1]>rc:
向前移动(高潮,抬头,向上)
画圆圈(屏幕、蛇色、高潮)
打破
elif event.key==pygame.K_向下:
如果climax[0][1]>climax[-1][1]:
水头=0
elif climax[0][1]climax[-1][0]:
水头=0
elif climax[0][0]
这真是奇怪的缩进。请检查缩进。