-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgroundobjects.py
More file actions
106 lines (95 loc) · 4.27 KB
/
Copy pathgroundobjects.py
File metadata and controls
106 lines (95 loc) · 4.27 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
import pygame
import json
import os
import random
class GroundObjects():
def __init__(self, screen):
# Data structures
self.screen = screen
self.plane = ''
self.groundobjects = []
# Load declarations
dir_path = os.path.dirname(os.path.realpath(__file__))
for filename in ["departure_airport.json", "arrival_airport.json"]:
ground_object_path = os.path.join(dir_path, 'ground_objects', filename)
with open(ground_object_path, "r") as fp:
objectmap = json.load(fp)
objects = objectmap["objects"]
x_offset = objectmap["x-offset"]
# Create Ground objects
for declaration in objects:
self.groundobjects.append(
self.GroundObject(
screen,
declaration["x"] + x_offset,
declaration["y"],
declaration["repeat"],
declaration["crashable"],
declaration["texture"]
)
)
def register_plane(self, plane):
self.plane = plane
# propage the plane to all groundobjects
for groundobject in self.groundobjects:
groundobject.register_plane(plane)
def update(self):
for groundobject in self.groundobjects:
groundobject.update()
def get_groundobjects(self):
return self.groundobjects
class GroundObject(pygame.sprite.Sprite):
def __init__(self, screen, x, y, repeat, crashable, texture):
# Initialize members
self.screen = screen
self.plane = ''
self.crashable = crashable
self.last_travelled_x_distrance = 0
dir_path = os.path.dirname(os.path.realpath(__file__))
texture_path = os.path.join(dir_path, 'graphics', texture)
img = pygame.image.load(texture_path)
# us a list of rects to repeat the image
self.image_maps = []
for current_repetition in range(0, repeat):
image_map = {}
# load the image
image_map["texture"] = img
image_map["rect"] = image_map["texture"].get_rect()
width = image_map["rect"].size[0]
# compute the offset based on the repetition
image_map["rect"].centerx = x + (width * current_repetition)
# ground objects are on the ground, in line
image_map["rect"].centery = y
image_map["original_centery"] = y
# append the newly computed rect
self.image_maps.append(image_map)
# Init the sprite
pygame.sprite.Sprite.__init__(self)
def register_plane(self, plane):
self.plane = plane
self.last_travelled_x_distrance = plane.get_travelled_x_distance()
def is_crashable(self):
return self.crashable
def get_image_maps(self):
return self.image_maps
def update(self):
# Compute the new x-offsets
x_offset = self.plane.get_travelled_x_distance() - self.last_travelled_x_distrance
# Calculate the correction factor for y based on plane height
y_offset = self.plane.get_y_coords() - 450
for i in range(0, len(self.image_maps)):
# if the image is not on the screen anymore skip processing
if (self.image_maps[i]["rect"].centerx < -300):
continue
else:
# Blit the ground texture
self.screen.blit(self.image_maps[i]["texture"], self.image_maps[i]["rect"])
# update x-coordinates if the plane has moved
if x_offset > 0:
self.image_maps[i]["rect"].centerx -= x_offset
# Correction for ground level
if (self.plane.get_y_coords()) < 450:
# Restore previous y value
self.image_maps[i]["rect"].centery = self.image_maps[i]["original_centery"] - y_offset
# Update plane distance:
self.last_travelled_x_distrance = self.plane.get_travelled_x_distance()