2017-09-24 52 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,並提高圈子的速度。