• Advertisement
Sign in to follow this  

stubborn code

This topic is 1771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

today i decided to do a simple shooter game so i could practice scrolling backgrounds but for some reason code that i use on a regular basis isn't working! my plan was to have the player shoot at/avoid  meteorites unfortunately my code to spawn multiple meteorites is only spawning a single one.iv spent hours trying to fix it.  i event opens a new page and typed up the code for that event but nothing seems to work. iv used similar code for my frogger clone and similar games. can someone please help? im sure its a noob mistake

Share this post


Link to post
Share on other sites
Advertisement
 
 
# IMPORT------------------------------------------
 
import pygame, sys, random
from pygame.locals import*
 
pygame.init()
 
 
# DISPLAY----------------------------------------
screen = pygame.display.set_mode([500,700])
 
WHITE = (255,255,255)
BLUE  = (  0,  0,255)
 
 
 
#ENTITIES------------------------------------------
class backg(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('stars.jpg')
        self.image = self.image.convert()
        self.rect = self.image.get_rect()
        self.dy = 5
        self.reset()
 
    def update(self):
        self.rect.bottom += self.dy
        if self.rect.top >= 0:
            self.reset()
 
    def reset(self):
        self.rect.bottom = screen.get_height()
 
#--------------------------------------------------
#this is the metorite
rock_speed = 3
class Rock(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('roid01.png')
        self.image.set_colorkey(WHITE)
        
        self.rect = self.image.get_rect()
 
    def reset_pos(self):
        self.rect.x = random.randrange(0,700)
        self.rect.y = 0
 
    def update(self):
        self.rect.y += rock_speed
 
        if self.rect.y > 600:
            self.reset_pos()
        
 
    
# ACTION------------------------------------------    
      # A - ASSIGN VALUES TO KEY VARIABLES----------------
 
Stars = backg()
star_list = []
#------------------------------------------
for i in range(100):
    x = random.randrange(0,500)
    y = random.randrange(-100,400)
    star_list.append([x,y])
    
#------------------------------------------------
#this is the metorite
rock_number = 5
for i in range(rock_number):
    srock = Rock()
    srock.rect.x = random.randrange(10,475)
    srock.rect.y = -100
 
    space_rocks = pygame.sprite.Group(srock)
 
 
#--------------------------------------------------------------
 
all_sprites_list = pygame.sprite.Group(Stars)
 
 
clock = pygame.time.Clock()
 
 
     # L - SET UP MAIN GAME LOOP-------------------------------
done = False
     
while done == False:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        
 
    # draw background scrolling-------------------------------
    all_sprites_list.draw(screen)
    
    
    # draw scrolling stars
    for i in range(len(star_list)):
        pygame.draw.circle(screen,WHITE,star_list,2)
        star_list[1] += 15
 
        if star_list[1] > 700:
            y = random.randrange(-50,300)
            star_list[1] = y
 
            x = random.randrange(0,500)
            star_list[0] = x
       
            
 
      # T - TIMER TO SET FRAME RATE------------------------
    clock.tick(40)
 
      # E - EVENT HANDLEING
 
    space_rocks.draw(screen)
    
 
      # R - REFRESH DISPLAY---------------------------------
    space_rocks.update()
    all_sprites_list.update()
 
    pygame.display.flip()
    
pygame.quit()

Share this post


Link to post
Share on other sites
ok i kinda figured out the problem. i decided to add the meteorites to the all_sprites_list< ill use the space_rocks group for collision instead.
its spawning more rocks but when its set to spawn 10 it only spawn like 5 >_<
but when i set rock_number to rock number = 5 * 2 and tell it to spawn the number in rock_number it will then spawn 5 rocks or what ever number i set the multiplied number by. And collision isn't working for the meteorites when I shoot them usually one will disapear and the others aren't affected Edited by MiniKong

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement