Reputation: 11
Traceback (most recent call last): File "***/Pcprojects/pygameKCC/kcc-shmup.py", line 49, in all_sprites.update()
I'm following a tutorial that has working code (for him), and for my life I can't get this sprite to render. Can anyone tell me what I'm doing wrong? This is my first time with sprites other than a basic bouncy ball screen saver type thing. Thanks!
import pygame
import random
import sys
import math
#global constants
WIDTH = 360
HEIGHT = 480
FPS = 30
#define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
#YELLOW = (X, X, X)
class Player(pygame.sprite.Sprite):
#sprite for the player.
def __init__(self):
pygame.sprite.Sprite.__init__(self)
#required image and collision rectangle
self.image = pygame.Surface((50, 50))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, HEIGHT / 2)
#itialize pygame
#itialize music mixer
pygame.init()
pygame.mixer.init()
#create screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()
#create a group for all sprites to belong to
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#updated:
all_sprites.update()
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
Upvotes: 1
Views: 96
Reputation: 142759
You have to first clear screen using fill()
and later draw sprites - draw()
. And after that you can send buffer on monitor - flip()
#Render/Draw
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()
BTW: to center element on screen
you can use
screen_rect = screen.get_rect()
and later
self.rect.center = screen_rect.center
The same way you can center other things - ie. text on button.
Upvotes: 1