Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import arcade
- # константы
- SCREEN_WIDTH = 700 # ширина экрана
- SCREEN_HEIGHT = 500 # высота экрана
- SCREEN_TITLE = "Пинг-понг" # заголовок окна
- BALL_IMG = "ping-pong/ball.png"
- BALL_SIZE = 0.15
- BALL_SPEEDX = 2
- BALL_SPEEDY = 5
- PLAT_IMG = "ping-pong/platform.png"
- PLAT_SIZE = 0.25
- PLAT_SPEED = 5
- class Platform(arcade.Sprite):
- # движение спрайта
- def update(self):
- self.center_x += self.change_x
- # установление границ (пределов)
- if self.left < 0:
- self.left = 0
- if self.right > SCREEN_WIDTH:
- self.right = SCREEN_WIDTH
- class Ball(arcade.Sprite):
- def update(self):
- self.center_x += self.change_x
- if self.right > SCREEN_WIDTH or self.left < 0:
- self.change_x = - self.change_x
- self.center_y += self.change_y
- if self.top > SCREEN_HEIGHT or self.bottom < 0:
- self.change_y = - self.change_y
- class GameWindow(arcade.Window):
- # конструктор, вызывается в момент создания окна
- def __init__(self, width, height, title):
- super().__init__(width, height, title) # супер - это ссылка на родителя
- self.ball = Ball(BALL_IMG, BALL_SIZE)
- self.plat = Platform(PLAT_IMG, PLAT_SIZE)
- self.points = 0
- self.popit = 3
- # начальные координаты
- def setup(self):
- self.ball.center_x = SCREEN_WIDTH/2
- self.ball.center_y = SCREEN_HEIGHT/2
- self.ball.change_x = BALL_SPEEDX # скорость по иксу
- self.ball.change_y = BALL_SPEEDY
- self.plat.center_x = SCREEN_WIDTH/2
- self.plat.center_y = SCREEN_HEIGHT/10
- def on_draw(self):
- arcade.start_render() # начало отрисовки
- arcade.set_background_color((0, 255, 255))
- self.ball.draw()
- self.plat.draw()
- arcade.draw_text(f"Счет: {self.points}",
- start_x=SCREEN_WIDTH - 140,
- start_y=SCREEN_HEIGHT - 30,
- color=arcade.color.DARK_GREEN,
- font_size=24)
- arcade.draw_text(f"Попытки: {self.popit}",
- start_x=20,
- start_y=SCREEN_HEIGHT - 30,
- color=arcade.color.DARK_GREEN,
- font_size=24)
- # self.ball.color = arcade.color.GRAY
- # self.plat.color = arcade.color.GRAY
- # обновление окна, смена кадров, игровая логика
- def on_update(self, delta_time: float):
- self.ball.update()
- self.plat.update()
- if arcade.check_for_collision(self.ball, self.plat):
- self.ball.change_y = - self.ball.change_y
- self.ball.bottom = self.plat.top
- self.points += 1
- if self.ball.bottom <= 0:
- self.popit -= 1
- self.setup()
- # нажатие на клавиши
- def on_key_press(self, symbol: int, modifiers: int):
- if symbol == arcade.key.D or symbol == arcade.key.RIGHT:
- self.plat.change_x = PLAT_SPEED
- if symbol == arcade.key.A or symbol == arcade.key.LEFT:
- self.plat.change_x = - PLAT_SPEED
- # отжатие клавиш
- def on_key_release(self, symbol: int, modifiers: int):
- if symbol == arcade.key.D or symbol == arcade.key.RIGHT:
- self.plat.change_x = 0
- if symbol == arcade.key.A or symbol == arcade.key.LEFT:
- self.plat.change_x = 0
- window = GameWindow(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
- window.setup()
- arcade.run() # цикл обработки событий
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement