Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To make a "bullet" start firing while in motion in Python and Processing 3.54, you would need to create a game loop or animation loop that updates the position of the bullet and checks for collision with any obstacles.

One way to accomplish this is to use the "update" method of the bullet class to move the bullet position and check for any collisions. You can then call this update method repeatedly in the game loop to make the bullet move continuously.

Here is an example code snippet that demonstrates this:

class Bullet:
    def __init__(self, x, y, speed):
        self.x = x
        self.y = y
        self.speed = speed

    def update(self):
        self.x += self.speed

        # Check for collision with obstacles
        if self.x > width:
            # Reset bullet position if it goes off screen
            self.x = 0
            self.y = random(height)

    def display(self):
        # Display bullet as a circle
        ellipse(self.x, self.y, 10, 10)

# Initialize bullet
bullet = Bullet(0, height/2, 10)

def setup():
    size(400, 400)

def draw():
    background(255)

    # Update bullet position and check for collision
    bullet.update()

    # Display bullet
    bullet.display()

In this code, the "update" method of the "Bullet" class updates the position of the bullet by adding the speed to the x-coordinate. It also checks if the bullet goes off the screen, and if so, resets its position randomly.

The "draw" function calls the "update" method of the bullet object, and then displays it on the screen using the "display" method.

To make the bullet start firing while in motion, you can simply adjust the initial position of the bullet to be somewhere other than (0, height/2). For example, you could randomly generate the initial y-coordinate within a certain range.