Hamza Khan
Hamza Khan

Reputation: 67

Spawning multiple objects off screen and making them move down the screen

I'm making a game where the player tries to avoid cubes moving down the screen. I'm struggling to create the cubes outside of the screen and having them randomly fall down the screen while the player tries to avoid. I also want this to happen until the player hits a cube where the game ends (I think I can do the collision part the program). Here's my code so far:

import pygame
import random

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 = 124
player_y = 750
player_x_change = 0

def player(player_x, player_y):
    screen.blit(player_icon, (player_x, player_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

    player(player_x, player_y)

    pygame.display.update()

This is everything including player movement that I have so far. Thanks for your time.

Upvotes: 0

Views: 605

Answers (1)

Mike67
Mike67

Reputation: 11342

For the enemy cubes, create an array of X/Y coordinates, one for each cube. Set Y to above the screen (negative) and X to a random number smaller than the screen width. In the main loop, just increment the Y coordinate of each cube before drawing. If the cube drops past the bottom of the screen, reset the Y value to above the screen again. Depending on the initial Y values, the cubes can come in waves. I also added the tick call at the bottom of the loop to slow down the game.

Here is the updated code:

import pygame
import random
from random import randint

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 = 124
player_y = 750
player_x_change = 0

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

# create cubes array [[x,y],[x,y],[x,y],....]
cubes = [[
     randint(1,260),   # X coordinate
     randint(-500,-20)]   # Y coordinate, -Y is above screen  (top of screen is zero)
     for x in range(20)]  # 20 cubes

# above syntax is a shortcut for the following loop
#    cubes = []
#    for x in range(20):
#       cubes.append([randint(1,260), randint(-500,-20)])

running = True
while running:

    screen.fill((255, 255, 255))  # clear screen
    
    # render cubes
    for cb in cubes:
       cb[1] += 2  # cube moves down 2 pixels
       screen.blit(icon,cb)  # draw cube
       if cb[1] > 800:  # if cube passed bottom of screen
          cb[1] = -20  # move to above screen
          cb[0] = randint(1,260)  # random X position

    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

    player(player_x, player_y)

    pygame.display.update()
    pygame.time.Clock().tick(60)  # slow loop - 60 FPS

Upvotes: 1

Related Questions