Sprites not displaying properly [duplicate] - python

I'm building a pong game trying to get better at programming but Im having trouble moving the ball. When the move_right method is called the ellipse stretches to the right instead of moving to the right. I've tried putting the ball variable in the init method but that just makes it not move at all even though the variables should be changing on account of the move_right method. I have also tried setting the x and y positions as parameters in the Ball class,but that just stretches it also.
I don't understand why when I run the following code the ball I'm trying to move stretches to the right instead of moves to the right. Can someone explain why this is happening? I have tried everything I can think of but i can't get it to do what I want.
import pygame,sys
import random
class Ball:
def __init__(self):
self.size = 30
self.color = light_grey
self.x_pos = width/2 -15
self.y_pos = height/2 -15
self.speed = 1
#self.ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
def draw_ball(self):
ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
pygame.draw.ellipse(screen,self.color,ball)
def move_right(self):
self.x_pos += self.speed
class Player:
def __init__(self,x_pos,y_pos,width,height):
self.x_pos = x_pos
self.y_pos = y_pos
self.width = width
self.height = height
self.color = light_grey
def draw_player(self):
player = pygame.Rect(self.x_pos,self.y_pos,self.width,self.height)
pygame.draw.rect(screen,self.color,player)
class Main:
def __init__(self):
self.ball=Ball()
self.player=Player(width-20,height/2 -70,10,140)
self.opponent= Player(10,height/2-70,10,140)
def draw_elements(self):
self.ball.draw_ball()
self.player.draw_player()
self.opponent.draw_player()
def move_ball(self):
self.ball.move_right()
pygame.init()
size = 30
clock = pygame.time.Clock()
pygame.display.set_caption("Pong")
width = 1000
height = 600
screen = pygame.display.set_mode((width,height))
bg_color = pygame.Color('grey12')
light_grey = (200,200,200)
main = Main()
#ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
#player = pygame.Rect(width-20,height/2 -70,10,140)
#opponent = pygame.Rect(10,height/2-70,10,140)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
#pygame.draw.rect(screen,light_grey,player)
#pygame.draw.rect(screen,light_grey,opponent)
#pygame.draw.ellipse(screen,light_grey,ball)
main.draw_elements()
main.move_ball()
main.ball.x_pos += main.ball.speed
pygame.display.flip()
clock.tick(60)

You have to clear the display in every frame with pygame.Surface.fill:
while True:
# [...]
screen.fill(0) # <---
main.draw_elements()
main.move_ball()
main.ball.x_pos += main.ball.speed
pygame.display.flip()
# [...]
Everything that is drawn is drawn on the target surface. The entire scene is redraw in each frame. Therefore the display needs to be cleared at the begin of every frame in the application loop. The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()

Related

Images layering over old image in Python Pygame [duplicate]

I'm building a pong game trying to get better at programming but Im having trouble moving the ball. When the move_right method is called the ellipse stretches to the right instead of moving to the right. I've tried putting the ball variable in the init method but that just makes it not move at all even though the variables should be changing on account of the move_right method. I have also tried setting the x and y positions as parameters in the Ball class,but that just stretches it also.
I don't understand why when I run the following code the ball I'm trying to move stretches to the right instead of moves to the right. Can someone explain why this is happening? I have tried everything I can think of but i can't get it to do what I want.
import pygame,sys
import random
class Ball:
def __init__(self):
self.size = 30
self.color = light_grey
self.x_pos = width/2 -15
self.y_pos = height/2 -15
self.speed = 1
#self.ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
def draw_ball(self):
ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
pygame.draw.ellipse(screen,self.color,ball)
def move_right(self):
self.x_pos += self.speed
class Player:
def __init__(self,x_pos,y_pos,width,height):
self.x_pos = x_pos
self.y_pos = y_pos
self.width = width
self.height = height
self.color = light_grey
def draw_player(self):
player = pygame.Rect(self.x_pos,self.y_pos,self.width,self.height)
pygame.draw.rect(screen,self.color,player)
class Main:
def __init__(self):
self.ball=Ball()
self.player=Player(width-20,height/2 -70,10,140)
self.opponent= Player(10,height/2-70,10,140)
def draw_elements(self):
self.ball.draw_ball()
self.player.draw_player()
self.opponent.draw_player()
def move_ball(self):
self.ball.move_right()
pygame.init()
size = 30
clock = pygame.time.Clock()
pygame.display.set_caption("Pong")
width = 1000
height = 600
screen = pygame.display.set_mode((width,height))
bg_color = pygame.Color('grey12')
light_grey = (200,200,200)
main = Main()
#ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
#player = pygame.Rect(width-20,height/2 -70,10,140)
#opponent = pygame.Rect(10,height/2-70,10,140)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
#pygame.draw.rect(screen,light_grey,player)
#pygame.draw.rect(screen,light_grey,opponent)
#pygame.draw.ellipse(screen,light_grey,ball)
main.draw_elements()
main.move_ball()
main.ball.x_pos += main.ball.speed
pygame.display.flip()
clock.tick(60)
You have to clear the display in every frame with pygame.Surface.fill:
while True:
# [...]
screen.fill(0) # <---
main.draw_elements()
main.move_ball()
main.ball.x_pos += main.ball.speed
pygame.display.flip()
# [...]
Everything that is drawn is drawn on the target surface. The entire scene is redraw in each frame. Therefore the display needs to be cleared at the begin of every frame in the application loop. The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()

