1

I'm making a game where a moving cube at the top of the screen will fire a cube down the screen at certain intervals. How would I do this. For example, I want it so that every 1 second the moving cube will fire a projectile down the screen towards the player icon and when it reaches past the screen, it will respawn where the moving cube is and be able to fire again.

This is what I have so far.

import pygame

pygame.init()

screen = pygame.display.set_mode((280, 800))

pygame.display.set_caption("Cube Run")

icon = pygame.image.load("cube.png")
pygame.display.set_icon(icon)

player_icon = pygame.image.load("cursor.png")
player_x = 128
player_y = 750
player_x_change = 0

cube_1 = pygame.image.load("rectangle.png")
cube1_x = 128
cube1_y = 0

cube1_x_change = 0.8

cube_fire = pygame.image.load("rectangle.png")
cube_fire_x = 0
cube_fire_y = 0

cube_y_change = 1.5
cube_fire_state = "ready"

def player(player_x, player_y):
    screen.blit(player_icon, (player_x, player_y))

def cube(cube1_x, cube1_y):
    screen.blit(cube_1, (cube1_x, cube1_y))

def cube_enemy(cube_fire_x, cube_fire_y):
    screen.blit(cube_fire, (cube_fire_x, cube_fire_y))

running = True
while running:

    screen.fill((255, 255, 255))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                player_x_change += 0.7
            if event.key == pygame.K_LEFT:
                player_x_change -= 0.7
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT or pygame.K_LEFT:
                player_x_change = 0

    player_x += player_x_change
    if player_x < 0:
        player_x = 0
    elif player_x > 280-32:
        player_x = 280-32

    cube1_x += cube1_x_change
    if cube1_x > 248:
        cube1_x_change = -1
        cube1_x += cube1_x_change
    elif cube1_x < 0:
        cube1_x_change = 1
        cube1_x += cube1_x_change

    cube_fire_x += cube1_x

    cube_enemy(cube_fire_x, cube_fire_y)

    player(player_x, player_y)

    cube(cube1_x, cube1_y)

    pygame.display.update()
Hamza Khan
  • 67
  • 9
  • I suggest you to pin point what is your exact question: what detail on how to do what you want to do are you unsure about? It will speed up the delays for obtaining a question. – jthulhu Aug 08 '20 at 17:10

2 Answers2

2

You can register events with pygame.time.set_timer. Create a new event and set how many milliseconds should pass before it's fired. This event will then appear at the set intervall.

FIRE_EVENT  = pygame.USEREVENT + 1  # This is just a integer.
OTHER_EVENT = pygame.USEREVENT + 2  # This is how you define more events.

pygame.time.set_timer(FIRE_EVENT, 1000)  # 1000 milliseconds is 1 seconds.

Then in your event loop, you check for this event and do whatever you want.

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        quit()
    elif event.type == FIRE_EVENT:  # Will appear once every second.
        make_square_fire()

When you want to disable the event, just set the interval to 0.

pygame.time.set_timer(FIRE_EVENT, 0)
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
0

In your code, you don't include a time manager of any kind – which means your code will run as fast as possible, an you can't really control how fast that will be, it will really depend on the machine it is working and on the CPU load, among other things.

Basically, you want to purposely wait within your program just the right amount of time so you can dynamically adapt to the execution speed. You could implement this by yourself (it isn't that hard, and there are plenty of tutorials out there), but to take a first glance of it, you could use pygame.Clock: first, create a clock with clock = pygame.Clock(). Then, within your main loop, call eta = clock.tick(FPS), where FPS represents the target frame rate you want your application to run (you can simply fix it to 60 at the start of your program if don't really know what value you want it to be), and the eta variable measures the time elapsed (in milliseconds) since last tick call.

Next, to have something happen, say, every second, just keep a counter:

counter = 1000 # in ms
clock = pygame.Clock()
while True:
  # do what you want
  eta = clock.tick(FPS)
  counter -= eta
  if counter < 0:
    # trigger the event
    counter += 1000
    # don't set it directly like
    # counter = 1000
    # to keep track of margin
jthulhu
  • 7,223
  • 2
  • 16
  • 33