I'm making an side scroller arcade game in python where you control a rocket and have to avoid the asteroids coming from right to left. My problem is that my asteroids do not appear on the screen. Help is very much appreciated.
import pygame as pg
import numpy as np
from pygame.locals import *
from random import *
#Make a first "game" which is actually the menu
res = (1000,800)
screen = pg.display.set_mode(res)
pg.display.set_caption('Rocket game')
black = (0,0,0)
white = (255,255,255)
backgroundmenu = pg.image.load("menuscreen.jpg")
pg.init()
running1 = True
while running1:
pg.event.pump()
keys = pg.key.get_pressed()
if keys[pg.K_1]:
running1 = False
running2 = True
elif keys[pg.K_2]:
running1 = False
running2 = False
for event in pg.event.get():
if event.type == pg.QUIT:
running1 = False
screen.blit(backgroundmenu,(0,0))
pg.display.flip()
pg.quit()
pg.init()
#Setup screen and define colors
res = (1000,800)
screen = pg.display.set_mode(res)
pg.display.set_caption('Rocket game')
#pg.image.load("space.jpg")
black = (0,0,0)
white = (255,255,255)
background1 = pg.image.load("space.jpg").convert()
background2 = pg.image.load("space.jpg").convert()
background3 = pg.image.load("space.jpg").convert()
#screen.blit(background1,(0,0))
x_back = 0
screenwidth = 1000
#Initialize variables and clock
vx = 0
vy = 0
x = 200
y = 600
t0 = 0.001*pg.time.get_ticks()
maxdt = 0.5
#Load rocket and asteroids
rocketgif = pg.image.load("rocket.gif")
rocketimg = pg.transform.scale(rocketgif, (100,100))
asteroidgif = pg.image.load("Asteroid.gif")
asteroidimg = pg.transform.scale(asteroidgif, (75,75))
#Load music
pg.mixer.music.load('gamesound.mp3')
pg.mixer.music.set_endevent(pg.constants.USEREVENT)
pg.mixer.music.play()
#clock = pg.time.Clock()
#Generate random asteroids
Nasteroid = 5
x_ast = 999
dx = 5
i = 0
asteroidpos_y= []
while i<Nasteroid:
y = randint(0,800)
asteroidpos_y.append(y)
i = i + 1
#Start game loop
running2 = True
while running2:
t = 0.001*pg.time.get_ticks()
dt = min(t-t0,maxdt)
i = 0
pg.event.pump()
while i<Nasteroid:
x_ast -= dx
y = y
screen.blit(asteroidimg,(x_ast,asteroidpos_y[i-1]))
i = i + 1
for event in pg.event.get():
if event.type == pg.QUIT: #Quit using red cross button
running2 = False
elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE:
running2 = False #Quit using key combination
elif event.type == pg.KEYDOWN: #Make user control rocket movement
#and stop if key is released.
if event.key == pg.K_LEFT:
vx = -8
elif event.key == pg.K_RIGHT:
vx = 8
elif event.key == pg.K_UP:
vy = -8
elif event.key == pg.K_DOWN:
vy = 8
elif event.type == pg.KEYUP:
if event.key == pg.K_LEFT:
vx = 0
elif event.key == pg.K_RIGHT:
vx = 0
elif event.key == pg.K_UP:
vy = 0
elif event.key == pg.K_DOWN:
vy = 0
elif event.type == pg.constants.USEREVENT:
pg.mixer.music.load("gamesound.mp3")
pg.mixer.music.play()
#Make the screen scroll behind the rocket to make it "move" (3x the same background behind eachother)
screen.blit(background1,(x_back,0))
screen.blit(background2,(x_back - screenwidth,0))
screen.blit(background3,(x_back - (2*screenwidth),0))
x_back = x_back - 1
if x_back == screenwidth:
x_back = 0
#Update the position of the rocket. If the rocket hits the edges the game stops.
x = x + vx
y = y + vy
"""if x <= 0 or x >=100:
running2 = False
if y <= 0 or y >=800:
running2 = False"""
pg.event.pump()
screen.blit(rocketimg,(x,y))
pg.display.flip()
pg.quit()