python - 我无法让 'player' 在 pygame 中双向移动,我该怎么做?

标签 python pygame

我正在制作一个游戏,想知道如何让我的“玩家”或绿色棋子双向移动。每当我单击任何箭头时,他都会向右移动,因为这就是我所能弄清楚的。动画代码位于第 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

此代码会检测何时按下任意键,然后将圆矩形对象向右移动 5 个单位(始终向右,因为它始终为 +=)。

如果你想双向移动,你将不得不检测的不仅仅是 KEYDOWN。您必须检测按键何时为左键(并减 5)以及何时为右键(并加 5)。

关于python - 我无法让 'player' 在 pygame 中双向移动,我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46387070/

相关文章:

python - 如何使对象出现在窗口边框的开头(或结尾)(使用pygame)

python - 用Python 3编写iOS游戏的最佳库是什么?

python - 无法为我的 Ping Pong pygame 创建一个 Hitbox

python - 如何腌制一个空文件?

python - flask 容器异常行为

python - 从 .xlsx 获取单元格颜色

python - 根据已过滤的 pandas 数据帧绘制定义的 x/y 范围的 pcolormesh,即使该行或列不存在于已过滤的数据帧中

python - 安装 pygame ubuntu 时遇到问题

python 不被识别为内部或外部命令

python - 椭圆路径中的动画pygame Sprite