Browse Source

Writing the score (lab4)

master
Lev 3 years ago
parent
commit
4f4c0dfcaa
  1. 17
      lab4/main.py

17
lab4/main.py

@ -11,8 +11,8 @@ if typing.TYPE_CHECKING:
pygame.init()
# width and height of the screen
W, H = 900, 800
# width and height of the main part of the screen + vertical offset for general info
W, H, OFFSET = 900, 800, 50
# ball's lifetime (seconds)
DEFAULT_TTL = 3
# ball's radius (default)
@ -21,6 +21,7 @@ DEFAULT_RADIUS = 20
dt = 10
# load the font
font = pygame.font.Font('orbitron-medium.otf', 15)
font_large = pygame.font.Font('orbitron-medium.otf', 25)
N_BALLS = 18
# List of ball types: [(radius, speed, color, points)]
@ -47,6 +48,10 @@ class Game:
"""
for ball in self.balls:
ball.draw_the_ball(screen)
pygame.draw.rect(screen, (255, 255, 255), pygame.Rect(0, 0, W, OFFSET))
text_surface = font_large.render(f'Score: {self.score}', False, (0, 0, 0))
screen.blit(text_surface, (360, 15))
def process_click(self, x: float, y: float):
"""
@ -55,7 +60,7 @@ class Game:
:param y: click's y coordinate
"""
for i in range(len(self.balls)):
if self.balls[i].is_point_inside(x, y):
if self.balls[i].is_point_inside(x, y - OFFSET):
self.score += self.balls[i].points
self.balls.pop(i)
break
@ -104,11 +109,11 @@ class Ball:
Just draw the ball on the screen
:param screen: the screen, obviously
"""
pygame.draw.circle(screen, self.color, self.position.tolist(), self.radius)
pygame.draw.circle(screen, self.color, [self.position[0], self.position[1] + OFFSET], self.radius)
text_surface = font.render(str(self.points), False, (0, 0, 0))
screen.blit(text_surface, (
round(self.position[0] - text_surface.get_rect().width / 2),
round(self.position[1] - text_surface.get_rect().height / 2)
round(self.position[1] - text_surface.get_rect().height / 2) + OFFSET
))
def is_point_inside(self, x: float, y: float) -> bool:
@ -140,7 +145,7 @@ class Ball:
# create the display surface object
screen = pygame.display.set_mode((W, H))
screen = pygame.display.set_mode((W, H + OFFSET))
game = Game()

Loading…
Cancel
Save