Skip to content
Permalink
01e6fbdd63
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
147 lines (127 sloc) 4.91 KB
import pygame
import random
import math
from pygame import mixer
from settings import *
from sprites import *
import os.path
class Game:
def __init__(self):
# initialize game window, etc
pygame.init()
pygame.mixer.init()
self.screen = pygame.display.set_mode(((width, height)))
# Title and Icon
pygame.display.set_caption(title)
icon = pygame.image.load('img/icon.png')
pygame.display.set_icon(icon)
self.font_name = pygame.font.match_font(basic_font)
self.clock = pygame.time.Clock()
self.running = True
self.load_data()
def load_data(self):
self.dir = os.path.dirname(__file__)
img_dir = os.path.join(self.dir, 'img')
self.spritesheet1 = SpriteSheet(os.path.join(img_dir, spritesheet1))
self.spritesheet2 = SpriteSheet(os.path.join(img_dir, spritesheet2))
def new(self):
# starts a new game not a new program
self.score = 0
self.all_sprites = pygame.sprite.Group()
self.platforms = pygame.sprite.Group()
self.player = Player(self)
self.all_sprites.add(self.player)
for plat in platform_list:
p = Platform(self, *plat)
self.all_sprites.add(p)
self.platforms.add(p)
self.run()
def run(self):
# Game loop
self.playing = True
while self.playing:
self.clock.tick(fps)
self.events()
self.update()
self.draw()
def update(self):
# Game loop - Update
self.all_sprites.update()
# check if player hits platform - only if falling
if self.player.vel.y > 0:
hits = pygame.sprite.spritecollide(self.player, self.platforms, False)
if hits:
self.player.pos.y = hits[0].rect.top
self.player.vel.y = 0
# if player reaches 3/4 of width of the screen
if self.player.rect.x >= (width * 0.6):
self.player.pos.x -= abs(self.player.vel.x)
for plat in self.platforms:
plat.rect.x -= self.player.vel.x
# if player reaches goes left screen moves left 1/10
if self.player.rect.x <= (width * 0.2):
self.player.pos.x += abs(self.player.vel.x)
for plat in self.platforms:
plat.rect.x -= self.player.vel.x
# Die, by falling of the screen
if self.player.rect.bottom > height + 20:
for sprite in self.all_sprites:
sprite.rect.y -= max(self.player.vel.y, 10)
self.playing = False
def events(self):
# Game loop - events
for event in pygame.event.get():
if event.type == pygame.QUIT:
if self.playing:
self.playing = False
self.running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.player.jump()
def draw(self):
# Game loop - draw/render
self.screen.fill(BG_colour)
self.all_sprites.draw(self.screen)
self.draw_text("Score: " + str(self.score), 26, (255, 255, 255), 40, 15)
# after everything is drawn "flip the board"
pygame.display.flip()
def show_start_screen(self):
# game start screen
self.screen.fill(BG_colour)
self.draw_text(title, 48, (255, 255, 255), width / 2, int(height / 4) + 50)
self.draw_text("Arrows to move, Z to shoot", 22, (255, 255, 255), width / 2, int(height / 2))
self.draw_text("Press a key to play", 22, (255, 255, 255), width / 2, int(height / 2) + 40)
pygame.display.flip()
self.wait_for_key()
def show_go_screen(self):
# game over screen
if not self.running:
return
self.screen.fill(BG_colour)
self.draw_text("GAME OVER", 48, (255, 255, 255), width / 2, int(height / 4) + 50)
self.draw_text("Final Score: " + str(self.score), 22, (255, 255, 255), width / 2, int(height / 2))
self.draw_text("Press a key to play again", 22, (255, 255, 255), width / 2, int(height / 2) + 40)
pygame.display.flip()
self.wait_for_key()
def wait_for_key(self):
waiting = True
while waiting:
self.clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
waiting = False
self.running = False
if event.type == pygame.KEYUP:
waiting = False
def draw_text(self, text, size, color, x, y):
font = pygame.font.Font(self.font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.center = (x, y)
self.screen.blit(text_surface, text_rect)
g = Game()
g.show_start_screen()
while g.running:
g.new()
g.show_go_screen() # go stands for Game Over
pygame.quit()