2011-10-04 74 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)