These days in the review of micro channel game aircraft war, playing playing in thinking about life, this aircraft war how can do so good, simple operation, simple start.
Help potty, YP, rice circle girls have something to cheer them up when they’re bored! Get their left/right hands moving rhythmically back and forth in the same direction!
It’s an epic invention. It’s an epic invention. It’s…
After a little twitching, I ended the game, feeling like everything was boring, and just as I was entering Sage mode, it occurred to me, wouldn’t it be nice if I could get more people to experience this magnificent feeling in a different way?
So I turned on my computer and created a plan_game.py…
Look at the renderings first
The operating environment
- Operating system: windows10
- Python version: Python 3.7
- Code editor: Pycharm 2018.2
- Use modules: OS, SYS, Random, PyGame
Because the implementation code uses a third party pyGame module, PIP install is not available. Here is a good tutorial for PyGame.
https://eyehere.net/2011/python-pygame-novice-professional-index/
The specific implementation
- First we specify the file directory for the material file. Convenient for our later use. These materials have been uploaded to the public account Python column, background reply: Aircraft war, can be obtained.
import os
Get the path to the Material_images directory under the current folder
source_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'material_images')
Copy the code
- Implement a Game class to complete the main logic of the Game.
import pygame
class Game(a):
def __init__(self, background_image_path, size=(480.700), title='Plane wars', font_name='Fangzhengshu Body', font_size=30, speed=2000):
":param background_image_path: background image path address :param size: game window size: param title: game window title: param font_name: Specify font :param font_size: specify font size: param speed: background image time to scroll the entire window once, in ms ""
self.size = size
self.screen = pygame.display.set_mode(size)
self.title = title
self.background_image_path = background_image_path
self.background = pygame.image.load(self.background_image_path).convert()
# Set the font object to get the system's own font
self.font = pygame.font.SysFont(font_name, font_size)
Get the Clock object, which we can use to get the time since the last time the image was drawn
self.clock = pygame.time.Clock()
# Initial position of background image
self.height = 0
# Use the height of the window for the time of scrolling to get the distance of scrolling per ms
self.every_ms_move_distance = self.size[1] / speed # 2 seconds,
# scores
self.score = 0
# Store all enemy planes
self.enemies = []
def show_score(self):
"" Displays the score at the top of the window, 10px away from the top, left and right centered.
pass
def set_time_passed(self):
# Control the drawing frame, the larger the faster
Get the last time to draw the image, ms
self.time_passed = self.clock.tick()
def draw_background(self):
Draw the background image and scroll down to create the impression that the plane is flying up.
# Distance per move = distance per ms * time since last move (ms)
pass
def create_enemy(self, image_path=os.path.join(source_dir,'enemy1.png'), enemy_number=5):
"" Create enemy planes: Param image_path: Image of enemy planes address: Param enemy_number: Maximum number of enemy planes on screen" "
pass
def draw_enemies(self, time_passed, screen):
"" Draw enemy planes to screen, clean up the enemy planes that ran out of the window, :param time_passed: the time that the last draw guide has passed: param screen: the window object" "drawn.
pass
def bullet_and_enemy_crash_detection(self, bullets):
Check if a bullet hits an enemy plane: Param Bullets: All the bullets in a plane.
pass
def plan_and_enemy_crash_detection(self, plan, allow_crash_size=None):
Param plan: Aircraft object :param allow_crash_size: size allowed for aircraft collision, only valid about ""
pass
def draw_plan(self, plan, time_passed):
"" Drawing aircraft :param plan: Aircraft object :param time_passed: time since last drawing :return:" "
pass
def game_over(self):
"Game over."
while True:
# Draw the background
pass
def run(self):
Game entry function, start function, body function :return: ""
Set the size of the game window
pygame.display.set_caption(self.title)
Initialize an airplane object
plan = Plan()
while True:
# If the plane self-destructs, the game is over, call game_over
pass
Check for listening events
pass
# Detect up, down, left and right movement cases.
# w, A, S, D and up, down, left, right
# Then execute plan.update to change the position of the aircraft
pass
# Bullet and enemy aircraft collision detection
self.bullet_and_enemy_crash_detection(plan.bullets)
# Collision detection between aircraft and enemy aircraft
self.plan_and_enemy_crash_detection(plan)
# set time_passed to the time since the last time
self.set_time_passed()
# Draw the background image
self.draw_background()
# display score
self.show_score()
# Build enemy planes
self.create_enemy()
# Draw enemy aircraft
self.draw_enemies(time_passed=self.time_passed, screen=self.screen)
# Draw aircraft
self.draw_plan(plan=plan, time_passed=self.time_passed)
# Draw bullets
plan.draw_bullets(time_passed=self.time_passed, screen=self.screen)
# Show our imagePygame.display.update () tells you how to view native fonts in your system. Pygame.font-get_fonts (), this function will get all native font files in your system. However, when we had Chinese in our game, we had to choose a font that supported Chinese, otherwise it wouldn't work.Copy the code
- Implement the DestroyAnimationMixin class, which is mainly used to display self-destruct animations of aircraft or enemy aircraft
# Mixin class to display aircraft self-destruct animation, can be used for aircraft and enemy aircraft self-destruct animation display
class DestroyAnimationMixin(a):
def show_destroy_animation(self, time_passed, destroy_time=200):
An animation is an animation that moves between several images too quickly for our eyes to recognize, so it is considered dynamic. This is called an animation :param time_passed: the time since the last image was drawn, in ms: Param Destroy_time: Total display time of self-destruct animation in ms ""
# Because we have four self-destructing pictures, which need to be displayed in sequence, first of all, the effect of animation
# self.destroy_image_position indicates which chapter will self-destruct images, starting from zero
Set self.destroyed to True if the value is greater than or equal to 4
if self.destroy_image_position >= 4:
self.destroyed = True
return
# Load self-destruct images in sequence
if self.time_passed >= destroy_time / 4:
self.image = pygame.image.load(os.path.join(source_dir, self.destroy_images[self.destroy_image_position])).convert_alpha()
self.destroy_image_position += 1
self.time_passed = 0
else:
self.time_passed += time_passed
Copy the code
- Realization of aircraft class, complete the main operation of the aircraft. Aircraft operations include: aircraft position, aircraft bullets, firing bullets and so on.
DestroyAnimationMixin > DestroyAnimationMixin > DestroyAnimationMixin > DestroyAnimationMixin > DestroyAnimationMixin
class Plan(DestroyAnimationMixin):
def __init__(self, image_path=os.path.join(source_dir,'plan.png'), background_size=(480.700)):
":param image_path: plane image address :param background_size: game window size"
self.background_size = background_size
self.image = pygame.image.load(image_path).convert_alpha()
self.image_size = self.image.get_size()
self.position = [(background_size[0]-self.image_size[0) /2.500]
# The distance the aircraft moves per flight
self.every_time_move_distance = 0.5
# Airplane bullets
self.bullets = []
# destroy association Attributes
# Start self-destruct
self.start_destroy = False
# End self-destruct
self.destroyed = False
# Self-destruct images
self.destroy_images = ['me_destroy_1.png'.'me_destroy_2.png'.'me_destroy_3.png'.'me_destroy_4.png']
# Self-destruct image position
self.destroy_image_position = 0
# Time since the last image was drawn
self.time_passed = 0
def update(self, direction):
Update aircraft position :param direction: Aircraft moving direction
pass
def shut(self, image_path=os.path.join(source_dir,'bullet.png')):
"Aircraft firing bullets: Param image_path: Bullet image"
pass
def draw_bullets(self, time_passed, screen):
"" Draws all bullets from the plane :param time_passed: Time since the last time the image was drawn :param screen: to which window" "
pass
Copy the code
- Realization of enemy aircraft class, complete the main operations of the enemy aircraft. Mainly used to update location.
DestroyAnimationMixin > DestroyAnimationMixin > DestroyAnimationMixin > DestroyAnimationMixin > DestroyAnimationMixin
class Enemy(DestroyAnimationMixin):
def __init__(self, image_path=os.path.join(source_dir, 'enemy1.png'), speed=2000, background_size=(480.700)):
":param image_path: enemy plane image address :param speed: enemy plane time to move the entire window, in ms, speed: param background_size: game window size"
self.image = pygame.image.load(image_path).convert_alpha()
self.speed = background_size[1] / speed
self.background_size = background_size
self.position = [random.randint(0, background_size[0]-self.image.get_size()[0]), -self.image.get_size()[1]]
# Start self-destruct
self.start_destroy = False
# Self-destruct completed
self.destroyed = False
# Self-destruct image path
self.destroy_images = ['enemy1_down1.png'.'enemy1_down2.png'.'enemy1_down3.png'.'enemy1_down3.png']
# Time since the last image was drawn
self.time_passed = 0
# destrucy_images in self.destroy_images position
self.destroy_image_position = 0
def update(self, time_passed):
Update enemy location :param time_passed: Time since last drawing :return: ""
pass
Copy the code
- Realize the bullet class, complete the main operation of the bullet
# Aircraft bullets
class Bullet(a):
def __init__(self, image_path=os.path.join(source_dir,'bullet.png'), background_size=(480.700), plan=None, speed=1000):
":param image_path: bullet image address :param background_size: Game window size: param plan: plane object :param speed: bullet flying speed"
self.image = pygame.image.load(image_path).convert_alpha()
self.background_size = background_size
self.speed = background_size[1] / speed
# Whether bullets hit enemy aircraft
self.destroyed = False
self.position = self._get_position(plan)
def _get_position(self, plan):
Param plan: aircraft object ""
bullet_size = self.image.get_size()
plan_width = plan.image_size[0]
x = (plan_width-bullet_size[0) /2
return [plan.position[0] + x, plan.position[1]]
def update(self, time_passed):
"Change bullet position :param time_passed: Time since last drawing"
If the bullet goes off screen or hits enemy aircraft, set self.position[1] to -100 and remove it at plan.draw
if self.position[1] + self.image.get_size()[1] < =0 or self.destroyed:
self.position[1] = - 100.
return
# Change in distance = time * speed
self.position[1] -= time_passed * self.speed
Copy the code
Now that we’re done, we just need to use Game().run() to run our Game.
You can get the complete code and the package.