Pythonoo游戏加农炮游戏。无法使炮弹移动

Pythonoo游戏加农炮游戏。无法使炮弹移动,python,move,pygame,Python,Move,Pygame,目前我正试图为我的软件工程课做家庭作业。我们正在创建一个面向对象的加农炮游戏。我们只需要制造大炮并发射炮弹 目前,我可以让我的代码在火炮的炮口点创建炮弹,但不幸的是,move函数并没有以向上的角度移动炮弹(在开始实际的炮弹弧射击之前先尝试此操作),而是当前的炮弹(为了便于查看它是否是在大炮的炮口处创建的,我将其标为红色)停留在原来的位置,我瞥见大炮本身在右下方移动。我很困惑我可能做错了什么。或者为什么我不能让球按应有的方式移动 import pygame from colors

目前我正试图为我的软件工程课做家庭作业。我们正在创建一个面向对象的加农炮游戏。我们只需要制造大炮并发射炮弹

目前,我可以让我的代码在火炮的炮口点创建炮弹,但不幸的是,move函数并没有以向上的角度移动炮弹(在开始实际的炮弹弧射击之前先尝试此操作),而是当前的炮弹(为了便于查看它是否是在大炮的炮口处创建的,我将其标为红色)停留在原来的位置,我瞥见大炮本身在右下方移动。我很困惑我可能做错了什么。或者为什么我不能让球按应有的方式移动

    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)
最后,我的cannon类包含了fire函数,它创建了炮弹,将其绘制到gui并启动它移动

我仍在努力研究我可能做错了什么,但任何帮助都将不胜感激

编辑:我发现了我的炮弹无法移动的代码问题。我是用时间乘以速度,而不是加上。它几乎会在被看到之前就从屏幕上消失


谢谢你的帮助!也许我可以越过这个进入arc fire。

你在这里发布的框架有几个问题:

  • 方法和变量没有缩进到CannonBall类中,因此是全局范围的一部分,而不是类本身

  • move方法不应该有while循环,相反,请尝试按照下面的代码构造游戏

  • 炮弹类中的
    pygame.display
    与主程序中运行的
    pygame.display
    不同,因此主显示不会更新,要执行您正在执行的操作,您需要将主程序中的显示传递给炮弹类以更新它

  • 您需要使用
    Cannon()
    CannonBall()初始化这些类的构造函数,而不是使用
    Cannon
    CannonBall()

  • 游戏框架:

    while True: #main loop  
        handle input  
        update spritee 
        draw to screen
    
    并将所有其他循环移出sprite类(在您的示例中是cannonball),因为当它运行时,您的程序无法获取输入或更新其他sprite

    Id建议更改移动到(以伪代码编写):


    您实际上还没有实例化Cannonball-您的aCannonball变量只是对类的引用。您需要调用一个类来实例化它。完成此操作后,您可以在方法调用中删除对Cannonball的重复引用。

    我已经为我必须创建的游戏编写了一个类似的类。这可能会有所帮助:

    '''
    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)
    
    “”
    创建于2011年10月2日
    @作者:麦克·斯宾克斯
    '''
    输入数学
    导入pygame
    类炮弹(对象):
    半径=4
    定义初始时间(自身、初始时间x、初始时间y、发射角度、发射速度、时间间隔):
    self.x=初始值
    self.y=初始值
    self.time\u interval=时间间隔
    self.angle=发射角度
    self.x\u速度=发射速度*math.cos(self.angle)
    self.y\u速度=发射速度*math.sin(self.angle)
    self.u飞行=真的吗
    self.x_delta=self.time_间隔*self.x_速度
    def移动(自我):
    prev_y_速度=self.y_速度
    self.y_速度=self.y_速度-(9.8*self.time_间隔)
    self.y=(self.y+(self.time_间隔*
    ((上一个y速度+自y速度)/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您需要在cannonball类中缩进您的方法,目前它们不属于该类。它们在代码中缩进。在尝试放入代码块时,我一定没有正确格式化代码的该部分。我已编辑代码块以更正此问题。代码中是否也缩进了_x和_y?(我假设他们现在是)是的。谢谢你的投入。我正在尝试重新编写我的代码。谢谢我花了一段时间才完成,但你的指导帮了我很大的忙!不过部分原因是想知道它为什么不会移动,它确实是。它移动得太快了。x+=速度+时间让我通过了。很高兴听到这个消息,尝试使用
    sp不过,eed*时间已经过去了,但是请降低速度,直到达到满意的速度,原因是无论计算机有多快,您都希望保持程序速度不变。另外,如果您的问题得到回答,请标记为答案,这将帮助我和任何有类似问题的人:)我以为我已经标记为已回答。现在是。我现在的工作速度超过了时间的流逝,并试图实际合并它应该触发的弧。可能比我想象的要困难一些。我想这可能是我在主循环中声明变量和移动方法的方式。如此接近,但我感觉如此之远。
    
    '''
    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)