SCUBA24 platformer scaffolds

(back to SCUBA24)

Platformer Scaffold 1

#import libraries
import pygame

#initialise pygame
pygame.init()

#set game window dimensions
SCREEN_WIDTH = #
SCREEN_HEIGHT = #

#create game window
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Jumpy')

#load images 

#game loop
run = True
while run:
    #draw background
    #event handler
    #update display window

pygame.quit()
    

Platformer Scaffold 2

#import libraries
import pygame

#initialise pygame
pygame.init()

#set game window dimensions
SCREEN_WIDTH = #
SCREEN_HEIGHT = #

#create game window
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Jumpy')

#set frame rate varibles 

#set game variables 
White = (255,255,255)
GRAVITY = #

#load images (add the player image!)

#player class 
class Player():
    def __init__(self, x, y):
        self.image = pygame.transform.scale()
	self.width = # think about the pygame functions get_????
	self.height = # think about the pygame functions get_????
        self.rect = pygame.Rect(0, 0, self.width, self.height)
	self.rect.center = (x, y)
	self.vel_y = 0
	self.flip = False

    def move(self):
        #reset the offset variables
	off_set_x = 0
	off_set_y = 0

	#check if a key is beig pressed using
	key = pygame.key.get_pressed()
	if key[pygame.K_a]:
	    # Move x to the right and flip the image 
	if key[pygame.K_d]:
	    # Move x to the right and do not flip the image 

	#gravity
	self.vel_y += GRAVITY
	off_set_y += self.vel_y

	#check player doesn't go off the edge of the screen using either self.rect.width/height or  self.rect.left/right SCREEN_WIDTH = #
	if off_set_x #?????
	    off_set_x = 
	if self.rect.right + off_set_x #?????:
	    off_set_x = 
			
        #check player doesn't go off the edge of the screen using either self.rect.height or  self.rect.bottom and SCREEN_HEIGHT = #
	if self.rect.bottom + off_set_y > SCREEN_HEIGHT:
	    off_set_y = 0
	    self.vel_y = -20

	#update rectangle position
	self.rect.x += #????
	self.rect.y += #????

    def draw(self):
        screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x, self.rect.y))
	pygame.draw.rect(screen, White  , self.rect, 2)


jumpy = Player(# fill in the arguments )

#game loop
run = True
while run:
    clock.tick(FPS)
    jumpy.move()

    #draw background
    screen.blit(bg_image, (0, 0))

    #draw sprites
    jumpy.draw()

    #event handler
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
	    run = False

    #update display window
    pygame.display.update()

pygame.quit()
    

Platformer Scaffold 3

import pygame

class Platform(pygame.sprite.Sprite):
    def __init__(self, x, y, width):
        pygame.sprite.Sprite.__init__(self)
	self.image = # using pygame.transform.scale() place the image 
	# we need a rect object and its x and y coordinates
        # look back at the character object for ideas...
	if #check if platform has gone off the screen
	    self.kill()
    

Platformer Scaffold 4

#import libraries
import pygame

#initialise pygame
pygame.init()

#set game window dimensions
SCREEN_WIDTH = #
SCREEN_HEIGHT = #

#create game window
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Jumpy')

#set frame rate varibles 

#set game variables 
White = (255,255,255)
GRAVITY = #
#
MAX_PLATFORMS = 10

#load images (add the player image!)

#player class 
class Player():
    def __init__(self, x, y):
        self.image = pygame.transform.scale()
	self.width = # think about the pygame functions get_????
	self.height = # think about the pygame functions get_????
        self.rect = pygame.Rect(0, 0, self.width, self.height)
        self.rect.center = (x, y)
        self.vel_y = 0
        self.flip = False

    def move(self):
        #reset the offset variables
	off_set_x = 0
	off_set_y = 0

	#check if a key is beig pressesd using
	key = pygame.key.get_pressed()
        if key[pygame.K_a]:
	    # Move x to the right and flip the image 
	if key[pygame.K_d]:
            # Move x to the left and do not flip the image 

	#gravity
	self.vel_y += GRAVITY
	off_set_y += self.vel_y

	# check player doesn't go off the edge of the screen
        # using either self.rect.width/height or self.rect.left/right
        # SCREEN_WIDTH = #

	if off_set_x #?????
	    off_set_x = 
	if self.rect.right + off_set_x #?????:
	    off_set_x = 
			
        # check player doesn't go off the edge of the screen
        # using either self.rect.height or self.rect.bottom and 
        # SCREEN_HEIGHT = #

	if self.rect.bottom + off_set_y > SCREEN_HEIGHT:
	    off_set_y = 0
	    self.vel_y = -20

	# update rectangle position
	    self.rect.x += #????
	    self.rect.y += #????

	def draw(self):
	    screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x, self.rect.y))
	    pygame.draw.rect(screen, White  , self.rect, 2)

