Keyboard Guide
#put import statements here img_dir = path.join(path.dirname(__file__), 'img') #set WIDTH HEIGHT and FPS here WIDTH = HEIGHT = FPS = #default colors WHITE = (255, 255, 255) #initialize pygame here screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Arcade") #set up clock here #this code loads your picture into the game player_img = pygame.image.load(path.join(img_dir, "INSERT FILENAME HERE")) projectile_img = pygame.image.load(path.join(img_dir, "INSERT FILENAME HERE")) enemy_img = pygame.image.load(path.join(img_dir, "INSERT FILENAME HERE")) background = pygame.image.load(path.join(img_dir, "INSERT FILENAME HERE")) background = pygame.transform.scale(background, (WIDTH, HEIGHT)) bg_rect = background.get_rect() font_name = pygame.font.match_font("arial") #this function puts the text on the screen def draw_text(surf, text, size, x, y): font = pygame.font.Font(font_name, size) text_surface = font.render(text, True, WHITE) #set up text_rect here #position the rect here using midtop #blit the text to the screen here #this is the class that sets up our projectiles class Projectile(pygame.sprite.Sprite): def __init__(self, x, y): pygame.sprite.Sprite.__init__(self) #set projectile size here #set projectile image here #set start position here #set y speed here def update(self): #update projectile location here #check to see if projectile has left the screen class Enemy(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) #set size of enemy here #set enemy image here #set starting position of enemy here #set speed of enemy here def update(self): #update enemy locations here class Player(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) #set player image here and scale it #set player starting position #set starting speed here def update(self): self.speedx = 0 keys = pygame.key.get_pressed() #check to see if right key is pressed #move player to the right #check to see if left key is pressed #move player to the left #prevent player from leaving the screen #set up the launch function def launch(self): #this line spawns a projectile projectile = Projectile(self.rect.centerx, self.rect.top) #add projectile to sprite list and projectile list #create all_sprites list #create player #add player to all_sprites list #set up enemies list #spawn enemies on screen #create projectiles list #set starting score running = True while running: #start clock here for event in pygame.event.get(): #check for quit event #check for space click all_sprites.update() hits = pygame.sprite.groupcollide(enemies, projectiles, True, True) #set up for loop to check for collisions with projectiles hits = pygame.sprite.spritecollide(player, enemies, False) if hits: running = False screen.blit(background, bg_rect) all_sprites.draw(screen) draw_text(screen, str(score), 18, WIDTH / 2, 10) pygame.display.flip() pygame.quit()
Start by importing the Pygame and random package and giving the game the ability to use images from your computer.ย os stands forย operating systemย (like Windows or Mac).
import pygame
import random
from os import path
Let’s begin by setting the properties of our game screen.
WIDTH = 480
HEIGHT = 600
FPS = 60
Don’t forget toย initialize the pygame package!
pygame.init()
You can change the title of your game as a new string here (remember strings need to be inside quotation marks):
pygame.display.set_caption("INSERT NAME HERE")
Now we set up the game clock.
clock = pygame.time.Clock()
We need to set up, position, and update our text rectangle like we did for the Clicker Game.
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
Great!