Difference between revisions of "Using Sprites"
(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...") |
(→Simple Sprite) |
||
Line 1: | Line 1: | ||
Sprites have many built in features in pygame, such as collision detection. | Sprites have many built in features in pygame, such as collision detection. | ||
− | + | #Import statements are to enable the code to use the functions from the library | |
− | |||
− | # | ||
− | |||
import pygame | import pygame | ||
− | + | import sys | |
+ | import os | ||
− | + | #initialize pygame & window | |
− | + | os.environ["SDL_VIDEO_CENTERED"] = "1" | |
− | |||
pygame.init() | pygame.init() | ||
− | + | SCREENWIDTH = 500 | |
− | + | SCREENHEIGHT = 500 | |
+ | SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT] | ||
+ | SCREEN = pygame.display.set_mode(SCREENSIZE) | ||
+ | COLOR=[0,0,0] | ||
b = pygame.sprite.Sprite() # create sprite | b = pygame.sprite.Sprite() # create sprite | ||
− | b.image = pygame.image.load(" | + | b.image = pygame.image.load("hero.png").convert() # load hero image |
− | b.rect = b.image.get_rect() # use image extent values | + | b.rect = b.image.get_rect()# use image extent values |
− | b.rect.topleft = [0, 0] # put the | + | b.rect.topleft = [0, 0] # put the hero in the top left corner |
− | |||
+ | #game loop | ||
+ | while True: | ||
+ | SCREEN.fill(COLOR) | ||
+ | for events in pygame.event.get(): #get all pygame events | ||
+ | if events.type == pygame.QUIT: #if event is quit then shutdown window and program | ||
+ | pygame.quit() | ||
+ | sys.exit() | ||
− | + | SCREEN.blit(b.image, b.rect) | |
− | + | pygame.display.update() | |
− | pygame. | ||
− |
Revision as of 16:01, 10 March 2018
Sprites have many built in features in pygame, such as collision detection.
- Import statements are to enable the code to use the functions from the library
import pygame import sys import os
- initialize pygame & window
os.environ["SDL_VIDEO_CENTERED"] = "1" pygame.init() SCREENWIDTH = 500 SCREENHEIGHT = 500 SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT] SCREEN = pygame.display.set_mode(SCREENSIZE) COLOR=[0,0,0]
b = pygame.sprite.Sprite() # create sprite b.image = pygame.image.load("hero.png").convert() # load hero image b.rect = b.image.get_rect()# use image extent values b.rect.topleft = [0, 0] # put the hero in the top left corner
- game loop
while True:
SCREEN.fill(COLOR) for events in pygame.event.get(): #get all pygame events if events.type == pygame.QUIT: #if event is quit then shutdown window and program pygame.quit() sys.exit()
SCREEN.blit(b.image, b.rect) pygame.display.update()