0

I'm trying to create a simple game where u have to dodge enemies(asteroids), with pygame, but now I'm having trouble spawning them and I don't know if i should use lists or other things, or the enemy class(asteroidClass) is enough. The interval between spawning them is pretty simple I just don't how to deal with the spawn part (dealing with this for 3 days).

import pygame
import random

pygame.init()
#images 
background = pygame.image.load('#path')
asteroid = pygame.image.load('#path')

display = pygame.display.set_mode((300,500))
FPS = 50

display.blit(background,(0,0))
#player everything is fine

#asteroid
class asteroidClass:
    def __init__(self,asteroidX,asteroidY,asteroidVel):
        self.x = asteroidX
        self.y = asteroidY
        self.vel = asteroidVel
    def asteroid_advancing(self):
        self.y += self.vel
        display.blit(asteroid, (self.x, self.y))

def update():
    pygame.display.update()
    pygame.time.Clock().tick(FPS)

#variables
asteroidX = random.randint(0,250)
asteroidY, asteroidVel = 0, 2
asteroidOutClass = asteroidClass(asteroidX,asteroidY,asteroidVel)



#main loop
run = True
while run:
    #should spawn multiple I don't know with what logic
    #spawning in the same x
    #saw in web they're using lists, maybe i should too?
    #when i print i it just do 0123456 like it should, then restart to 0123456, is it wrong? kinda 100%
    for i in range(7):
        asteroidOutClass.asteroid_advancing() #everytime it's called should spawn and asteroid in random x?

    update()
    display.blit(background, (0, 0))

1 Answers1

0

A few things:

  • Create the asteroids before the main loop
  • Use the init method to randomly set asteroid position and speed
  • Be sure to call an event method in the main loop to prevent freezing

Here's the updated code. I removed the background calls and used a circle for the asteroid.

import pygame
import random

pygame.init()
#images pygame.load() not in here

display = pygame.display.set_mode((300,500))
FPS = 50

#display.blit(background,(0,0))
#player everything is fine

#asteroid
class asteroidClass:
    def __init__(self):
        self.x = random.randrange(0, 300)
        self.y = 0 # always top
        self.velx = random.randrange(1,15)/3
        self.vely = random.randrange(1,15)/3
    def asteroid_advancing(self):
        self.y += self.vely
        self.x += self.velx
        # wrap around screen
        if self.x < 0: self.x=300
        if self.x > 300: self.x=0
        if self.y < 0: self.y=500
        if self.y > 500: self.y=0
        pygame.draw.circle(display, (200, 0, 0), (int(self.x), int(self.y)), 20) # draw asteroid

def update():
    pygame.display.update()
    pygame.time.Clock().tick(FPS)

#variables
# create list of 5 asteroids
roidlist = [asteroidClass() for x in range(7)]

#main loop
run = True
while run:
    for event in pygame.event.get(): # required for OS events
      if event.type == pygame.QUIT:
         pygame.quit()
    display.fill((0,0,0))
    #should spawn multiple I don't know with what logic
    #spawning in the same x
    #saw in web they're using lists, maybe i should too?
    #when i print i it just do 0123456 like it should, then restart to 0123456, is it wrong? kinda 100%
    for r in roidlist:
        r.asteroid_advancing() #everytime it's called should spawn and asteroid in random x?

    update()
#    display.blit(background, (0, 0))
Mike67
  • 11,175
  • 2
  • 7
  • 15