Advertisement
Roman9234

Untitled

Apr 25th, 2024
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.80 KB | None | 0 0
  1. import arcade
  2.  
  3. # константы
  4. SCREEN_WIDTH = 700  # ширина экрана
  5. SCREEN_HEIGHT = 500  # высота экрана
  6. SCREEN_TITLE = "Пинг-понг"  # заголовок окна
  7.  
  8. BALL_IMG = "ping-pong/ball.png"
  9. BALL_SIZE = 0.15
  10. BALL_SPEEDX = 2
  11. BALL_SPEEDY = 5
  12.  
  13. PLAT_IMG = "ping-pong/platform.png"
  14. PLAT_SIZE = 0.25
  15. PLAT_SPEED = 5
  16.  
  17.  
  18. class Platform(arcade.Sprite):
  19.     # движение спрайта
  20.     def update(self):
  21.         self.center_x += self.change_x
  22.         # установление границ (пределов)
  23.         if self.left < 0:
  24.             self.left = 0
  25.         if self.right > SCREEN_WIDTH:
  26.             self.right = SCREEN_WIDTH
  27.  
  28.  
  29. class Ball(arcade.Sprite):
  30.     def update(self):
  31.         self.center_x += self.change_x
  32.         if self.right > SCREEN_WIDTH or self.left < 0:
  33.             self.change_x = - self.change_x
  34.         self.center_y += self.change_y
  35.         if self.top > SCREEN_HEIGHT or self.bottom < 0:
  36.             self.change_y = - self.change_y
  37.  
  38.  
  39. class GameWindow(arcade.Window):
  40.     # конструктор, вызывается в момент создания окна
  41.     def __init__(self, width, height, title):
  42.         super().__init__(width, height, title)  # супер - это ссылка на родителя
  43.         self.ball = Ball(BALL_IMG, BALL_SIZE)
  44.         self.plat = Platform(PLAT_IMG, PLAT_SIZE)
  45.         self.points = 0
  46.         self.popit = 3
  47.  
  48.     # начальные координаты
  49.     def setup(self):
  50.         self.ball.center_x = SCREEN_WIDTH/2
  51.         self.ball.center_y = SCREEN_HEIGHT/2
  52.         self.ball.change_x = BALL_SPEEDX  # скорость по иксу
  53.         self.ball.change_y = BALL_SPEEDY
  54.         self.plat.center_x = SCREEN_WIDTH/2
  55.         self.plat.center_y = SCREEN_HEIGHT/10
  56.  
  57.     def on_draw(self):
  58.         arcade.start_render()  # начало отрисовки
  59.         arcade.set_background_color((0, 255, 255))
  60.         self.ball.draw()
  61.         self.plat.draw()
  62.         arcade.draw_text(f"Счет: {self.points}",
  63.                          start_x=SCREEN_WIDTH - 140,
  64.                          start_y=SCREEN_HEIGHT - 30,
  65.                          color=arcade.color.DARK_GREEN,
  66.                          font_size=24)
  67.         arcade.draw_text(f"Попытки: {self.popit}",
  68.                          start_x=20,
  69.                          start_y=SCREEN_HEIGHT - 30,
  70.                          color=arcade.color.DARK_GREEN,
  71.                          font_size=24)
  72.         # self.ball.color = arcade.color.GRAY
  73.         # self.plat.color = arcade.color.GRAY
  74.  
  75.     # обновление окна, смена кадров, игровая логика
  76.     def on_update(self, delta_time: float):
  77.         self.ball.update()
  78.         self.plat.update()
  79.         if arcade.check_for_collision(self.ball, self.plat):
  80.             self.ball.change_y = - self.ball.change_y
  81.             self.ball.bottom = self.plat.top
  82.             self.points += 1
  83.  
  84.         if self.ball.bottom <= 0:
  85.             self.popit -= 1
  86.             self.setup()
  87.  
  88.  
  89.     # нажатие на клавиши
  90.     def on_key_press(self, symbol: int, modifiers: int):
  91.         if symbol == arcade.key.D or symbol == arcade.key.RIGHT:
  92.             self.plat.change_x = PLAT_SPEED
  93.         if symbol == arcade.key.A or symbol == arcade.key.LEFT:
  94.             self.plat.change_x = - PLAT_SPEED
  95.  
  96.     # отжатие клавиш
  97.     def on_key_release(self, symbol: int, modifiers: int):
  98.         if symbol == arcade.key.D or symbol == arcade.key.RIGHT:
  99.             self.plat.change_x = 0
  100.         if symbol == arcade.key.A or symbol == arcade.key.LEFT:
  101.             self.plat.change_x = 0
  102.  
  103.  
  104. window = GameWindow(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
  105. window.setup()
  106. arcade.run()  # цикл обработки событий
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement