我无法让我的玩家#39;要在pygame中双向移动,我该怎么做?

时间:2017-09-24 05:56:06

标签: python pygame

我正在制作一款游戏,并想知道如何让我的玩家“#39;或绿色双向移动。每当我点击任何一个箭头时,他就会向右移动,因为我可以理解这一切。动画代码在第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()

2 个答案:

答案 0 :(得分:1)

第30和31行是:

if event.type == pygame.KEYDOWN:
            circlerect.x += 5

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

如果您想要双向移动,则必须检测的不仅仅是KEYDOWN。您必须检测何时按下按键(并减去5)以及何时按下(并添加5)。

答案 1 :(得分:1)

正如Isaac所提到的,您必须检查用户按下了哪个key并相应地向左移动-= 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

问题在于,这只会在每次按键时移动玩家一次,并且您必须添加moving_left之类的变量才能获得连续移动。在您的情况下,使用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

旁注:clock.tick(300)太高了。最好将游戏限制在30或60 fps,并提高圈速。