-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp
178 lines (144 loc) · 5.2 KB
/
temp
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
#!/usr/bin/python3
import pygame
from objects import *
levels = {0 : [
{'obj': ground, 'pos': (0, 700), 'size': (500, 100)},
{'obj': ground, 'pos': (600, 700), 'size': (400, 100)},
#{'obj': inviz_ground, 'pos': (500, 700), 'size': (100, 100)},
{'obj': player, 'pos':(0, 250), 'size': (5, 5)},
{'obj': player, 'pos': (10, 10), 'size': (5, 5)}
]
}
pygame.init()
def update_obj_on_lvl():
for objec in obj_list:
objec.update()
screen = pygame.display.set_mode((1000, 800))
# p = player(screen=screen)
# p.scale(100, 100)
# p.make_on_pos(10, 10)
# g = ground(screen=screen)
# g.scale(1000, 100)
# g.make_on_pos(0, 700)
#יוצר אובייקים בשלב
for lvl in levels[0]:
objec = lvl["obj"](screen)
if player.__name__ == lvl["obj"].__name__:
players.append(objec)
objec.make_on_pos(*lvl["pos"])
objec.scale(*lvl["size"])
obj_list.append(objec)
objec.update()
running = True
pygame.display.flip()
while running:
screen.fill("black")
update_obj_on_lvl()
key = pygame.key.get_pressed()
if key[pygame.K_RIGHT]:
for p in players:
p.velocity[0] = 1
if key[pygame.K_LEFT]:
for p in players:
p.velocity[0] = -1
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
for p in players:
key = pygame.key.get_pressed()
if p.on_ground:
if key[pygame.K_SPACE]:
p.move(y=-1)
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
[object]
import pygame
solids = []
obj_list = []
players = []
class template_obj():
def make_colisions(self):
for colision_name in self.template_colisions.keys():
template = self.template_colisions[colision_name]
colision = pygame.Rect(self.x + template[0], self.y + template[1], self.W + template[2], self.H + template[3])
pygame.draw.rect(self.screen, "blue", colision)
self.colisions[colision_name] = colision
def __init__(self, img, screen):
self.IMG = img
self.screen = screen
try:
self.loaded_img = pygame.image.load(img)
except pygame.error:
print(f"Error: Image file '{img}' not found.")
# Handle the error gracefully, e.g. by using a default image or displaying an error message to the user.
self.H = self.loaded_img.get_height()
self.W = self.loaded_img.get_width()
self.x, self.y = 0 ,0
self.colisions = {}
# x y w h
self.template_colisions = {'sprite': (0, 0 ,0 ,0)}
self.make_colisions()
def make_on_pos(self, x, y):
self.screen.blit(self.loaded_img, (x, y))
self.x, self.y = x, y
self.make_colisions()
def scale(self, width, height):
self.W, self.H = width, height
self.loaded_img = pygame.transform.scale(self.loaded_img, (width, height))
self.make_colisions()
def check_colision(self, rect_list):
colisions_touching = []
for colision_name in self.colisions.keys():
colision = self.colisions[colision_name]
if len(colision.collidelistall(rect_list)) > 0:
colisions_touching.append(colision_name)
return colisions_touching
class player(template_obj):
def __init__(self, screen):
self.jumping = False
self.screen = screen
self.velocity = [0, 0]
self.gravity = 0.004
self.on_ground = False
self.pos_on_list = len(solids)
super().__init__(r"./recorces/player.gif" ,screen)
solids.append(self.colisions["sprite"])
def move(self, x = None, y = None):
self.velocity[0] += x if x != None else self.velocity[0]
self.velocity[1] += y if y != None else self.velocity[1]
def fall(self):
if self.on_ground & ( self.velocity[1] > 0 ):
self.velocity[1] = 0
else:
self.move(y= self.gravity)
def friction(self):
if abs(self.velocity[0]) > 0.01:
self.velocity[0] *= 0.3
else:
self.velocity[0] = 0
def update(self):
#solids.append(self.colisions["sprite"])
self.on_ground = len( self.check_colision(solids) ) > 0
self.friction()
self.fall()
self.x += self.velocity[0]
self.y += self.velocity[1]
self.make_on_pos(self.x, self.y)
class ground(template_obj):
def __init__(self, screen):
super().__init__(r"./recorces/player.gif", screen)
self.pos_on_list = len(solids)
solids.append(self.colisions["sprite"])
def update(self):
solids[self.pos_on_list] = self.colisions["sprite"]
self.make_on_pos(self.x, self.y)
def game_over(screen):
imp = pygame.image.load(r"D:\Users\user\Downloads\hakathon-game-Ofeks\recorces\game over.jpg").convert()
pygame.display.set_mode((imp.get_width(),imp.get_height()))
screen.blit(imp, (0,0))
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False