dimanche 9 août 2020

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.




Aucun commentaire:

Enregistrer un commentaire