main.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from turtle import Screen
  2. from paddle import Paddle
  3. from ball import Ball
  4. from screenScore import ScoreScreen
  5. from screenTable import ScreenTable
  6. from config import SCREEN_WIDTH, SCREEN_HEIGHT, PADDLE_LENGTH, MAX_PLAYER_SCORE
  7. screen = Screen()
  8. screen.bgcolor("black")
  9. screen.setup(width=SCREEN_WIDTH, height=SCREEN_HEIGHT)
  10. screen.title("Pong")
  11. screen.tracer(0)
  12. r_paddle = Paddle(SCREEN_WIDTH / 2 - 30)
  13. l_paddle = Paddle(30 - SCREEN_WIDTH / 2)
  14. ball = Ball()
  15. score_screen = ScoreScreen()
  16. screen_table = ScreenTable()
  17. game_is_on = True
  18. def q():
  19. global game_is_on
  20. print('quit')
  21. screen_table.quit()
  22. game_is_on = False
  23. screen.listen()
  24. screen.onkey(r_paddle.up, "Up")
  25. screen.onkey(r_paddle.down, "Down")
  26. screen.onkey(l_paddle.up, "w")
  27. screen.onkey(l_paddle.down, "s")
  28. screen.onkey(q, "q")
  29. def restart_game():
  30. screen_table.reset()
  31. score_screen.reset()
  32. while game_is_on:
  33. screen.update()
  34. if screen_table.r_score > MAX_PLAYER_SCORE or screen_table.l_score > MAX_PLAYER_SCORE:
  35. restart_game()
  36. if ball.is_paddle_hit(l_paddle, r_paddle):
  37. score_screen.increase(ball)
  38. ball.hit('hor')
  39. ball.move()
  40. if ball.xcor() > SCREEN_WIDTH / 2 or ball.xcor() < -SCREEN_WIDTH / 2:
  41. if (ball.xcor() > 0):
  42. screen_table.increase('l')
  43. else:
  44. screen_table.increase('r')
  45. ball.reset()
  46. score_screen.reset()
  47. screen.exitonclick()