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