preface
Good morning everyone! Are you looking forward to the game?
Chestnut today to everyone to design a simple screen but the operation is very abuse of the heart of the small game, the game is quite a test of your reaction and hand
Quick, there’s a screen full of tiny particles that you can’t hide from.
Chestnut in this little game, never live more than 60 seconds, I do not know how long you can insist?
No more words to say, we come to experience it!
The body of the
Implementation of particle Avoidance Mini-game
This is an animation of the motion of particles, calculated by the particles touching or touching the border
The game stops. How do you achieve this effect? Today, let’s explore it with xiaobian.
Environment configuration:
Python3, Pycharm, Pygame.
PIP Install PyGame
Effect display:
Game interface —
Game over —
Code demo:
import time import pygame W = 600 H = 500 class Ball: X = None # x y = None # y speed_x = None # x speed speed_y = None # y speed radius = None # color = None # color def __init__(self, x, y, speed_x, speed_y, radius, color): """ initialize :param x: x coordinates :param y: y coordinates :param speed_x: X speed :param speed_Y: Y speed :param radius: Param color: Self. x = x self.y = y self.speed_x = speed_x self.speed_y = speed_y self.radius = radius self.color = color def self.x = x self.y = speed_x self.y = speed_y self.radius = radius self.color = color def Draw (self, screen): """ Draw a ball :param screen: window :return: """ pygame.draw.circle(screen, self.color, [self.x, self.y], self.radius) def move(self, screen): """ Ball move :param screen: window :return: Self. x += self.speed_x self.y += self.speed_y # if self.x > W - self.radius or self.x < self. If self.y > H -self. radius or self.y < self.radius: Self.speed_y = -self.speed_y # time. Sleep (0.001) self.draw(screen) class Player: X = 1000 y = 1000 def __init__(self, radius, color): "" Self. radius = radius self.color = color def move(self, screen): # pygame.mouse.get_focused(): pygame.mouse.get_focused() Get_pos () mouse = pygame.mouse. Get_pressed () pygame.draw. Circle (screen, self.color, [x, Y], self.radius) self.x = x self.y = y balls = [] def create_ball(screen): "" """ x = random.randint(0, W) y = random.randint(0, H) speed_x = random.randint(-5, 5) speed_y = random.randint(-5, 5) r = 3 color = 'white' b = Ball(x, y, speed_x, speed_y, r, color) balls.append(b) b.draw(screen) def show_text(screen, text, pos, color, font_bold=False, font_size=18, Font_italic =False): """ display text: param screen: window :param text: text: param pos: Coordinates :param color: color: param font_bold: Bold :param font_size: size: param font_italic: italic: return: """ cur_font = pygame.font.SysFont('Courier', font_size) cur_font.set_bold(font_bold) cur_font.set_italic(font_italic) text_fmt = cur_font.render(text, 1, color) screen.blit(text_fmt, pos) def close(): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0) def main(): Screen = Pygame.display.set_mode ((W,H)) # Set the window title Pygame.display. Set_caption (' Escape particle game ') for I in range(0, 10): create_ball(screen) p = Player(10, 'red') text_time = "TIME:%.3d" % (time.perf_counter()) is_loop = True while is_loop: # redraw screen.fill((0)) p.m move(screen) for ball in balls: ball.move(screen) if abs(p.x - ball.x) < 13 and abs(p.y - ball.y) < 13: Is_loop = False break # text_time = "TIME:%.3d" % (time.perf_counter()) show_text(screen, text_time, (500, 40)) (255,192,203), True) Pygame.display.update () close() while True: close() show_text(screen, "Game over! , (120, 180), "pink", True, 60) show_text(screen, text_time, (220, 270), "pink", True, 30) pygame.display.update() if __name__ == '__main__': main()Copy the code
summary
With this “avoid particles small game” you are no longer bored…… Hey hey. JPG
Like the little attention sa! Need source drop old rules drop me, private letter can be
Source code base —
Private letter xiaobian 06 or click on this line of blue font can be free ha!
🎉 Recommended reading —
Project collection 1.0 all kinds of game source code here for free!
Project collection 1.1 various manual only aspects of the source project site here!
Project collection 1.2 all kinds of project source code are here to see!
Project collection 1.3 all kinds of Turtle code to find here!
🎄 Article summary —
The article summary | Python 1.0-2021 | has continuously updated, direct look at this article is enough
More content + source code in the article summary oh!! Welcome to read ~)