When I run the program below which is supposed to create 4 bouncing program, the game will display anywhere between 0-3 of the 4 rectangles I have added. A print statement in the update function for the function shows each sprite is being updated.
I figured it out, a bug in my program.
# import pygame library so we can use it! import pygame import random import mygame_colors # run initialization code on the library pygame.init() # setup display dimensions display_width = 1200 display_height = 600 FPS = 30 gameSurface = pygame.display.set_mode((display_width, display_height)) pygame.display.set_caption('Window Caption!') colors = (mygame_colors.RED,mygame_colors.GREEN,mygame_colors.WHITE,mygame_colors.BLUE) # game code that needs to run only once class Enemy(pygame.sprite.Sprite): def __init__(self, number): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((10*(number+1), 10)) self.number = number # self.color = (random.randint(100,255),random.randint(100,255),random.randint(100,255)) self.color = colors[x] self.image.fill(self.color) self.rect = self.image.get_rect() self.rect.center = (random.randint(0,display_width), random.randint(0,display_width)) self.x_speed = random.randint(-30,30) if self.x_speed == 0: self.x_speed += 1 self.y_speed = random.randint(-30,30) if self.y_speed == 0: self.y_speed += 1 def update(self,*args): super().update(self,*args) self.rect.x += self.x_speed self.rect.y += self.y_speed x,y = self.rect.center if x > display_width or x < 0: self.x_speed *= -1 if y > display_height or y < 0: self.y_speed *= -1 print ("%s %s: %s,%s,%s"%(self.number,self.color,self.rect.x,self.rect.y,self.image)) # create game clock clock = pygame.time.Clock() # create a sprite group to keep track of sprites all_sprites = pygame.sprite.Group() for x in range(4): player = Enemy(x) all_sprites.add(player) # fill entire screen with color gameSurface.fill(mygame_colors.BLACK) # main game loop running = True # when running is True game loop will run while running == True: # get input events and respond to them # if event is .QUIT for event in pygame.event.get(): if event.type == pygame.QUIT: running = False clock.tick(FPS) # game code that repeats every frame # clear display (redraw background) gameSurface.fill(mygame_colors.BLACK) # update all_sprites.update() # draw all sprites all_sprites.draw(gameSurface) # pygame.draw.rect(gameSurface, r_color, [rectX, rectY, 25, 25]) # pygame.draw.circle(gameSurface,r_color,(int(rectX),int(rectY)),25) # gameSurface.blit(image1, [rectX, rectY]) # update and redraw entire screen pygame.display.flip() # pygame.display.update()Here is the code for mygame_colors.py:
BLACK = (0, 0, 0) WHITE = (255, 255, 255) GRAY = (235, 235, 235) GREEN = (0, 255, 0) RED = (255, 0, 0) BLUE = (65, 231, 255) PURPLE = (255, 0, 255) YELLOW = (255, 255, 0)
I figured it out, a bug in my program.