This repository was archived by the owner on Mar 20, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathGameInit.py
More file actions
158 lines (108 loc) · 4.02 KB
/
Copy pathGameInit.py
File metadata and controls
158 lines (108 loc) · 4.02 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# GameInit.py - Initializes PyGame and our game, and it contains the game loop.
# Created by Josh Kennedy on 17 May 2014
#
# Pop a Dots
# Copyright 2014 Chad Jensen and Josh Kennedy
# Copyright 2015-2016 Sirkles LLC
import os
import sys
import platform
import random
import time
import pygame
import AssetCache
import Colors
import CirclesGame
import HelperAPI
# Our global loop condition, so we can exit the game from the menu.
gameDone = False
# Initializes PyGame and its subsystems.
def initialize():
# Print the OS details.
print("Detected OS: %s %s - %s\n" % (platform.system(), platform.release(), platform.version()))
# Print the Python details.
print("Detected Python Version: " + platform.python_implementation() + ' ' + platform.python_version())
print("Using Python installed at: " + sys.executable + '\n')
print("Starting up...", end='')
# Set up environment variables for SDL (PyGame's subsystem).
# Centers the screen.
os.environ["SDL_VIDEO_CENTERED"] = '1'
# Initialize PyGame TTF.
pygame.font.init()
# Initialize PyGame Mixer.
pygame.mixer.pre_init(44100, -16, 1, 512) # I don't know what these values mean, but they work best.
# Initialize PyGame.
pygame.init()
# Initialize the window to 720p.
screen = pygame.display.set_mode(AssetCache.screenResolution, vsync=1)
pygame.display.set_caption("Pop a Dots", "Pop a Dots")
# Set icon.
pygame.display.set_icon(pygame.image.load(os.path.dirname(os.path.realpath(__file__)) + "/Resources/Icon_Full.png"))
# Clear the screen to white, and update it.
screen.fill(Colors.White.get_tuple())
pygame.display.flip()
# Load and cache the assets.
AssetCache.start_cache(os.path.dirname(os.path.realpath(__file__)) + "/Resources/")
# aaaand we're done!
print(" done!")
# Print out graphics driver details, just so we know.
print("\nDetected Graphics Driver: " + pygame.display.get_driver() + '\n')
# Kickstarts the game.
def start():
global gameDone
print("Initializing... ", end='')
fps = 60 # Our FPS, obviously.
fps_clock = pygame.time.Clock() # The clock that's going to keep track of the current FPS.
# Initialize and seed the pseudo-random number generator.
random.seed()
# Create an instance of the Circle Game.
circle_game = CirclesGame.CirclesGame()
print(" done!\n")
last_frame = time.time()
# Set the initial value to gameDone
gameDone = False
# game_title = "Pop a Dots"
# The game loop.
while not gameDone:
# Calculate the fps, and delta time.
fps_clock.tick_busy_loop(fps)
current_frame = time.time()
delta_time = current_frame - last_frame
last_frame = current_frame
# Clear the screen.
pygame.display.get_surface().fill(Colors.White.get_tuple())
# Update Game
circle_game.update(delta_time)
# Draw Game
circle_game.draw(delta_time)
# Update the window title.
# pygame.display.set_caption("{} - {:6.3f} FPS".format(game_title, fpsClock.get_fps()))
# Update the screen.
pygame.display.flip()
# Handle input.
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameDone = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_F12:
HelperAPI.takeScreenshot()
circle_game.handleInput(event)
print("Exiting game...", end='')
# Stop all audio channels.
pygame.mixer.stop()
# Delete cache from memory.
AssetCache.unload_cache()
# Uninitialize PyGame Mixer.
pygame.mixer.quit()
# Uninitialize PyGame TTF.
pygame.font.quit()
# Uninitialize PyGame.
pygame.quit()
print(" done!")
print("Bye!\n")
def draw_fps_text(font, position, text, color, screen):
text_surface = font.render(text, True, color.get_tuple())
screen.blit(text_surface, (position.x, position.y))
def exit_game():
global gameDone
gameDone = True