2017-09-24 60 views
1

我正在制作一款游戏,并想知道如何让我的“玩家”或者绿色作品移动。每当我点击任何箭头时,他都会右移,因为这就是我所能想到的。动画代码在第27行和第28行。我需要绿色的圆圈可以在每次单击箭头时左右移动5个像素。 编辑:我也需要碰撞,当它击中红色子弹的任何地方,因为在我当前的代码中,子弹必须击中玩家的确切坐标。我不能让我的'玩家'移动到pygame的两种方式,我该怎么做?

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() 
+0

您需要用代码替换语句:'circlerect.x + = 5',该代码将检测按下的不同键并执行其他操作。因为它总是为'circleerect.x'增加5。你有没有尝试类似'if event.key == pygame.K_RIGHT:...' – Bill

回答

1

线30和31分别是:

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

当任何键被按下并且随后将移动circlerect对象5个单位向右(总是在正确的,因为它始终是此代码检测+ =)。

如果你想移动两种方式,你将不得不检测不仅仅是KEYDOWN。您必须检测何时按下按键(并减去5)以及何时按下(并加上5)。

1

正如艾萨克提到的那样,您必须检查用户按下了哪个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,并提高圈子的速度。