TextObject
class, which is used to render text on the screen. We will create the main window, including the background image, and then learn how to draw objects: bricks, a ball and a racket.
TextObject
class TextObject
designed to display text on the screen. It can be concluded that from the point of view of design it must be a subclass of the GameObject
class, because it is also a visual object and also needs to be moved sometimes. But I did not want to introduce a deep hierarchy of classes, in which all the Breakout text displayed remained unchanged on the screen.
TextObject
class creates a font object. It renders the text on a separate text surface, which is then copied (rendered) onto the main surface. An interesting aspect of TextObject
is that it does not have any fixed text. It gets the text_func()
function that is called each time it is rendered.
import pygame class TextObject: def __init__(self, x, y, text_func, color, font_name, font_size): self.pos = (x, y) self.text_func = text_func self.color = color self.font = pygame.font.SysFont(font_name, font_size) self.bounds = self.get_surface(text_func()) def draw(self, surface, centralized=False): text_surface, self.bounds = \ self.get_surface(self.text_func()) if centralized: pos = (self.pos[0] - self.bounds.width // 2, self.pos[1]) else: pos = self.pos surface.blit(text_surface, pos) def get_surface(self, text): text_surface = self.font.render(text, False, self.color) return text_surface, text_surface.get_rect() def update(self): pass
init()
Pygame is called, and then the main drawing surface and the timer are created.
tick()
timer method with a frame rate.
import pygame pygame.init() screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() while True: screen.fill((192, 192, 192)) pygame.display.update() clock.tick(60)
pygame.image.load()
function. Then, instead of filling the screen with color, it blits (copies the bits) of the image onto the screen at the position (0,0). As a result, an image is displayed on the screen.
import pygame pygame.init() screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() background_image = pygame.image.load('images/background.jpg') while True: screen.blit(background_image, (0, 0)) pygame.display.update() clock.tick(60)
pygame.draw
module pygame.draw
are functions for drawing the following shapes:
pygame.draw.rect()
function that gets the surface, color, and a Rect object (left and top coordinates, width and height) and a rendering rectangle. If the optional width parameter is greater than zero, then it draws a path. If the width is zero (the default value), then draws a solid rectangle.
Brick
class is a subclass of GameObject
and gets all its properties, but also has a color that processes itself (because there may be game objects that have several colors). We will not consider the special_effect
field yet.
import pygame from game_object import GameObject class Brick(GameObject): def __init__(self, x, y, w, h, color, special_effect=None): GameObject.__init__(self, x, y, w, h) self.color = color self.special_effect = special_effect def draw(self, surface): pygame.draw.rect(surface, self.color, self.bounds)
pygame.draw.circle()
function that receives the color, center, radius, and optional width parameter, which defaults to zero. As in the pygame.draw.rect()
function, if the width is zero, then a solid circle is drawn. Ball is also a subclass of GameObject.
GameObject
class to process. The Ball class has a slight difference - the x and y parameters denote its center, and the x and y parameters passed to the base GameObject
class are the upper left corner of the bounding box. To convert the center to the upper left corner, just subtract the radius.
import pygame from game_object import GameObject class Ball(GameObject): def __init__(self, x, y, r, color, speed): GameObject.__init__(self, x - r, y - r, r * 2, r * 2, speed) self.radius = r self.diameter = r * 2 self.color = color def draw(self, surface): pygame.draw.circle(surface, self.color, self.center, self.radius)
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 draw(self, surface): pygame.draw.rect(surface, self.color, self.bounds)
Source: https://habr.com/ru/post/347170/