jumpy = Player(# fill in the aruguments )

#create sprite groups
platform_group = pygame.sprite.Group()

#create temporary platforms
for p in range(MAX_PLATFORMS):
    p_w =  # create the width of the platform ( try a random number between 40 and 60)
    p_x = # find the x coordd of the platform 
    p_y = #using p try to find a away to create a platform such that
          #it is 80 to 120 px away from the last one 
    platform = # create an platform Object using Platform()
    platform_group.add(platform)

#game loop
run = True
while run:

	clock.tick(FPS)

	jumpy.move()

	#draw background
	screen.blit(bg_image, (0, 0))

	#draw sprites
	platform_group.draw(screen)
	jumpy.draw()

	#event handler
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			run = False

	#update display window
	pygame.display.update()

pygame.quit()        
    

Platformer Scaffold 5

#import libraries
import pygame
import random

#initialise pygame
pygame.init()

#set game window dimensions
SCREEN_WIDTH = #
SCREEN_HEIGHT = #

#create game window
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Jumpy')

#set frame rate varibles 

#set game variables  (Gobal)
White = (255,255,255)
GRAVITY = #
MAX_PLATFORMS = 10
scroll = # what should we first set the scroll to, since at first the player isnt moving
bg_scroll = # what should we first set the scroll to, since at first the player isnt moving
SCROLL_THRESH = 200

#load images
jumpy_image = pygame.image.load('assets/jump.png')
bg_image = pygame.image.load('assets/bg.png')
platform_image = pygame.image.load('assets/wood.png')

#function for drawing the background
def draw_bg(bg_scroll):
	screen.blit(bg_image, (0, 0 + bg_scroll))
	screen.blit(bg_image, (0, -600 + bg_scroll))

#player class 
class Player():
    def __init__(self, x, y):
        self.image = pygame.transform.scale()
	self.width = # think about the pygame functions get_????
	self.height = # think about the pygame functions get_????
        self.rect = pygame.Rect(0, 0, self.width, self.height)
	self.rect.center = (x, y)
	self.vel_y = 0
	self.flip = False

    def move(self,platform_group):
        #reset the offset variables
	scroll = 0 
	off_set_x = 0
	off_set_y = 0

	#check if a key is beig pressesd using
	key = pygame.key.get_pressed()
	if key[pygame.K_a]:
	    # Move x to the right and flip the image 
	if key[pygame.K_d]:
	    # Move x to the left and do not flip the image 

        #gravity
	self.vel_y += GRAVITY
        off_set_y += self.vel_y

        #check player doesn't go off the edge of the screen using either self.rect.width/height or  self.rect.left/right SCREEN_WIDTH = #
	if off_set_x #?????
	    off_set_x = 
	if self.rect.right + off_set_x #?????:
	    off_set_x = 
			
        #check player doesn't go off the edge of the screen using either self.rect.height or  self.rect.bottom and SCREEN_HEIGHT = #
	if self.rect.bottom + off_set_y > SCREEN_HEIGHT:
	    off_set_y = 0
	    self.vel_y = -20
			
	for platform in platform_group:
	    #collision in the y direction
	    if platform.rect.colliderect(, , , ):
	        #check if above the platform
		if self.rect.bottom < platform.rect.centery:
		    if self.vel_y > 0:
		        self.rect.bottom = platform.rect.top
			off_set_y = 0
			self.vel_y = -20
		#check collision with ground
		if self.rect.bottom + off_set_y > SCREEN_HEIGHT:
		    dy = 0
		    self.vel_y = -20

		#check if the player has bounced to the top of the screen
		if self.rect.top <= SCROLL_THRESH:
		    #if player is jumping
		    if self.vel_y < 0:
		        scroll = -off_set_y

		#update rectangle position
		self.rect.x += #????
		self.rect.y += off_set_y + scroll
		return scroll
        
	def draw(self):
	    screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x, self.rect.y))
	    pygame.draw.rect(screen, White  , self.rect, 2)


jumpy = Player ( # fill in the aruguments )

#create sprite groups
platform_group = pygame.sprite.Group()

#create temporary platforms
for p in range(MAX_PLATFORMS):
	p_w =  # create the widith of the platform ( try a random number between 40 and 60)
	p_x = # find the x crod of the platform 
	p_y = #using p try to find a away to create a platform such that it is 80 to 120 px away from the last one 
	platform = # create an platform Object using Platform()
	platform_group.add(platform)

#game loop
run = True
while run:
    clock.tick(FPS)

    #draw background
	bg_scroll += scroll
	if bg_scroll >= 600:
		bg_scroll = 0
	draw_bg(bg_scroll)
	

    #update platforms
	platform_group.update(scroll)

	jumpy.move()

	#draw background
	screen.blit(bg_image, (0, 0))

	#draw sprites
	platform_group.draw(screen)
	jumpy.draw()
	
    #update platforms
	platform_group.update(scroll)



	#event handler
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			run = False


	#update display window
	pygame.display.update()

pygame.quit()
    

Updated platforms.py

import pygame

class Platform(pygame.sprite.Sprite):
    def __init__(self, x, y, width):
        pygame.sprite.Sprite.__init__(self)
	self.image = # using pygame.transform.scale() place the image 
	# we need a rect objects and its and a x and y postion look back at the character Object to get an idea
	self.width = # think about the pygame functions
	self.height = # think about the pygame functions 
        self.rect = pygame.Rect(0, 0, self.width, self.height)
        self.rect.center = (x, y)
    def update(self):
        #update platform's vertical position
	self.rect.y += scroll
	if #check if platform has gone off the screen
	   self.kill()			
    

Assets


Acknowledgements

This exercise is based on the Jumpy platform tutorial from Coding with Russ.