• 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
# IMPORT------------------------------------------
import pygame, sys, random
from pygame.locals import*
# DISPLAY----------------------------------------
screen = pygame.display.set_mode([500,700])
WHITE = (255,255,255)
BLUE  = (  0,  0,255)
class backg(pygame.sprite.Sprite):
    def __init__(self):
        self.image = pygame.image.load('stars.jpg')
        self.image = self.image.convert()
        self.rect = self.image.get_rect()
        self.dy = 5
    def update(self):
        self.rect.bottom += self.dy
        if self.rect.top >= 0:
    def reset(self):
        self.rect.bottom = screen.get_height()
#this is the metorite
rock_speed = 3
class Rock(pygame.sprite.Sprite):
    def __init__(self):
        self.image = pygame.image.load('roid01.png')
        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:
# 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)
#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-------------------------------
    # draw scrolling stars
    for i in range(len(star_list)):
        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------------------------
      # R - REFRESH DISPLAY---------------------------------

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