0

I have to create a game for university. Here is how the game should be: On the left side you have a plane that you can control (up and down). This works very good so far. On the right side obstacles are spawning and they are flying towards the plane so you have to dodge the obstacles. With my code only one obstacle is spawned in the beginning. It flies towards the plane like it should and also the collision detection works. My problem is, that I just don´t know how to spawn multiple obstacles. I have thought about implementing a timer that counts up and every x seconds a new obstacle is spawning but if I write something like "time.sleep" into the while-loop the game doesn´t even start. I am very thankful for every idea and tip as I will get a grade on this project. So thanks a lot in advance! If there are any more things that you need to know please leave a comment. Best regards, Timo :) enter image description here

import pygame
import random
import time
import math

# Initialize pygame
pygame.init()

# Create window (width, height)
screen = pygame.display.set_mode(((800, 600)))
ScreenHeight = screen.get_height()
ScreenWidth = screen.get_width()

# Background picture
background = pygame.image.load("background.jpg")

# Title and Icon
pygame.display.set_caption("F22-Raptor Simulator")
icon = pygame.image.load("jet1.png")
pygame.display.set_icon(icon)

# Player
playerImg = pygame.image.load("player1.png")
playerX = 10
playerY = 270
playerY_change = 0


def player(x, y):
    screen.blit(playerImg, (x, y))


# Obstacle
obstacleImg = pygame.image.load("rock.png")
obstacleX = random.randint(600, 700)
obstacleY = random.randint(0, ScreenHeight - 64)
obstacleX_change = -0.3


def obstacle(x, y):
    screen.blit(obstacleImg, (x, y))


# Collosion detection (Player with obstacle)
def collisionDetection(playerX, playerY, obstacleX, obstacleY):
    distance = math.sqrt((math.pow(playerX - obstacleX, 2)) + ((math.pow(playerY - obstacleY, 2))))
    if distance < 64:
        return True
    else:
        return False


# Keep window running (Infinite-Loop)
running = True

# While-Loop (Everything that takes place during the game is inside here
while running:

    # Background-Color of window
    screen.fill((192, 192, 192))

    # Insert Background
    screen.blit(background, (0, 0))

    # End game / close window
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # Movement up/down
        if event.type == pygame.KEYDOWN:
            # Movement up
            if event.key == pygame.K_w:
                playerY_change = -0.15
            # Movement down
            if event.key == pygame.K_s:
                playerY_change = 0.15
        # If no key pressed -> no movement
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w or event.key == pygame.K_s:
                playerY_change = 0

    # Display movement of player
    playerY += playerY_change

    # Movement-boundaries Player
    if playerY <= 0:
        playerY = 0
    if playerY >= 536:
        playerY = 536

    # Display player
    player(playerX, playerY)

    # Display movement of obstacle
    obstacleX += obstacleX_change

    # Display obstacle

    obstacle(obstacleX, obstacleY)

    # Collision Detection
    collision = collisionDetection(playerX, playerY, obstacleX, obstacleY)
    if collision:
        playerX = 10
        playerY = 270
        player(playerX, playerY)

    # Update after each iteration of the while-loop
    pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Doritito
  • 77
  • 7
  • 1
    I would recommend you to read this post : https://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds – sos Mar 16 '21 at 13:49
  • @sos Thanks for your comment. But if I implement a timer in my while-loop and execute the obstacle() function every second for example. It just keeps updating the current obstacle that is spawned in the beginning. Do I need to create a class to create multiple objects flying towards the ship? – Doritito Mar 16 '21 at 13:57
  • 1
    I think after every n seconds, you will need to create a new obstacle (as a new object) so that you can handle the collision and the display of each obstacle easily. you can keep track of the obstacles in a list and delete those that have passed by the player without collision. if you are not familiar with classes then you can use [namedtuple] (https://docs.python.org/3/library/collections.html#collections.namedtuple) otherwise a class Obstacle where you can define more than just attributes would be a solution to your problem – sos Mar 16 '21 at 14:11
  • 1
    Maybe you find [this](https://stackoverflow.com/questions/56288406/how-to-make-a-wave-timer-in-pygame/56290843#56290843) answer helpful. Of course there are 2 million different ways to implement something like this, but you don't have to bother with threads etc. – sloth Mar 16 '21 at 14:25
  • Thanks @sos I will have a look into it later :) – Doritito Mar 16 '21 at 15:16
  • Thanks @sloth I will also have a look into that :) – Doritito Mar 16 '21 at 15:16

0 Answers0