0% found this document useful (0 votes)
30 views

Message

This document defines functions and classes to build a basic space shooter game using Pygame. It imports Pygame and other modules, defines game settings like screen size, and helper functions for drawing text, waiting for input, and drawing multiple images. It includes a Player class and Game class, with the Game class initializing Pygame, loading assets, and controlling the main game loop which shows the start screen, plays levels by updating and drawing sprites each frame, and checks for collisions and input.

Uploaded by

dinisfcorreia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Message

This document defines functions and classes to build a basic space shooter game using Pygame. It imports Pygame and other modules, defines game settings like screen size, and helper functions for drawing text, waiting for input, and drawing multiple images. It includes a Player class and Game class, with the Game class initializing Pygame, loading assets, and controlling the main game loop which shows the start screen, plays levels by updating and drawing sprites each frame, and checks for collisions and input.

Uploaded by

dinisfcorreia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import pygame

import random
import os

# Game Settings
DISPLAY_W = 480
DISPLAY_H = 600
FPS = 60

BLACK = pygame.Color(0, 0, 0)
WHITE = pygame.Color(255, 255, 255)
BLUE = pygame.Color(0, 255, 255)

# Suggestion, change if you want


IMG_DIR = os.path.join(os.path.dirname(__file__), 'assets', 'img') # for images
SND_DIR = os.path.join(os.path.dirname(__file__), 'assets', 'snd') # for sounds
OTHER_DIR = os.path.join(os.path.dirname(__file__), 'assets', 'other') # for other
stuff

# game assets loaded with pygame, to be filled by Game


ASSETS = {}

# TODO: define game constants


# Example:
# PLAYER_SPEED = 400

########################
# HELPER FUNCTIONS #
########################

def drawText(surface, text, font_name, size, x, y, color=WHITE):


"""
Draws text on surface at (x, y) using the given
font_name (pathname to font), font size and color.
The color argument is optional, the default is white.

Example:
fontName = pygame.font.match_font('arial')
drawText(self.display, 'Hello', fontName, 18, (DISPLAY_W/2, 10))
"""
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.center = (x, y)
surface.blit(text_surface, text_rect)

def waitKey(key=pygame.K_RETURN):
"""
Waits for the given key to be pressed (KEYUP)
or the window to be closed (QUIT).
Returns False if QUIT event detected and True oherwise.
The key argument is optional, the default is the ENTER key.

Example:
running = waitKey()
"""
while True:
for event in pygame.event.get():

if event.type == pygame.QUIT:
return False
if event.type == pygame.KEYUP and event.key == key:
return True

def drawMultImg(surface, img, n, start, spacing):


"""
Draws img n times to surface, the 1st one is centered
at start and the rest are placed in increments of spacing.
start and spacing can be a tuple, list or Vector2.

Example:
drawMultImg(self.display, ASSETS['test'], 2, (20,20), (35,0))
"""
for i in range(n):
rect = img.get_rect()
rect.center = (start[0] + i * spacing[0], start[1] + i * spacing[1])
surface.blit(img, rect)

########################
# SPRITE CLASSES #
########################

class Player(pygame.sprite.Sprite):

def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 50))
self.image.fill((BLUE))
self.rect = self.image.get_rect()
self.rect.center = (DISPLAY_W / 2, DISPLAY_H / 2)

def __init__(self):
pygame.sprite.Sprite.__init__(self)

# variables used by Group draws


self.image = pygame.Surface((50, 50)) # TODO: this is a placeholder, put
your player image
self.rect = self.image.get_rect()

# TODO: use rect to place player on screen

# TODO: define position variable (center of player, should match with rect)

# TODO: define radius variable for collisions

def update(self, dt):


# TODO: handle input, check pressed keys for movement,
# use input to define a direction vector

# TODO: determine speed vector using the directions


# TODO: add spped vector to player position

# TODO: check and keep player inside window limits


# correct the position variable

# TODO: update rect using the position variable


pass

########################
# GAME CLASS #
########################

class Game:
"""
Game Class, responsible for initialing pygame modules,
loading game assets and controlling game states and game loop.
"""

def __init__(self):
pygame.init()
self.display = pygame.display.set_mode((DISPLAY_W, DISPLAY_H))
pygame.display.set_caption('Space Shooter')

self.loadAssets()
self.running = True

def loadAssets(self):
# TODO: load game assets: images, sounds, fonts...
# Example:
# ASSETS['player_img'] = pygame.image.load(os.path.join(IMG_DIR,
'player.png')).convert()
# ASSETS['player_img'].set_colorkey(BLACK)
# ASSETS['font_name'] = pygame.font.match_font('arial')
pass

def run(self):
while self.running:
self.showStartScreen()
if self.running:
self.playLevel()

def showStartScreen(self):
# TODO: draw start screen and wait user input
pass

def playLevel(self): # game loop


# SETUP LEVEL

clock = pygame.time.Clock()

allSprites = pygame.sprite.Group()
# TODO: create other groups if needed

player = Player()
allSprites.add(player)
# TODO: create other sprites/objects if needed

playing = True
# START LEVEL
while playing:
# dt: seconds passed since last frame
dt = clock.tick(FPS) / 1000

# check events
for event in pygame.event.get():
# close window event
if event.type == pygame.QUIT:
self.running = False
playing = False

# update all entities (calls update() methods)


allSprites.update(dt)

# TODO: handle enemy->player collisions

# TODO: spawn enemies

self.display.fill(BLACK) # TODO: change to a background image


allSprites.draw(self.display) # draws all sprites

pygame.display.update()

def quit(self):
pygame.quit()

if __name__ == "__main__":
g = Game()
g.run()
g.quit()

You might also like