Search code examples
pythonpygameclock

PYGAME - create clock and do action at intervals


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

Solution

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