-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
pygame template (advanced).py
101 lines (87 loc) · 2.42 KB
/
pygame template (advanced).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Pygame Template (advanced)
# Use this to start a new Pygame project
# KidsCanCode 2016
import pygame as pg
import random
# define some colors (R, G, B)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
FUCHSIA = (255, 0, 255)
GRAY = (128, 128, 128)
LIME = (0, 128, 0)
MAROON = (128, 0, 0)
NAVYBLUE = (0, 0, 128)
OLIVE = (128, 128, 0)
PURPLE = (128, 0, 128)
RED = (255, 0, 0)
SILVER = (192, 192, 192)
TEAL = (0, 128, 128)
YELLOW = (255, 255, 0)
ORANGE = (255, 128, 0)
CYAN = (0, 255, 255)
# basic constants to set up your game
WIDTH = 360
HEIGHT = 480
FPS = 30
TITLE = "My Game"
BGCOLOR = BLACK
class Game:
# The Game object will initialize the game, run the game loop,
# and display start/end screens
def __init__(self):
# initialize the game and create the window
# initialize pygame
pg.init()
# initialize sound - uncomment if you're using sound
# pygame.mixer.init()
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption(TITLE)
# start the clock
self.clock = pg.time.Clock()
self.load_data()
self.running = True
def new(self):
# initialize all your variables and do all the setup for a new game
self.run()
def load_data(self):
# load all your assets (sounds, images, etc.)
pass
def run(self):
# The Game loop - set self.running to False to end the game
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
def update(self):
# the update part of the game loop
pass
def draw(self):
# draw everything to the screen
self.screen.fill(BGCOLOR)
pg.display.flip()
def events(self):
# catch all events here
for event in pg.event.get():
# this one checks for the window being closed
if event.type == pg.QUIT:
if self.playing:
self.playing = False
self.running = False
# add any other events here (keys, mouse, etc.)
def show_start_screen(self):
# show the start screen
pass
def show_go_screen(self):
# show the game over screen
pass
# create the game object
g = Game()
g.show_start_screen()
while g.running:
g.new()
g.show_go_screen()
pg.quit()