Difference between revisions of "Using Sprites"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(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)
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
==Simple Sprite==
 
==Simple Sprite==
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
#!/usr/bin/python
+
#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()
 +
</syntaxhighlight>
 +
 +
==Example with movement==
 +
<syntaxhighlight lang=python>
 +
#Import statements are to enable the code to use the functions from the library
 
import pygame
 
import pygame
from pygame.locals import KEYDOWN
+
import sys
 +
import os
  
width  = 320
+
#initialize pygame & window
height = 240
+
os.environ["SDL_VIDEO_CENTERED"] = "1"
size  = [width, height]
 
 
pygame.init()
 
pygame.init()
screen = pygame.display.set_mode(size)
+
SCREENWIDTH = 500
background = pygame.Surface(screen.get_size())
+
SCREENHEIGHT = 500
 +
SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT]
 +
SCREEN = pygame.display.set_mode(SCREENSIZE)
 +
COLOR=[0,0,0]
 +
 
 +
pos = [0,0] # position of sprite on the screen
  
 
b = pygame.sprite.Sprite() # create sprite
 
b = pygame.sprite.Sprite() # create sprite
b.image = pygame.image.load("ball.png").convert() # load ball image
+
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 ball in the top left corner
+
b.rect.topleft = pos # put the hero at position
screen.blit(b.image, b.rect)
+
 
 +
#game loop
 +
while True:
 +
    SCREEN.fill(COLOR)
 +
    for events in pygame.event.get(): #get all pygame events
 +
        if events.type == pygame.KEYDOWN:
 +
            if events.key == pygame.K_LEFT:
 +
                pos[0]=pos[0]-10
 +
            elif events.key == pygame.K_RIGHT:
 +
              pos[0]=pos[0]+10
 +
            elif events.key == pygame.K_UP:
 +
                pos[1]=pos[1]-10
 +
            elif events.key == pygame.K_DOWN:
 +
                pos[1]=pos[1]+10
 +
            b.rect.topleft = pos
  
 +
        if events.type == pygame.QUIT: #if event is quit then shutdown window and program
 +
            pygame.quit()
 +
            sys.exit()
  
pygame.display.update()
+
    SCREEN.blit(b.image, b.rect)
while pygame.event.poll().type != KEYDOWN:
+
     pygame.display.update()
     pygame.time.delay(100)
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 17:03, 10 March 2018

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

Simple Sprite

#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()

Example with movement

#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]

pos = [0,0] # position of sprite on the screen

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 = pos # put the hero at position

#game loop
while True:
    SCREEN.fill(COLOR)
    for events in pygame.event.get(): #get all pygame events
        if events.type == pygame.KEYDOWN:
            if events.key == pygame.K_LEFT:
                pos[0]=pos[0]-10
            elif events.key == pygame.K_RIGHT:
               pos[0]=pos[0]+10
            elif events.key == pygame.K_UP:
                pos[1]=pos[1]-10
            elif events.key == pygame.K_DOWN:
                pos[1]=pos[1]+10
            b.rect.topleft = pos

        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()