-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
197 lines (141 loc) · 5.12 KB
/
main.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Example file showing a basic pygame "game loop"
import pygame
from math import sin, cos
import random as rnd
import time
# pygame setup
pygame.init()
pygame.font.init()
deffont = pygame.font.SysFont('Arial', 30)
WIDTH = 1280
HEIGHT = 720
mdflap_img = pygame.transform.scale(pygame.image.load("assets/yellowbird-midflap.png"), (50, 50))
mdflap_rect = mdflap_img.get_rect()
mdflap_rect.x = 100
dnflap_img = pygame.transform.scale(pygame.image.load("assets/yellowbird-downflap.png"), (50, 50))
dnflap_rect = dnflap_img.get_rect()
dnflap_rect.x = 100
upflap_img = pygame.transform.scale(pygame.image.load("assets/yellowbird-upflap.png"), (50, 50))
upflap_rect = upflap_img.get_rect()
upflap_rect.x = 100
background_img = pygame.transform.scale(pygame.image.load("assets/background.png"), (HEIGHT/512 * 288, HEIGHT))
background_rect = background_img.get_rect()
current_img = upflap_img
current_rect = upflap_rect
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
running = True
frame = 0
class Tube():
def __init__(self, y, gap_height=250): # init tubes
self.color = (20,200,10)
self.texture = pygame.image.load("assets/tube.png")
self.wid = 50
self.fac = gap_height
self.y = y
# define top texture
self.img_top = pygame.transform.rotate(pygame.transform.scale(self.texture, (self.wid, self.fac+self.y)), 180)
self.rec_top = self.img_top.get_rect()
# define bottom texture
self.img_dwn = pygame.transform.flip(pygame.transform.scale(self.texture, (self.wid, self.fac-self.y)), True, False)
self.rec_dwn = self.img_top.get_rect()
self.x = WIDTH-self.wid
# TODO: Neues Skalierungsmodell sodass die tubes richtig aussehen
# distance from top
self.rec_top.top = 0
self.rec_dwn.top = HEIGHT-self.fac+self.y
# distance from left
self.rec_top.left = WIDTH-self.wid
self.rec_dwn.left = WIDTH-self.wid
def draw(self): # handles drawing
screen.blit(self.img_top, self.rec_top)
screen.blit(self.img_dwn, self.rec_dwn)
def set_x(self, x): # handles setting x
self.x = x
self.rec_top.x = x
self.rec_dwn.x = x
def reload_recs(self):
self.rec_top = pygame.Rect(WIDTH-self.wid, 0 , self.wid, self.fac+self.y)
self.rec_dwn = pygame.Rect(WIDTH-self.wid, HEIGHT-self.fac+self.y, self.wid, self.fac-self.y)
def decrease_x(self, x=1): # handles decreasing x for moving tubes
self.x -= x
self.rec_top.x -= x
self.rec_dwn.x -= x
tubez = Tube(0)
curr_y = 0
currynormalized = (HEIGHT/2) + curr_y
player = 100
jumping = 0
jumpFactor = 0.2
score = 0
while running:
# handle quitting
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
jumping = -jumpFactor*30
player += jumping
jumping += jumpFactor
# handle player on the ground
if player < HEIGHT-50:
player += jumping
jumping += jumpFactor
if player > HEIGHT-50:
player = HEIGHT - 50
if player <= 0:
jumping = 0
player = 1
if jumping > 1:
current_img = upflap_img
current_rect = upflap_rect
elif jumping > 0:
current_img = mdflap_img
current_rect = mdflap_rect
else:
current_img = dnflap_img
current_rect = dnflap_rect
# clear display
screen.fill((45, 255, 227))
# set background
_ = -(frame % background_rect.width)
background_rect.x = _
while _ < WIDTH*2:
screen.blit(background_img, background_rect)
_ += background_rect.width
background_rect.x = _
# handle tubes
tubez.draw()
tubez.decrease_x(5 + min(score/5, 12))
# draw player
current_rect.y = player
screen.blit(current_img, current_rect)
# handle score display
text_surface = deffont.render(f'Score: {score}', False, (0, 0, 0))
screen.blit(text_surface, (0,0))
# flip display
pygame.display.flip()
# handle Game over
if tubez.x <=150 and tubez.x >=100:
if player>currynormalized+tubez.fac/2 or player<currynormalized-tubez.fac/2:
print("GAME OVER")
screen.fill((0, 0, 0))
game_over1 = deffont.render(f'GAME OVER!', False, (255, 255, 255))
game_over2 = deffont.render(f'Score: {score}', False, (255, 255, 255))
game_over3 = deffont.render(f'Game will close in 3s', False, (255, 255, 255))
screen.blit(game_over1, (WIDTH/2, HEIGHT/2-15))
screen.blit(game_over2, (WIDTH/2, HEIGHT/2+15))
screen.blit(game_over3, (WIDTH/2, HEIGHT/2+45))
pygame.display.flip()
time.sleep(3)
running = False
# handle tubes reaching left screen border
if (tubez.x < 0-tubez.wid):
del tubez
score += 1
curr_y = rnd.randint(-150, 150)
tubez = Tube(curr_y)
currynormalized = (HEIGHT/2) + curr_y
frame += 1
clock.tick(60) # limits FPS to 60
pygame.quit()