-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
136 lines (107 loc) · 4.42 KB
/
Game.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
import pygame
import pymunk
from configparser import ConfigParser
from GameObjects import Spaceship, Decoration, Asteroid, Planet
import pymunk.pygame_util
from MainGroup import *
import Controller as controller
import random as rand
import os
from math import degrees
from config import *
path = r""
class Game():
def __init__(self, debug=False):
pygame.init()
pygame.font.init()
self.debug = debug
self.running = True
self.__dict__ |= GAME.__dict__
self.window = WINDOW.__dict__
self.colors = COLORS.__dict__
self.window_icon = pygame.image.load(path + self.window["icon"])
self.dt = self.pymunk_physics_calculating_speed_step / self.max_fps
# pygame
self.clock = pygame.time.Clock()
pygame.display.set_caption(self.window["caption"])
pygame.display.set_icon(self.window_icon)
self.screen = pygame.display.set_mode(
(self.window["width"], self.window["height"]), pygame.RESIZABLE)
# pymunk
self.space = pymunk.Space()
self.space.gravity = (
self.default_gravity_x, self.default_gravity_y)
self.draw_options = pymunk.pygame_util.DrawOptions(self.screen)
# pygame
# Characteristics text
self.characteristics_font = pygame.font.SysFont("calibri", 30)
# "Loading" text
self.loading_font = pygame.font.SysFont('calibri', 50)
self.loading_text_surface = self.loading_font.render(
"Loading...", True, (0, 255, 191))
self.screen.blit(self.loading_text_surface, (50, 50))
pygame.display.update()
# Decorations
self.decorations = [Decoration.StaticDecoration(
path+"assets/Decorations/static/"+rand.choice(
os.listdir(path+"assets/Decorations/static")),
(rand.randint(0, self.area_size_width),
rand.randint(0, self.area_size_height)),
0)
for _ in range(self.num_decorations)]
# Player
self.spaceship = Spaceship.Spaceship(
self.space, (500, 300),
path+"assets/Spaceships/Default/default.json")
# Asteroids
self.asteroids = [Asteroid.Asteroid(
self.space,
(rand.randint(0, self.area_size_width),
rand.randint(0, self.area_size_height)),
path + "assets/Asteroids/asteroid.png",
(asteroid_size := rand.randint(3, 30), asteroid_size),
1/900 * asteroid_size**2,
0.7,
0.7,
asteroid_size/2)
for _ in range(self.num_asteroids)]
# Planets
self.planet = Planet.Planet(self.space,
(100, -500),
path+"assets/Planets/planet.png",
(590, 590),
100,
0.9,
0.5,
275)
# Appending all sprites to groups
self.main_group = MainGroup(self.colors["default_bg"],
self.space,
*self.decorations,
self.spaceship,
*self.asteroids,
self.planet)
self.spaceship.detect_main_group()
def draw_text(self):
text = f"[Pos]: {round(self.spaceship.body.position[0],1)} | {round(self.spaceship.body.position[1],1)}\n[Rot]: {round(degrees(self.spaceship.body.angle),1)}\n[Vel]: {round(self.spaceship.body.velocity[0],1)} | {round(self.spaceship.body.velocity[1],1)}\n[RotVel]: {round(self.spaceship.body.angular_velocity,1)}"
for i, j in enumerate(text.splitlines()):
self.screen.blit(self.characteristics_font.render(
j,
True, (246, 124, 0)), (500, 30+30*i))
def draw_all(self):
self.main_group.update()
self.main_group.draw()
self.draw_text()
if self.debug:
self.space.debug_draw(self.draw_options)
def run(self):
# Main loop
while self.running:
self.draw_all()
self.space.step(self.dt)
pygame.display.update()
self.clock.tick(self.max_fps)
controller.read_and_apply_input(self)
if __name__ == '__main__':
game = Game(debug=0)
game.run()