Python Forum
draw not showing all sprites
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
draw not showing all sprites
#1
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.

# 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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pygame, sprites, and rects menator01 12 1,965 Dec-07-2023, 02:37 AM
Last Post: Benixon
  [PyGame] Sprites just randomly appear and dissapear in my pygame.sprite.GoupeSingle trueShadoWrr 2 2,002 Feb-13-2023, 09:34 AM
Last Post: Vadanane
  [PyGame] I found a way to generate sprites without .blit. Is it effecient? xBlackHeartx 19 8,488 Dec-07-2019, 01:06 PM
Last Post: metulburr
  [PyGame] Having 4 players(Sprites) all being able to jump ElijahCastle 5 4,030 May-07-2019, 05:04 PM
Last Post: SheeppOSU
  Sprites and Actor error ajlconsulting 6 9,367 Jan-30-2019, 12:50 AM
Last Post: metulburr
  [PyGame] move randomly sprites reutB 4 8,254 Mar-29-2017, 01:12 PM
Last Post: metulburr
  How can I get rid of the line following the sprites? Houston11 1 3,754 Jan-06-2017, 10:14 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020