我正在尝试为我的软件工程类目前的作业工作。我们正在创建一个面向对象的大炮游戏。我们只需要制造大炮并发射炮弹。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并启动它移动。
我仍然试图研究我可以做错什么,但任何帮助将很乐意赞赏。
编辑:我发现问题与我的炮弹不会移动的代码。我在过去的时间里增加了速度,而不是增加。它甚至可以在即将看到之前立即拍摄屏幕。
谢谢你的帮助!也许我可以通过这种方式进入弧火。
你需要在cannonball类中缩进你的方法,目前它们不属于那个类。 – Serdalis
它们在代码中缩进。当尝试放置在代码块中时,我不得正确格式化代码的这一部分。我编辑了代码块来纠正它。 –
是否在代码中缩进_x和_y? (我假设他们现在是) – Serdalis