Python 我能';我的';播放器';要在pygame中双向移动,我该怎么做?

Python 我能';我的';播放器';要在pygame中双向移动,我该怎么做?,python,pygame,Python,Pygame,我正在做一个游戏,想知道如何让我的“玩家”或绿色棋子双向移动。每当我点击任何一个箭头,他都会向右移动,因为这就是我所能想到的。动画代码位于第27行和第28行。我需要绿色圆圈能够在每次单击箭头时左右移动5个像素。 编辑:我还需要碰撞发生在它击中红色子弹的任何地方,因为在我当前的代码中,子弹必须击中玩家的精确坐标 import pygame import random BLACK = (0,0,0) WHITE = (255,255,255) GREEN = (0,255,0) RED = (255

我正在做一个游戏,想知道如何让我的“玩家”或绿色棋子双向移动。每当我点击任何一个箭头,他都会向右移动,因为这就是我所能想到的。动画代码位于第27行和第28行。我需要绿色圆圈能够在每次单击箭头时左右移动5个像素。 编辑:我还需要碰撞发生在它击中红色子弹的任何地方,因为在我当前的代码中,子弹必须击中玩家的精确坐标

import pygame
import random
BLACK = (0,0,0)
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
pygame.init()
size = (700,700)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Dodger")
done = False
clock = pygame.time.Clock()

def resetpositions():
    global bulletrect1, bulletrect2, bulletrect3, bulletrect4, bulletrect5, circlerect
    bulletrect1 = pygame.rect.Rect((350, 0, 20, 20))
    bulletrect2 = pygame.rect.Rect((175, 0, 20, 20))
    bulletrect3 = pygame.rect.Rect((525, 0, 20, 20))
    bulletrect4 = pygame.rect.Rect((525, 0, 20, 20))
    bulletrect5 = pygame.rect.Rect((525, 0, 20, 20))

circlerect = pygame.rect.Rect((350, 600, 20, 20))

resetpositions()
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
            circlerect.x += 5
    bulletrect1.y += 1
    bulletrect2.y += 2
    bulletrect3.y += 3
    bulletrect4.y += 4
    bulletrect5.y += 5
    screen.fill(BLACK)
    pygame.draw.circle(screen, GREEN, (circlerect.center), 15)
    pygame.draw.circle(screen, RED, (bulletrect1.center), 20)
    pygame.draw.circle(screen, RED, (bulletrect2.center), 20)
    pygame.draw.circle(screen, RED, (bulletrect3.center), 20)
    pygame.draw.circle(screen, RED, (bulletrect4.center), 20)
    pygame.draw.circle(screen, RED, (bulletrect5.center), 20)
    if bulletrect1.y == 800:
        bulletrect1.y = 0
        bulletrect1.x = random.randint(20,680)
    if bulletrect2.y == 800:
        bulletrect2.y = 0
        bulletrect2.x = random.randint(20,680)
    if bulletrect3.y == 800:
        bulletrect3.y = 0
        bulletrect3.x = random.randint(20,680)
    if bulletrect4.y == 800:
        bulletrect4.y = 0
        bulletrect4.x = random.randint(20,680)
    if bulletrect5.y == 800:
        bulletrect5.y = 0
        bulletrect5.x = random.randint(20,680)
    if circlerect.x == 685:
       circlerect.x = 15
    if circlerect.collidelist((bulletrect1, bulletrect2, bulletrect3, bulletrect4, bulletrect5)) == 0:
        screen.fill(BLACK)
        font = pygame.font.SysFont('Calibri',40,True,False)
        text = font.render("GAME OVER",True,RED)
        screen.blit(text,[250,350])
        pygame.display.update()
        pygame.time.delay(3000)
        resetpositions()
    pygame.display.flip()
    clock.tick(300)
pygame.quit()
第30行和第31行是:

if event.type == pygame.KEYDOWN:
            circlerect.x += 5
此代码检测何时按下任何键,并随后将circlerect对象向右移动5个单位(始终向右,因为它始终为+=)


如果你想双向移动,你需要检测的不仅仅是按键。您必须检测按键的左键(减去5)和右键(加5)。

正如Isaac所述,您必须检查用户按下的键,并相应地向左移动
-=5
或向右移动
+=5

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
        circlerect.x -= 5
    elif event.key == pygame.K_RIGHT:
        circlerect.x += 5
问题是,每次按键只移动播放器一次,您必须添加变量,如
向左移动
,以获得连续移动。在您的情况下,使用
pygame.key.get_pressed
比处理事件循环中的事件更容易,因为您可以使用它来检查是否按下了键

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        done = True

# A list of the pressed keys.
keys = pygame.key.get_pressed()
# If the key at index `K_LEFT` is held.
if keys[pygame.K_LEFT]:
    circlerect.x -= 5
elif keys[pygame.K_RIGHT]:
    circlerect.x += 5

旁注:
时钟。勾号(300)
过高。最好将游戏速度限制在30或60 fps,并提高圆圈的速度。

您需要将语句:
circlerect.x+=5
替换为可以检测不同按键并执行不同操作的代码。按原样,它总是将5添加到
circlerect.x
。您是否尝试过类似于
if event.key==pygame.K\u RIGHT:…