2011-10-04 56 views
0

我正在嘗試爲我的軟件工程類目前的作業工作。我們正在創建一個面向對象的大炮遊戲。我們只需要製造大炮併發射炮彈。Python OO-pygame大炮遊戲。無法獲得炮彈移動

目前我可以讓我的代碼在加農炮炮口點創建一個炮彈,但移動功能不幸地不會向上移動炮彈球(在開始實際炮彈弧火之前嘗試這一步)炮彈(我爲了便於查看它是否在大炮的炮口點創建而變成紅色)保持在原來的位置,並且我瞥見炮彈本身在右下方移動。我對我做錯了什麼感到困惑。或者爲什麼我無法讓球移動,因爲它應該。

import pygame 
    from colors import color 

class Cannonball(object): 
''' 
classdocs 
''' 
    _x = 135 
    _y = 310 

    def __init__(self): 
     ''' 
     Constructor for cannonball. Initiates x and y at initial values 
     of x == 135 and y == 310. at 30 FPS 
     ''' 
     self._x = Cannonball._x 
     self._y = Cannonball._y 
     clock = pygame.time.Clock() 
     time_passed = clock.tick(30) 
     time_passed_seconds = time_passed/1000.0 


    def draw(self, screen): 
     ''' 
     Draws the cannonball 
     '''  
     sprite = pygame.draw.circle(screen, color["red"], (self._x,self._y), 5) 
     return sprite 

    def move(self, screen, time_passed): 
     ''' 
     initiates the cannonball to move until its past the screen 
     ''' 
     speed = 50 
     self._x += speed+time_passed 
     self._y -= speed+time_passed  
     pygame.display.flip() 

這裏是我的炮彈類

import pygame 
    import sys 
    from cannon import Cannon 
    from cannonball import Cannonball 
    from colors import color 

    pygame.init() 
    '''create the GUI window''' 
    screen = pygame.display.set_mode((640,480)) 
    pygame.display.set_caption("Cannon Game")  
    background = screen.fill(color["white"]) 

    clock = pygame.time.Clock() 
    '''draw the Cannon onto the window''' 
    aCannon = Cannon 
    aCannon.draw(aCannon, screen) 
    pygame.display.flip() 

    while True: 
     '''handle keydown and quit events for Cannon Game. Space bar 
     fires the cannonball 
     ''' 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       exit() 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_SPACE: 
       aCannonball = aCannon.fire(screen) 
       cannon_ball = True 
      if cannon_ball == True: 
       aCannonball.draw(screen) 
       aCannonball.move(screen, time_passed) 
       pygame.display.flip() 

這是我的司機。試圖保持目前的簡單,因爲它幫助我獲得迄今爲止創造的東西。

import pygame 
    from colors import color 
    from cannonball import Cannonball 

    class Cannon (object): 
     ''' 
     classdocs 
     ''' 

    def __init__(self): 
      ''' 
      Constructor 
      ''' 


     def draw(self, screen): 
      pygame.draw.circle(screen, color["black"], (40,400), 20) 
      pygame.draw.polygon(screen, color["black"], ([30,400], [135, 300], [150,   320], [50,400]), 0) 

     def fire (self, screen): 
      print("Fire") //used this as a tester to make sure the fire call worked before attempting to have a cannonball created and move. 
      aCannonball = Cannonball() 
      aCannonball.draw(screen) 
      aCannonball.move(screen) 

最後我的大炮類包含火災函數,它創建炮彈,將它繪製到gui並啓動它移動。

我仍然試圖研究我可以做錯什麼,但任何幫助將很樂意讚賞。

編輯:我發現問題與我的炮彈不會移動的代碼。我在過去的時間裏增加了速度,而不是增加。它甚至可以在即將看到之前立即拍攝屏幕。

謝謝你的幫助!也許我可以通過這種方式進入弧火。

+0

你需要在cannonball類中縮進你的方法,目前它們不屬於那個類。 – Serdalis

+0

它們在代碼中縮進。當嘗試放置在代碼塊中時,我不得正確格式化代碼的這一部分。我編輯了代碼塊來糾正它。 –

+0

是否在代碼中縮進_x和_y? (我假設他們現在是) – Serdalis

回答

1

有幾個與框架的問題你已經張貼在這裏:

  1. 的方法和變量不會咬入CannonBall類,因此是全球範圍內的一部分,而不是類本身。

  2. 移動方法不應該有一個while循環,而是嘗試構建類似於以下代碼的遊戲。

  3. pygame.display是在你的炮彈類是不是正在運行離子主程序相同pygame.display,所以主顯示屏將不會被更新,做你正在做的事情,你需要通過在顯示器向炮彈班的主要程序進行更新。

  4. 而不是使用Cannon,你需要使用Cannon()CannonBallCannonBall()初始化這些類的構造函數。

遊戲框架:

while True: #main loop 
    handle input 
    update spritee 
    draw to screen 

,並將所有其他循環出來的精靈類的(在你的情況炮彈),因爲同時運行,程序無法獲得輸入或更新其他精靈。

編號重評改變移動到(寫在僞代碼):

def move(self, screen, time_passed): 
    x += speed*time_passed; 
    y += speed*time_passed; 
    if(x out of screen remove sprite); 
    if(y out of screen remove sprite); 
+0

感謝我花了一段時間才解決問題,但您的指導非常有幫助!然而,其中的一部分是弄清楚爲什麼它不會移動,而且是這樣。這只是移動得太快而已。 x + =速度+ time_passed讓我度過了。 –

+0

多數民衆贊成這是很好聽,儘量使用'speed * time_passed',但降低速度,直到你有一個快樂的速度,它的原因'*'是因爲你想保持程序速度不變,無論電腦有多快。如果你的問題得到解答,請標記爲答案,它會幫助我和任何有類似問題的人:) – Serdalis

+0

以爲我已標記爲已回答。就是現在。我現在的工作速度超過了time_passed,並試圖實際融入它應該觸發的弧線。比我想象的要強硬一點。認爲這可能是我如何在主循環中聲明我的變量和移動方法。如此接近,但我感到如此遙遠。 –

0

您還沒有實際實例炮彈 - 你aCannonball變量只是在類的引用。你需要調用一個類來實例化它。完成之後,您可以在方法調用中刪除對炮彈的重複引用。

0

我寫了一個類似的遊戲,我必須創建。也許這會有所幫助:

''' 
Created on Oct 2, 2011 

@author: Mac Spinks 
''' 
import math 
import pygame 

class Cannonball(object): 

    RADIUS = 4 

    def __init__(self, initial_x, initial_y, launch_angle, launch_velocity, time_interval): 
     self.x = initial_x 
     self.y = initial_y 
     self.time_interval = time_interval 
     self.angle = launch_angle 
     self.x_velocity = launch_velocity*math.cos(self.angle) 
     self.y_velocity = launch_velocity*math.sin(self.angle) 
     self.is_flying = True 
     self.x_delta = self.time_interval * self.x_velocity 

    def move(self): 

     prev_y_velocity = self.y_velocity 
     self.y_velocity = self.y_velocity - (9.8 * self.time_interval) 
     self.y = (self.y + (self.time_interval * 
          ((prev_y_velocity + self.y_velocity)/ 2))) 
     self.y = max(self.y, 0) 
     self.x += (self.x_delta) 
     self.container = pygame.Rect(self.x - self.RADIUS, self.y - self.RADIUS, self.RADIUS*2, self.RADIUS*2) 
     self.is_flying = (self.y > 0) 
     self.hit_ground = (self.y <= 0)