Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sprite not rendering
#1
I'm new to pygame. I've read a few examples on sprites, but I've missed some small, key point. This code raises no errors, but the intended sprite will not render. Instead I just see the blank, white background. What I should see is a green square with a '7' in the center. This block should be moving to the right.

import sys
import pygame

pygame.init()

RED   = (255,   0,   0)
GREEN = (  0, 255,   0)
BLUE  = (  0,   0, 255)
WHITE = (255, 255, 255)

main_surface = pygame.display.set_mode((500,500), 0, 32)
clock = pygame.time.Clock()
fps = 30
SYS_FONT = pygame.font.SysFont(None, 95)

class Block(pygame.sprite.Sprite):
    def __init__(self, color, text):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((80, 80))
        self.rect = self.image.get_rect()
        self.image.fill(color)
        self.text_surf = SYS_FONT.render(text, False, WHITE)
        self.text_rect = self.text_surf.get_rect(center=self.rect.center)
        self.image.blit(self.text_surf, self.text_rect)

    def update(self):
        self.rect.x += 5

sprites = pygame.sprite.Group()
block_7 = Block(GREEN, '7')
sprites.add(block_7)

sprites.draw(main_surface)

while True:
    main_surface.fill(WHITE)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()
    sprites.update()
    clock.tick(fps)
Reply
#2
here is the modified section of your code. You need to add the draw method to the main game loop and put pygame.display.update() after that.
#sprites.draw(main_surface)
 
while True:
    main_surface.fill(WHITE)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    
    sprites.update()
    sprites.draw(main_surface)
    pygame.display.update()
    clock.tick(fps)
Cheers for using classes and pygame rects. Wink
Recommended Tutorials:
Reply
#3
(Oct-03-2019, 11:18 AM)metulburr Wrote: here is the modified section of your code. You need to add the draw method to the main game loop and put pygame.display.update() after that.
#sprites.draw(main_surface)
 
while True:
    main_surface.fill(WHITE)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    
    sprites.update()
    sprites.draw(main_surface)
    pygame.display.update()
    clock.tick(fps)
Cheers for using classes and pygame rects. Wink

Thank you. I knew it was a simple mistake.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Sprite image.get_rect() moves sprite to 0, 0 michael1789 2 4,599 Dec-13-2019, 08:37 PM
Last Post: michael1789
  Need help making a sprite GalaxyCoyote 4 3,238 Aug-11-2019, 09:12 PM
Last Post: metulburr
  moving a sprite pfaber11 3 2,587 May-15-2019, 12:52 PM
Last Post: pfaber11
  [PyGame] Need Help With Sprite ghost0fkarma 2 3,282 Jan-09-2018, 02:14 PM
Last Post: ghost0fkarma

Forum Jump:

User Panel Messages

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