forked from wishrohitv/Pygame-to-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitems.py
More file actions
94 lines (83 loc) · 2.79 KB
/
items.py
File metadata and controls
94 lines (83 loc) · 2.79 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
import os
import pygame
from ui import Screen
class Element(pygame.sprite.Sprite):
image: pygame.Surface
rect: pygame.Rect
def __init__(
self,
size: tuple[int, int],
pos: tuple[int, int],
color: tuple[int, int, int] = (0, 0, 0),
image: str | None = None,
):
super().__init__()
if image != None:
self.image = pygame.image.load(image).convert_alpha()
else:
self.image = pygame.Surface(size)
self.image.fill(color)
self.rect = self.image.get_rect(topleft=pos)
class Car(Element):
skinlist = {
"main": os.path.abspath("storage/main_car.png"),
"tractor": os.path.abspath("storage/tractor_car.png"),
"passenger": os.path.abspath("storage/passenger_car.png"),
}
def __init__(
self,
size: tuple[int, int],
pos: tuple[int, int],
skin: str = "main",
relative_width: float = 0.2,
):
super().__init__(size, pos, image=self.skinlist[skin])
window_width = Screen.current_width()
self.image = pygame.transform.scale(
self.image,
(window_width * relative_width, window_width * relative_width * 2),
)
self.rect = self.image.get_rect()
class Road(pygame.sprite.Group):
def __init__(self):
super().__init__()
self.size = Screen.current_size()
self.road_width = int(self.size[0] / 1.4)
self.roadmark_width = int(self.size[0] / 60)
self.elements = [
{
"size": (self.road_width, self.size[1]),
"pos": (self.size[0] / 2 - self.road_width / 2, 0),
"color": (50, 50, 50),
},
{
"size": (self.roadmark_width, self.size[1]),
"pos": (self.size[0] / 2 - self.roadmark_width / 2, 0),
"color": (255, 240, 60),
},
{
"size": (self.roadmark_width, self.size[1]),
"pos": (
self.size[0] / 2 - self.road_width / 2 + self.roadmark_width * 2,
0,
),
"color": (255, 255, 255),
},
{
"size": (self.roadmark_width, self.size[1]),
"pos": (
self.size[0] / 2 + self.road_width / 2 - self.roadmark_width * 3,
0,
),
"color": (255, 255, 255),
},
]
self._build_road()
def _build_road(self):
def add_sprite(
size: tuple[int, int], pos: tuple[int, int], color: tuple[int, int, int]
):
elem = Element(size, pos, color)
self.add(elem)
for i in self.elements:
add_sprite(i["size"], i["pos"], i["color"])