TextObject
class, which is used to render text to the screen, created the main window, and learned how to draw objects: bricks, a ball, and a racket. # handle_mouse_event() self.mouse_handlers.append(b.handle_mouse_event) # handle() self.keydown_handlers[pygame.K_LEFT].append(paddle.handle) self.keydown_handlers[pygame.K_RIGHT].append(paddle.handle) self.keyup_handlers[pygame.K_LEFT].append(paddle.handle) self.keyup_handlers[pygame.K_RIGHT].append(paddle.handle)
handle()
method is called. The Paddle object does not need to know whether it was a key press or key release event, because it controls the current state with a pair of boolean variables: moving_left
and moving_right
. If moving_left
is True, then the left key was pressed, and the next event will be the release of the key, which will reset the variable. The same applies to the "right" key. The logic is simple and consists in switching the state of these variables in response to any event. def handle(self, key): if key == pygame.K_LEFT: self.moving_left = not self.moving_left else: self.moving_right = not self.moving_right
def handle_mouse_event(self, type, pos): if type == pygame.MOUSEMOTION: self.handle_mouse_move(pos) elif type == pygame.MOUSEBUTTONDOWN: self.handle_mouse_down(pos) elif type == pygame.MOUSEBUTTONUP: self.handle_mouse_up(pos) def handle_mouse_move(self, pos): if self.bounds.collidepoint(pos): if self.state != 'pressed': self.state = 'hover' else: self.state = 'normal' def handle_mouse_down(self, pos): if self.bounds.collidepoint(pos): self.state = 'pressed' def handle_mouse_up(self, pos): if self.state == 'pressed': self.on_click(self) self.state = 'hover'
handle_mouse_event()
method, registered to receive mouse events, checks the type of the event and forwards it to the appropriate method that handles this type of event.show_message()
method of the Breakout class uses this approach and calls time.sleep()
. Here is the corresponding code: import config as c class Breakout(Game): def show_message(self, text, color=colors.WHITE, font_name='Arial', font_size=20, centralized=False): message = TextObject(c.screen_width // 2, c.screen_height // 2, lambda: text, color, font_name, font_size) self.draw() message.draw(self.surface, centralized) pygame.display.update() time.sleep(c.message_duration)
moving_left
and moving_right
. The movement itself takes place in the update()
method. Certain calculations are performed here if the racket is close to the left or right edge of the screen. We do not want the racket to go beyond the screen (taking into account the specified offset). import pygame import config as c from game_object import GameObject class Paddle(GameObject): def __init__(self, x, y, w, h, color, offset): GameObject.__init__(self, x, y, w, h) self.color = color self.offset = offset self.moving_left = False self.moving_right = False ... def update(self): if self.moving_left: dx = -(min(self.offset, self.left)) elif self.moving_right: dx = min(self.offset, c.screen_width - self.right) else: return self.move(dx, 0)
GameObject
, which moves objects based on their speed (its horizontal and vertical components). As we will see soon, the speed of the ball is determined by many factors in the Breakout class. Since the movement is simply to add speed to the current position, the direction in which the ball moves is completely determined by the speed along the horizontal and vertical axes.create_ball()
method, it receives a speed with a random horizontal component in the interval from -2 to 2 and a vertical component specified in the config.py module (the default value is 3). def create_ball(self): speed = (random.randint(-2, 2), c.ball_speed) self.ball = Ball(c.screen_width // 2, c.screen_height // 2, c.ball_radius, c.ball_color, speed) self.objects.append(self.ball)
Source: https://habr.com/ru/post/347256/
All Articles