-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame_mode.py
181 lines (154 loc) · 7.19 KB
/
game_mode.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import random
import sys
import pygame
from PIL import Image
import game_object
import player_object
class Game:
"""Generic Game, initialize important attributes and contains mainloop
"""
def __init__(self, map_size, screen_size, debug):
self.map_size = map_size
self.screen_size = screen_size
self.debug_font = pygame.font.SysFont("monospace", 15)
self.debug = debug
# self.delta_time = 0
# Setting up the screen
self.screen = pygame.display.set_mode(self.screen_size, pygame.DOUBLEBUF)
# Game clock setting
self.clock = pygame.time.Clock()
# Groups
self.ships = pygame.sprite.Group()
self.bullets = pygame.sprite.Group()
self.edge = pygame.sprite.Group()
self.allies = pygame.sprite.Group()
self.environment = pygame.sprite.Group()
self.starry_sky = pygame.sprite.Group()
self.targets = pygame.sprite.Group()
self.all = pygame.sprite.Group()
# default shines, theoretic performance boost
self.standard_shine = pygame.Surface([5, 2], pygame.SRCALPHA).convert_alpha()
self.standard_shine.fill((155, 155, 0))
self.white_shine = pygame.Surface([2, 2], pygame.SRCALPHA).convert_alpha()
self.white_shine.fill((255, 255, 255))
self.lightred_shine = pygame.Surface([2, 2], pygame.SRCALPHA).convert_alpha()
self.lightred_shine.fill((255, 150, 150))
class TestGame(Game):
"""Test game with some sprites and basic settings"""
def __init__(self, map_size, screen_size, debug):
super().__init__(map_size, screen_size, debug)
self.asteroids = pygame.sprite.Group()
# Finalize screen caption
pygame.display.set_caption("Shooter")
# User
self.user = player_object.Ship(self, pygame.image.load('Images/ship.png').convert_alpha(),
[self.map_size[0] / 2, self.map_size[1] / 2 + 100],
0.7, 0.4, 10, 10.0, 12.0, 8, need_max_rect=False,
camera_mode='scrolling', controlled=True)
self.camera_x = 100
self.camera_y = 200
self.ships.add(self.user)
# self.targets.add(self.user)
self.allies.add(self.user)
self.all.add(self.user)
# Edge objects [[position], [dimension]]
boundaries = [[[0, 0], [map_size[0], 20]],
[[0, map_size[1]], [map_size[0], 20]],
[[0, 0], [20, map_size[1]]],
[[map_size[0], 0], [20, map_size[1] + 20]]]
for obj in boundaries:
item = game_object.Edge(self, obj[0], obj[1], (255, 0, 0))
self.edge.add(item)
self.environment.add(item)
self.all.add(item)
# Starry sky, randomly generated
img = Image.new('RGB', map_size)
pixel_map = img.load()
for x in range(1000):
pixel_map[random.randint(0, self.map_size[0] - 1),
random.randint(0, self.map_size[1] - 1)] = (255, 255, 255, 0)
stars = game_object.Background(self, pygame.image.frombuffer(img.tobytes("raw", "RGB"), map_size, "RGB").convert())
self.starry_sky.add(stars)
img.close()
# Base
self.base = game_object.Surface(self, pygame.image.load("Images/base.png").convert_alpha(),
[self.map_size[0] / 2, self.map_size[1] / 2],
False, 0, spin=0.5, life=200)
self.environment.add(self.base)
self.allies.add(self.base)
# Asteroids
self.asteroid_image1 = pygame.image.load("Images/asteroid.png").convert_alpha()
for x in range(20):
position = [random.randint(1, self.map_size[0]),
random.randint(1, self.map_size[1])]
while self.base.distance_from(position) < 700:
position = [random.randint(1, self.map_size[0]),
random.randint(1, self.map_size[1])]
test = game_object.Surface(self, self.asteroid_image1,
position, False, 8,
spin=random.uniform(-2, 2),
speed=[random.uniform(-4, 4), random.uniform(-4, 4)], life=5)
self.environment.add(test)
self.asteroids.add(test)
self.targets.add(test)
self.all.add(test)
def mainloop(self):
"""Main game loop
"""
done = False
counter = 0
# check for exit
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# delta time, game is frame rate indipendent
self.delta_time = 30 / self.clock.tick_busy_loop(60)
# asteroids spawn
counter += 1 / self.delta_time
if counter / 60 > 1: # TODO optimize position
counter = 0
position = [random.randint(1, self.map_size[0]),
random.randint(1, self.map_size[1])]
while self.user.distance_from(position) < 450 or self.base.distance_from(position) < 800:
position = [random.randint(1, self.map_size[0]),
random.randint(1, self.map_size[1])]
test = game_object.Surface(self, self.asteroid_image1,
position,
False, 8, spin=random.uniform(-2, 2),
speed=[random.uniform(-3, 3), random.uniform(-3, 3)], life=5)
self.environment.add(test)
self.asteroids.add(test)
self.targets.add(test)
self.all.add(test)
# keys events
keys = pygame.key.get_pressed()
# asteroids collision
for ally, asteroids in pygame.sprite.groupcollide(self.allies, self.asteroids, False, False).items():
for obj in asteroids:
ally.hit(obj._damage)
obj.explode(color=(255, 0, 0))
# pause the game
if not keys[pygame.K_a]:
# Refresh screen and update sprites
self.screen.fill((0, 0, 0))
self.starry_sky.update()
self.bullets.update()
self.ships.update(keys)
self.environment.update()
display_label = self.debug_font.render(" Sprites in game:" +
str(len(self.all.sprites())) +
", fps: " + str(self.clock.get_fps()),
1, (255, 255, 0))
GAME.screen.blit(display_label, (0, 0))
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
# Initialize pygame
pygame.init()
if len(sys.argv) > 1 and sys.argv[1] == '-d':
debug = True
else:
debug = False
GAME = TestGame((2500, 2500), (1300, 800), debug) # mapsize, screensize, debug_enable
GAME.mainloop()