Python Crash Course 2nd Edition - Creating an Alien [duplicate]

I'm building a pong game trying to get better at programming but Im having trouble moving the ball. When the move_right method is called the ellipse stretches to the right instead of moving to the right. I've tried putting the ball variable in the init method but that just makes it not move at all even though the variables should be changing on account of the move_right method. I have also tried setting the x and y positions as parameters in the Ball class,but that just stretches it also.
I don't understand why when I run the following code the ball I'm trying to move stretches to the right instead of moves to the right. Can someone explain why this is happening? I have tried everything I can think of but i can't get it to do what I want.
import pygame,sys
import random
class Ball:
def __init__(self):
self.size = 30
self.color = light_grey
self.x_pos = width/2 -15
self.y_pos = height/2 -15
self.speed = 1
#self.ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
def draw_ball(self):
ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
pygame.draw.ellipse(screen,self.color,ball)
def move_right(self):
self.x_pos += self.speed
class Player:
def __init__(self,x_pos,y_pos,width,height):
self.x_pos = x_pos
self.y_pos = y_pos
self.width = width
self.height = height
self.color = light_grey
def draw_player(self):
player = pygame.Rect(self.x_pos,self.y_pos,self.width,self.height)
pygame.draw.rect(screen,self.color,player)
class Main:
def __init__(self):
self.ball=Ball()
self.player=Player(width-20,height/2 -70,10,140)
self.opponent= Player(10,height/2-70,10,140)
def draw_elements(self):
self.ball.draw_ball()
self.player.draw_player()
self.opponent.draw_player()
def move_ball(self):
self.ball.move_right()
pygame.init()
size = 30
clock = pygame.time.Clock()
pygame.display.set_caption("Pong")
width = 1000
height = 600
screen = pygame.display.set_mode((width,height))
bg_color = pygame.Color('grey12')
light_grey = (200,200,200)
main = Main()
#ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
#player = pygame.Rect(width-20,height/2 -70,10,140)
#opponent = pygame.Rect(10,height/2-70,10,140)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
#pygame.draw.rect(screen,light_grey,player)
#pygame.draw.rect(screen,light_grey,opponent)
#pygame.draw.ellipse(screen,light_grey,ball)
main.draw_elements()
main.move_ball()
main.ball.x_pos += main.ball.speed
pygame.display.flip()
clock.tick(60)
You have to clear the display in every frame with pygame.Surface.fill:
while True:
# [...]
screen.fill(0) # <---
main.draw_elements()
main.move_ball()
main.ball.x_pos += main.ball.speed
pygame.display.flip()
# [...]
Everything that is drawn is drawn on the target surface. The entire scene is redraw in each frame. Therefore the display needs to be cleared at the begin of every frame in the application loop. The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()

Pygame returns an error when you click a sprite with another sprite that isn't listed

