I am attempting to work on a homework for my Software Engineering class currently. We are creating a object oriented cannon game. We just need to get the cannon created and fire a cannon ball.
Currently I can get my code to create a cannonball at the muzzle point of the Cannon, but the move function unfortunatly does not move the cannonball at an upward angle (attempting this first before starting the actual cannonball arc fire) Currently instead the cannonball (which i have made red for ease of seeing if it is created at the muzzle point of the cannon) stays where it is, and I have glimpsed the cannon itself move in the bottom right. I am perplexed as to what I could have done wrong. Or why I cannot get the ball to move as it should.
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()
Here is my cannonball class
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()
This is my Driver. Trying to keep that simple currently as it has helped me get things created so far.
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)
And finally my cannon class which contains the fire function, which creates the cannonball, draws it to the gui and initiates it to move.
I am still attempting to research what I could have done wrong but any help would be gladly appreciated.
EDIT: I found the issue with the code in which my cannonball would not move. I was multiplying speed by the time passed rather than adding. It would shoot off the screen almost immediately before it could even be seen.
Thank you for the help! Maybe I can move past this into the arc fire.
There are a couple of problems with the framework you have posted here:
The methods and variables are not indented into the
CannonBallclass and thus are part of the global scope, not the class itself.The move method should not have a while loop, instead, try and structure your game similar to the following code.
The
pygame.displaythat is in your cannonball class is not the samepygame.displaythat is being run ion your main program, so the main display will not be updated, to do what you are doing you need to pass the display in the main program to the cannonball class to update it.Instead of using
CannonandCannonBallyou need to useCannon()andCannonBall()to initialise the constructors of those classes.Game Framework:
And move all other loops out of the sprite classes (in your case the cannonball), because while that is running, your program cannot get input or update other sprites.
Id recomment changing move to (written in pseudocode):