Using Sprites

From TRCCompSci - AQA Computer Science
Revision as of 15:31, 10 March 2018 by Admin (talk | contribs) (Created page with "Sprites have many built in features in pygame, such as collision detection. ==Simple Sprite== <syntaxhighlight lang=python> #!/usr/bin/python import pygame from pygame.local...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Sprites have many built in features in pygame, such as collision detection.

Simple Sprite

#!/usr/bin/python

import pygame
from pygame.locals import KEYDOWN

width  = 320
height = 240
size   = [width, height]
pygame.init()
screen = pygame.display.set_mode(size)
background = pygame.Surface(screen.get_size())

b = pygame.sprite.Sprite() # create sprite
b.image = pygame.image.load("ball.png").convert() # load ball image
b.rect = b.image.get_rect() # use image extent values
b.rect.topleft = [0, 0] # put the ball in the top left corner
screen.blit(b.image, b.rect)


pygame.display.update()
while pygame.event.poll().type != KEYDOWN:
    pygame.time.delay(100)