I'm just making a title screen for a future project, and when I run it the "PLAY" and "QUIT" buttons work fine enough but when you click the surrounding background or the title image, the game freezes up and returns the error, "AttributeError: 'list' object has no attribute 'sprites'" I tried adding a blank audio file seperate from the "if play == 0:" in case it was just confused when it had nothing to do, but the problem stayed.
import pygame
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
YELLOW = (255,255,0)
score = 0
play = 0
class Sprite(pygame.sprite.Sprite):
def __init__(self,filename):
super().__init__()
self.image = pygame.image.load(filename).convert()
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
class Player(Sprite):
def update(self):
""" Method called when updating a sprite. """
# Get the current mouse position. This returns the position
# as a list of two numbers.
pos = pygame.mouse.get_pos()
# Now see how the mouse position is different from the current
# player position. (How far did we move?)
diff_x = self.rect.x - pos[0]
diff_y = self.rect.y - pos[1]
# Loop through each block that we are carrying and adjust
# it by the amount we moved.
# Now wet the player object to the mouse location
self.rect.x = pos[0]
self.rect.y = pos[1]
class Block(pygame.sprite.Sprite):
def __init__(self, color, width, height):
# Call the parent class (Sprite) constructor
super().__init__()
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# image.
# Update the position of this object by setting the values
# of rect.x and rect.y
self.rect = self.image.get_rect()
# Initialize Pygame
pygame.init()
# Set the height and width of the screen
screen_width = 750
screen_height = 500
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption("LARP")
# This is a list of 'sprites.' Each block in the program is
# added to this list. The list is managed by a class called 'Group.'
block_list = pygame.sprite.Group()
quit_block = pygame.sprite.Group()
start_block = pygame.sprite.Group()
title_image = pygame.image.load("title.png").convert_alpha()
quit_image = pygame.image.load("quit.png").convert_alpha()
logo_image = pygame.image.load("logo.png").convert_alpha()
player_image = pygame.image.load("player.png").convert_alpha()
background_image = pygame.image.load("title_screen.png").convert_alpha()
bomp = pygame.mixer.Sound("bump.wav")
# This is a list of every sprite.
# All blocks and the player block as well.
player_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
#for i in range(50):
#This represents a block
#block = Block(BLACK, 20, 15)
#Set a random location for the block
#block.rect.x = random.randrange(screen_width)
#block.rect.y = random.randrange(screen_height)
#Add the block to the list of objects
#block_list.add(block)
#all_sprites_list.add(block)
title = Sprite("title.png")
title.rect.x = 340
title.rect.y = 230
all_sprites_list.add(title)
start_block.add(title)
quitg = Sprite("quit.png")
quitg.rect.x = 340
quitg.rect.y = 350
all_sprites_list.add(quitg)
quit_block.add(quitg)
logo = Sprite("logo.png")
logo.rect.y = 90
logo.rect.x = 310
all_sprites_list.add(logo)
block_list.add(logo)
player = Player("player.png")
player_list.add(player)
all_sprites_list.add(player)
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Hide the mouse cursor
pygame.mouse.set_visible(False)
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
# When the mouse button is pressed, see if we are in contact with
# other sprites:
if play == 0:
start_block = pygame.sprite.spritecollide(player, start_block, True)
if len(start_block) >= 1:
play += 1
quit_block = pygame.sprite.spritecollide(player, quit_block, True)
if len(quit_block) >= 1:
done = True
#print(score)
#blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
#if len(blocks_hit_list) >= 1:
#score +=1
# Set the list of blocks we are in contact with as the list of
# blocks being carried.
#player.carry_block_list = blocks_hit_list
elif event.type == pygame.MOUSEBUTTONUP:
# When we let up on the mouse, set the list of blocks we are
# carrying as empty.
player.carry_block_list = []
all_sprites_list.update()
# Clear the screen
screen.fill(WHITE)
# Draw all the spites
if play == 0:
screen.blit(background_image, [0,0])
all_sprites_list.draw(screen)
screen.blit(player_image, [player.rect.x, player.rect.y])
# Limit to 60 frames per second
clock.tick(60)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
pygame.quit()
start_block and quit_block are pygame.sprite.Group(). However, pygame.sprite.spritecollide(). Therefore an instruction like:
start_block = pygame.sprite.spritecollide(player, start_block, True)
makes no sense because the group start_block is overwritten with the list start_block
Change the name of the variable the gets the return value. e.g.:
start_block_hit = pygame.sprite.spritecollide(player, start_block, False)
Note, you don't need to store the return values at all. The doKill argument must be False, otherwise your button will be destroyed when you click it:
if pygame.sprite.spritecollide(player, start_block, False):
play += 1
if pygame.sprite.spritecollide(player, quit_block, False):
done = True

Sprites won't show up

