2015-05-11 56 views
0

我正在嘗試創建一個小而簡單的模擬,使流星出現在屏幕中間並移開......直到用戶按下一個鍵。按下鍵時,路徑會動態修正爲新路徑。我嘗試了兩種不同的方法,首先是KEYDOWN,然後是pygame.key.get_pressed()函數,但在這兩種情況下,在釋放密鑰後,圖像「Meteor」再次下降到其原始路徑。如何製作精靈正確的路徑?

我希望能指出我做錯了什麼。

這裏我到目前爲止的代碼:

import pygame,sys 
from pygame.locals import * 
from colors import * 
import random 


FPS = 30 
fpsClock = pygame.time.Clock() 


pygame.init() 

X_Display = 400 
Y_Display = 400 

DisplaySurf = pygame.display.set_mode((X_Display, Y_Display)) 
DisplaySurf.fill(Silver) 
pygame.display.set_caption("Meteor") 

X_Real_Max = X_Display/2 
Y_Real_Max = Y_Display/2 
X_Center = 0 
Y_Center = 0 

XMeteor = 40 
YMeteor = 20 
XCenterMeteor = 0 
YCenterMeteor = 0 
XPosMeteor = XCenterMeteor + (200 - (XMeteor/2)) 
YPosMeteor = YCenterMeteor + (200 - (YMeteor/2)) 

imgMeteor = pygame.image.load("Desktop\imgMeteor.png") 

VisEffect = 1.05 


while True: 

    XMeteor *= VisEffect 
    YMeteor *= VisEffect 

    XCenterMeteor *= VisEffect 
    YCenterMeteor *= VisEffect 

    XPosMeteor = XCenterMeteor + (200 - (XMeteor/2)) 
    YPosMeteor = YCenterMeteor + (200 - (YMeteor/2)) 

    #Pressed = pygame.key.get_pressed() 
    #if Pressed[pygame.K_w]: 
     #YPosMeteor -= 10 

    if XMeteor > 150: 
     XMeteor = 40 
     YMeteor = 20 
     XCenterMeteor = random.randrange(-100, 100) 
     YCenterMeteor = random.randrange(-100, 100) 
     XPosMeteor = XCenterMeteor + (200 - (XMeteor/2)) 
     YPosMeteor = YCenterMeteor + (200 - (YMeteor/2)) 

    Meteor = pygame.transform.scale(imgMeteor, (int(XMeteor),int(YMeteor))) 


    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
     #if event.type == KEYDOWN: 
      #if event.key == K_w: 
       #YPosMeteor -= 50 
     #elif event.type == KEYUP: 
      #YPosMeteor = YPosMeteor 


    DisplaySurf.blit(Meteor, (XPosMeteor, YPosMeteor)) 
    pygame.display.update() 
    fpsClock.tick(FPS) 
    DisplaySurf.fill(Silver) 

回答

0

你更新錯誤的變量。 YPosMeteor每次都會在循環中被破壞,所以當你改變這個變量時沒有持久的效果。您想要更改YCenterMeteorYMeteor

+0

哦!對,這樣做更有意義。我檢查出來! – user3604362