Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6672561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:31:22+00:00 2026-05-26T03:31:22+00:00

I am attempting to work on a homework for my Software Engineering class currently.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:31:23+00:00Added an answer on May 26, 2026 at 3:31 am

    There are a couple of problems with the framework you have posted here:

    1. The methods and variables are not indented into the CannonBall class and thus are part of the global scope, not the class itself.

    2. The move method should not have a while loop, instead, try and structure your game similar to the following code.

    3. The pygame.display that is in your cannonball class is not the same pygame.display that 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.

    4. Instead of using Cannon and CannonBall you need to use Cannon() and CannonBall() to initialise the constructors of those classes.

    Game Framework:

    while True: #main loop  
        handle input  
        update spritee 
        draw to screen
    

    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):

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am attempting to get my android app to work on both phone type
I am attempting to get the following to work filename = File.expand_path(./script) filename.taint $SAFE
I am attempting to work with Google Checkout on Google App Engine. Currently, I
I'm attempting to work on a multiplayer game for android and would like to
I'm attempting to get the following site to work: The Test logo appears properly
I am attempting to write a custom membership class. It seems to work ok
I'm attempting to work in Fotran 77, and I've found the need for a
I'm attempting to use an UpdatePanel, but can't get partial-page updates to work. When
For a client, I am attempting to get this site to work in IE
How do I get ListBox, Canvas and Thumb to work together? I am attempting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.