我正在嘗試爲我的軟件工程類目前的作業工作。我們正在創建一個面向對象的大炮遊戲。我們只需要製造大炮併發射炮彈。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