-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
145 lines (132 loc) · 5.27 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
# Pygame project
import pygame
import random
import time
from settings import *
class FloppyBird():
# initialize pygame and create window
def __init__(self):
self.screen = pygame.display.set_mode((400, 608))
self.font_name = pygame.font.match_font(FONT_NAME)
pygame.display.set_caption(TITLE)
self.bird = pygame.Rect(65, 50, 50, 50)
self.background = pygame.image.load("assets/background.png").convert()
self.birdSprites = [pygame.image.load("assets/1.png").convert_alpha(),
pygame.image.load("assets/2.png").convert_alpha(),
pygame.image.load("assets/dead.png")]
self.wallUp = pygame.image.load("assets/bottom.png").convert_alpha()
self.wallDown = pygame.image.load("assets/top.png").convert_alpha()
# Gap is the space between the two wall - Gap è lo spazio tra i due muri
self.gap = 150
# Wallx is the distance of the walls from the beginning - è la distanza dei muri dall'inizio del gioco
self.wallx = 200
# BirdY is the location where the bird is located at the beginning - locazione del personaggio all'inizio del gioco
self.birdY = 300
# Jump initialize - inizializzazione del salto
self.jump = 0
# JumpSpeed is the velocity of the jump - velocità del salto
self.jumpSpeed = 20
self.gravity = 3
self.dead = False
self.sprite = 0
self.counter = 0
self.offset = random.randint(-110, 110)
self.clock = pygame.time.Clock()
pygame.font.init()
self.font = pygame.font.SysFont("Arial", 50)
self.running = True
def updateWalls(self):
self.wallx -= 2
if self.wallx < -80:
self.wallx = 400
self.counter += 1
self.offset = random.randint(-110, 110)
def birdUpdate(self):
if self.jump:
self.jumpSpeed -= 0.5
self.birdY -= self.jumpSpeed
self.jump -= 0.5
else:
self.birdY += self.gravity
self.gravity += 0.2
self.bird[1] = self.birdY
upRect = pygame.Rect(self.wallx,
360 + self.gap - self.offset + 10,
self.wallUp.get_width() - 10,
self.wallUp.get_height())
downRect = pygame.Rect(self.wallx,
0 - self.gap - self.offset - 10,
self.wallDown.get_width() - 10,
self.wallDown.get_height())
if upRect.colliderect(self.bird):
self.dead = True
if downRect.colliderect(self.bird):
self.dead = True
if not 0 < self.bird[1] < 720:
self.bird[1] = 50
self.birdY = 50
self.dead = False
self.counter = 0
self.wallx = 400
self.offset = random.randint(-110, 110)
self.gravity = 5
def show_start_screen(self):
self.screen.blit(self.background, (0, 0))
self.draw_text("Bird", 64, BLACK, WIDTH / 2, HEIGHT * 1 / 7)
self.draw_text("(c) Mezzas", 22, BLACK, WIDTH / 2, HEIGHT * 1 / 4)
self.draw_text("Press a key to play", 18, BLACK, WIDTH / 2, HEIGHT * 4 / 5)
self.draw_text("Floppy", 64, BLACK, WIDTH / 2, 15)
pygame.display.flip()
self.wait_for_key()
def draw_text(self, text, size, color, x, y):
self.font = pygame.font.Font(self.font_name, size)
self.text_surface = self.font.render(text, True, color)
self.text_rect = self.text_surface.get_rect()
self.text_rect.midtop = (x, y)
self.screen.blit(self.text_surface, self.text_rect)
def run(self):
while self.running:
self.clock.tick(FPS)
self.events()
self.draw()
self.updateWalls()
self.birdUpdate()
def draw(self):
self.screen.fill((255, 255, 255))
self.screen.blit(self.background, (0, 0))
self.screen.blit(self.wallUp,
(self.wallx, 360 + self.gap - self.offset))
self.screen.blit(self.wallDown,
(self.wallx, 0 - self.gap - self.offset))
self.screen.blit(self.font.render(str(self.counter), -1, (255, 255, 255)), (200, 50))
if self.dead:
self.sprite = 2
elif self.jump:
self.sprite = 1
self.screen.blit(self.birdSprites[self.sprite], (70, self.birdY))
if not self.dead:
self.sprite = 0
pygame.display.update()
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if event.type == pygame.KEYDOWN and not self.dead:
self.jump = 17
self.gravity = 5
self.jumpSpeed = 10
def wait_for_key(self):
waiting = True
while waiting:
self.clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
waiting = False
self.running = False
if event.type == pygame.KEYUP:
waiting = False
g = FloppyBird()
g.show_start_screen()
while g.running:
g.run()
pygame.quit()