Cannot figure out why my code won't show sprites when I run it. The code is from a how-to book I found at my library, I have reread the code multiple times and haven't found anything different than what the book says to do. This is my first time coding on python and I haven't seen anything to help me solve my problem yet. This is all the code I've written for the game so far.
import pygame
from pygame import *
from random import randint
pygame.init()
WINDOW_WIDTH = 1100
WINDOW_HEIGHT = 600
WINDOW_RES = (WINDOW_WIDTH, WINDOW_HEIGHT)
WIDTH = 100
HEIGHT = 100
WHITE = (255, 255, 255)
SPAWN_RATE = 360
GAME_WINDOW = display.set_mode(WINDOW_RES)
display.set_caption('Attack of the vampire Pizzas!')
pizza_img = image.load('vampire.png')
pizza_surf = Surface.convert_alpha(pizza_img)
VAMPIRE_PIZZA = transform.scale(pizza_surf, (WIDTH, HEIGHT))
background_img = image.load('restaurant.jpg')
background_surf = Surface.convert_alpha(background_img)
BACKGROUND = transform.scale(background_surf, WINDOW_RES)
class VampireSprite(sprite.Sprite):
def __init__(self):
super().__init__()
self.speed = 2
self.lane = randint(0, 4)
all_vampires.add(self)
self.image = VAMPIRE_PIZZA.copy()
y = 50 + self.lane * 100
self.rect = self.image.get_rect(center = (1100, y))
def update(self, game_window):
game_window.blit(self.image, (self.rect.x, self.rect.y))
all_vampires = sprite.Group()
tile_color = WHITE
for row in range(6):
for column in range(11):
draw.rect(BACKGROUND, tile_color, (WIDTH * column, HEIGHT * row, WIDTH, HEIGHT), 1)
GAME_WINDOW.blit(BACKGROUND, (0,0))
----------------------------------------------
#Start Main Game Loop
game_running = True
#Game Loop
while game_running:
for event in pygame.event.get():
#Exit loop on quit
if event.type == QUIT:
game_running = False
if randint(1, SPAWN_RATE) == 1:
VampireSprite()
for vampire in all_vampires:
vampire.update(GAME_WINDOW)
display.update()
pygame.quit()
The Code seems to have all the correct components, except that it's a bit mixed up.
Generally when you make a sprite, it has the __init__() function - obviously for initialisation, and additionally an update() function. The update() function usually does not draw the object to the display/surface, but adjusts the position (i.e.: the sprite.rect) or changes the "look" (image) used for the sprite, for drawing later.
Sprites are usually grouped into an aptly-named Sprite Group. Once sprites are in a group, a single call to group.update() will call the update() function on every sprite contained. It's really convenient, and works well.
So tweaking your Vampire Pizza Sprite:
class VampireSprite(sprite.Sprite):
def __init__(self):
super().__init__()
self.speed = 2
self.lane = randint(0, 4)
self.image = VAMPIRE_PIZZA.copy()
y = 50 + self.lane * 100
self.rect = self.image.get_rect(center = (1100, y))
def update(self):
# TODO - do Vampire Pizzas Move?
# if so, update the self.rect
pass
And that's all that's needed. I removed the painting code, this will be handled by a sprite group.
So later on:
# Make a group of vampire sprites
all_vampires = sprite.Group()
all_vampires.add( VampireSprite() ) # Start with a single vampire
# Game Loop
game_running = True
while game_running:
# handle events
for event in pygame.event.get():
#Exit loop on quit
if event.type == QUIT:
game_running = False
# spawn some vampires, maybe
if randint(1, SPAWN_RATE) == 1:
all_vampires.add( VampireSprite() ) # add to the group
# Update/move all vampire sprites
all_vampires.update() # updates every sprite in group
# repaint the screen
GAME_WINDOW.blit(BACKGROUND, (0,0))
# draw some columns(?)
tile_color = WHITE
for row in range(6):
for column in range(11):
draw.rect(GAME_WINDOW, tile_color, (WIDTH * column, HEIGHT * row, WIDTH, HEIGHT), 1)
all_vampires.draw() # paints every sprite in group
display.update()
pygame.quit()
There's two calls from the all_vampires sprite group - all_vampires.update() and all_vampires.draw()? With just these two calls, all sprites in the group are moved (adjusted/whatever), and painted to the screen.

Enemies won't display

