This is the fifth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Hello, I’m Yue Chuang.

So, we’ve talked a little bit about pyGame operations, and now we’re in the real world.

directory

  1. The game is introduced
  2. Pinball game

1. Introduction to the game

Football movement in the picture, encounter up, left, right and the rod will rebound, and the rod will catch the points, if the fall to the bottom, the game is over.

2. The small ball

  1. Create a new folder, create the py file, image file under the folder, and then we start to write code. This is a picture, can be changed to draw the circle.

Next, write the basic code:

import pygame, sys
from pygame.locals import *

# Initialize PyGame
pygame.init()
screen = pygame.display.set_mode([800.700])

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    pygame.display.flip()
Copy the code
  1. Create a class that controls the ball to give the ball a style. Get the rectangle marquee of the ball, and then get the left and top edge of the rectangle marquee of the ball, and the speed of the ball.
# Create ball class
class Myballclass(pygame.sprite.Sprite) :
    # Give the velocity of the graph and give the corresponding value
    def __init__(self, image_file, speed, location) :
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.image = pygame.transform.smoothscale(self.image, (80.70))
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location
        self.speed = speed
Copy the code
  1. Create the ball instantiation object and display it on the screen.
# Ball given value
myball = Myballclass(r'football.png', ball_speed, [10.20])
# Refresh time
time = 30
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    screen.blit(myball.image, myball.rect)
    pygame.display.flip()
Copy the code
  1. Now let the ball move and bounce. When the ball moves to the very edge, the velocity is reversed (this is written in the ball, and self is used to indicate the class property).
# Ball movement
    def ball_move(self) :
        self.rect = self.rect.move(self.speed)
        # Control the ball in the game interface
        if self.rect.left < 0 or self.rect.right > screen.get_width():
            self.speed[0] = -self.speed[0]
        if self.rect.top <= 0:
            self.speed[1] = -self.speed[1]
Copy the code
  1. Class needs to write the terminating function. Still keep the ball moving, and then decide that the game is over and process the words to be displayed at the end of the game.
 # Game over
    def over(self) :
        self.rect = self.rect.move(self.speed)

        The game ends when the bottom of the ball is greater than the height of the screen
        if self.rect.bottom > screen.get_height():
            # SysFount Create a font object from the system font (font style, size)
            font = pygame.font.SysFont('宋体'.40)

            # Render paint text on new Surface (text, anti-aliasing, color, background)
            text_surface = font.render(u"Game Over".True, (0.0.255))
            screen.blit(text_surface, (screen.get_width() // 2, screen.get_height() // 2))
            return 0
Copy the code

The next part, continue tomorrow!

Does this article meet the requirements for entry? Please let me know by leaving a message. The code ratio is not over? thank you