Score for pong displayed at top+borders

This commit is contained in:
adithyagenie 2022-11-24 00:53:26 +05:30
parent 149819fdd4
commit d5ec6036c5

15
pong.py
View file

@ -8,8 +8,9 @@ quit = threading.Event()
class Scores: class Scores:
def __init__(self): def __init__(self, screen):
self.score = 0 self.score = 0
self.screen = screen
self.collision_count = 0 self.collision_count = 0
self.speed_multiplier = 0.20 self.speed_multiplier = 0.20
self.speed_calc = lambda speed: speed - 0.02 if self.score % 30 == 0 else speed self.speed_calc = lambda speed: speed - 0.02 if self.score % 30 == 0 else speed
@ -18,8 +19,13 @@ class Scores:
self.score += 10 self.score += 10
self.collision_count += 1 self.collision_count += 1
self.speed_multiplier = self.speed_calc(self.speed_multiplier) self.speed_multiplier = self.speed_calc(self.speed_multiplier)
self.scoreupdater()
return self.speed_multiplier return self.speed_multiplier
def scoreupdater(self):
y,x = self.screen.getmaxyx()
self.screen.addstr(0, x - 5, str(self.score))
class Ball: class Ball:
def __init__(self, y, x, screen): def __init__(self, y, x, screen):
@ -166,8 +172,9 @@ def main(screen):
curses.curs_set(False) curses.curs_set(False)
screen.keypad(True) screen.keypad(True)
y, x = screen.getmaxyx() y, x = screen.getmaxyx()
screen.border(0,0,0," ",0,0," ", " ")
ball = Ball(y, x, screen) ball = Ball(y, x, screen)
score = Scores() score = Scores(screen)
player = Player(y, x) player = Player(y, x)
ball_thread = threading.Thread( ball_thread = threading.Thread(
target=ball_movement, target=ball_movement,
@ -184,10 +191,14 @@ def main(screen):
player, player,
), ),
) )
screen.addstr(0, x - 13, " ")
screen.addstr(0, x - 12, "Score: 0")
ball_thread.start() ball_thread.start()
player_thread.run() player_thread.run()
# player_movement(screen, player) # player_movement(screen, player)
if __name__ == "__main__": if __name__ == "__main__":
curses.wrapper(main) curses.wrapper(main)