I'm trying to create a top down shooter, but my enemies won't spawn in. I'm trying to have the enemy move in a diagonal pattern but also be able to bounce off of the wall kind of like a top down bouncing ball. I'm also trying to have a new enemy spawn every 10 seconds, but when I run the program nothing shows up, there is no error either. Can someone please help.
import pygame
pygame.init()
import random
import time
inPlay = True
width=900
height=700
screen = pygame.display.set_mode((width, height))
#enemy
enemyFrequency=10
enemyPause=enemyFrequency
killEnemy=True
#------------------------------#
# classes #
#------------------------------#
######################################################
class Enemy(pygame.sprite.Group):
def __init__(self,ballX,ballY,ballSpeed,picture2=None):
pygame.sprite.Group.__init__(self)
self.ballX=ballX
self.ballY=ballY
self.ballSpeed=ballSpeed
self.image2=pygame.image.load(picture2)
def move(self):
for enemys in self:
self.ballX+=self.ballSpeed
self.ballY+=self.ballSpeed
def decay(self):
for enemys in self:
if enemys.y > height:
self.remove(enemys)
#------------------------------#
# functions #
#------------------------------#
def redraw_game_window():
screen.fill((30, 30, 30))
enemys.draw(screen)
pygame.display.update()
#------------------------------#
# main program #
#------------------------------#
RB=width-player.rect.width
CEILING = 3*player.rect.height # for ship movement
FLOOR = height - player.rect.height #
#enemy
enemies=pygame.sprite.Group()
enemySpeed=3
eX=random.randint(0,RB)
eY=random.randint(-height,0)
enemys=Enemy (eX,eY,enemySpeed,'asteroid.png')
t0 = pygame.time.clock()
dt = 0
enemyCount = 0
clock = pygame.time.Clock()
while inPlay:
redraw_game_window()
for event in pygame.event.get():
if event.type == pygame.QUIT:
inPlay=False
#enemies spawning
if dt < nSeconds:
t1 = time.process_time()
dt = t1 - t0
enemies.add(enemys)
else:
enemyInstance = Enemy()
enemyCount += 1
t0 = time.clock()
dt = 0
enemys.move()
enemys.decay()
clock.tick(30)
pygame.quit()
I hacked your code to the point where it seemed to be doing what the code describes, and what matches your question. There were a lot of things missing in this code, maybe you cut them out before posting to SO to make the code shorter.
The crux of the problem is the handling of the enemy sprites. I don't know if it was by design, but the original code was defining what seemed like a sprite, but based on a sprite group.
So I modified the code based on the idea that enemies should be a global sprite group, and went from there "fixing" things. There was no Player class defined, I added one. I could not follow the timing code, it was using pygame.time.clock() (which is an object) as a time value. It seemed like this code was keeping a delta, and counting time to respawn a new enemy, I had to re-write this bit to get it to work - my apologies.
The other comment I want to make - sprites can define an update() function. This function should handle the position changes and appearance of the sprite. PyGame sprite groups will handle the calling of this function automatically (if it's defined) in the sprite group update() meta-call. This gives the program a clean and simple way to handle sprite animation.
import pygame
pygame.init()
import random
import time
inPlay = True
width=900
height=700
screen = pygame.display.set_mode((width, height))
#enemy
enemies = pygame.sprite.Group() # Holds all enemy sprites
enemyFrequency = 1000 # milliseconds between enemy spawn
killEnemy = True
enemyCount = 0
#------------------------------#
# classes #
#------------------------------#
######################################################
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('player.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.center = (100,100) # TODO - position properly
def update(self):
# TODO
pass
class Enemy(pygame.sprite.Sprite):
def __init__(self, ballX, ballY, ballSpeed, picture2=None):
pygame.sprite.Sprite.__init__(self)
self.ballSpeed = ballSpeed
self.image = pygame.image.load(picture2).convert_alpha()
self.rect = self.image.get_rect()
self.rect.center = ( ballX, ballY )
def update(self):
global enemies # group of all enemy sprites (to which this sprite belongs)
global height # window height
self.rect.x += self.ballSpeed
self.rect.y += self.ballSpeed
# decay the enemy
if ( self.rect.y > height ):
enemies.remove(self) # went off screen, delete it
#------------------------------#
# functions #
#------------------------------#
def redraw_game_window():
screen.fill((30, 30, 30))
enemies.draw(screen)
pygame.display.update()
#------------------------------#
# main program #
#------------------------------#
player = Player()
RB=width-player.rect.width
CEILING = 3*player.rect.height # for ship movement
FLOOR = height - player.rect.height #
# start with 3 enemies
for i in range( 3 ):
enemySpeed = 3
eX=random.randint(0,RB)
eY=random.randint(-height,0)
enemies.add( Enemy(eX,eY,enemySpeed,'asteroid.png') )
clock = pygame.time.Clock()
last_enemy_spawn_time = 0
while inPlay:
time_now = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
inPlay=False
# is it time to spawn a new enemy?
if ( time_now - last_enemy_spawn_time > enemyFrequency ):
last_enemy_spawn_time = time_now # reset timer
#enemies spawning
eX=random.randint(0,RB)
eY=random.randint(-height,0)
enemies.add( Enemy(eX,eY,enemySpeed,'asteroid.png') )
enemyCount += 1
enemies.update() # call the update() of every sprite
redraw_game_window()
#enemies.decay() -- MOVED INTO Enemy.update()
clock.tick(30)
pygame.quit()

Categories