Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a Wechat airplane war with Python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use Python to write a Wechat aircraft war, I hope you will learn something after reading this article, let's discuss it together!

These days I am reliving the plane war of Wechat and Mini Game. I am thinking about life while playing. How can this plane war be done so well? it is easy to operate and easy to use.

Help squatting toilet, YP, rice circle girls can have one thing to cheer them up when they are bored! Let their left / right hands move back and forth rhythmically and rhythmically in the same direction!

This is an epic invention, a rich and colorful stroke, is …...

After a burst of convulsions, I ended the game and instantly felt that everything was dull. As I entered the sage mode, I suddenly thought, wouldn't it be beautiful if I could make more people feel this beauty in different ways?

So I turned on the computer and created a plan_game.py.

Take a look at the effect picture first.

Operating environment

Operating system: windows10

Python version: python 3.7

Code Editor: pycharm 2018.2

Usage module: os,sys,random,pygame

Because the implementation code uses a third-party module of pygame, there is no first pip install, here by the way to provide a better pygame tutorial.

Https://eyehere.net/2011/python-pygame-novice-professional-index/

Concrete realization

First of all, let's specify the file directory of the material file. It is convenient for us to use behind.

Import os # gets the path of the material_images directory under the current folder source_dir = os.path.join (os.path.dirname (os.path.abspath (_ _ file__)), 'material_images')

Implement a Game class that completes the main logic of the game.

Import pygame

Class Game ():

Def _ _ init__ (self, background_image_path, size= (480,700), title=' aircraft Battle', font_name=' founder body', font_size=30, speed=2000):

''

: param background_image_path: path address of the background image

Param size: the size of the game window

: param title: title of the game window

: param font_name: specify font

: param font_size: specify font size

: param speed: the time it takes for the background image to scroll the entire window at 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 font that comes with the system

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

# using the height of the window for scrolling time, you can get the scrolling distance per ms

Self.every_ms_move_distance = self.size [1] / speed # 2 seconds

# Scor

Self.score = 0

# store all enemy aircraft

Self.enemies = []

Def show_score (self):

''

Displays the score, with 10px at the top of the window, centered left and right

''

Pass

Def set_time_passed (self):

# Control the frame of the drawing, the bigger the faster

# get the time since the last time the image was drawn, ms

Self.time_passed = self.clock.tick ()

Def draw_background (self):

''

Draw the background picture and scroll down to create the feeling that the plane is flying all the way up.

''

# distance per move = distance per ms * Last to present time (ms)

Pass

Def create_enemy (self, image_path=os.path.join (source_dir,'enemy1.png'), enemy_number=5):

''

Create enemy aircraft

: param image_path: the image address of the enemy plane

Param enemy_number: there are at most several enemy planes on the screen.

''

Pass

Def draw_enemies (self, time_passed, screen):

''

Draw the enemy plane to the screen and clean up the enemy plane running out of the window

: param time_passed: the time elapsed the last time the guide was drawn

: param screen: drawn window object

''

Pass

Def bullet_and_enemy_crash_detection (self, bullets):

''

Check if the bullet hit the enemy plane.

Param bullets: all the bullets from the plane

''

Pass

Def plan_and_enemy_crash_detection (self, plan, allow_crash_size=None):

''

Check whether the enemy plane collides with the plane.

: param plan: aircraft object

: param allow_crash_size: allow the size of the aircraft collision, only left and right valid

''

Pass

Def draw_plan (self, plan, time_passed):

''

Draw an aircraft

: param plan: aircraft object

: param time_passed: since the last time it was painted

: return:

''

Pass

Def game_over (self):

''

Game over

''

While True:

# draw background image

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 aircraft object

Plan = Plan ()

While True:

# if the plane self-destructs and the game is over, call the game_over function

Pass

# detect listening events

Pass

# detect mobile cases up and down.

# wrecoery a dint and upper, lower, left and right are all fine

# then execute the plan.update function to change the position of the plane

Pass

# collision detection between bullets and enemy aircraft

Self.bullet_and_enemy_crash_detection (plan.bullets)

# collision detection between aircraft and enemy aircraft

Self.plan_and_enemy_crash_detection (plan)

# set the value of the attribute time_passed to the last time, which is convenient for later use

Self.set_time_passed ()

# draw a background picture

Self.draw_background ()

# display score

Self.show_score ()

# generate enemy aircraft

Self.create_enemy ()

# drawing enemy aircraft

Self.draw_enemies (time_passed=self.time_passed, screen=self.screen)

# drawing airplanes

Self.draw_plan (plan=plan, time_passed=self.time_passed)

# draw bullets

Plan.draw_bullets (time_passed=self.time_passed, screen=self.screen)

# display our image

Pygame.display.update ()

Here is how to check which fonts are included in your system.

Pygame.font.get_fonts (), this function can get all the native font files in the system. However, when we have Chinese in the game, we also have to choose fonts that support Chinese, otherwise Chinese will not be displayed.

Implement the DestroyAnimationMixin class, which is mainly used to display self-destruct animations of aircraft or enemy aircraft

# Mixin class showing aircraft self-destruct animation, can be used for aircraft and enemy aircraft self-destruct animation display class DestroyAnimationMixin (): def show_destroy_animation (self, time_passed, destroy_time=200):''show self-destruct animation is actually a few pictures switch faster, our eyes can not recognize, so we think it is dynamic That is, animation: param time_passed: the time from the last time the image was drawn to the present, unit ms: param destroy_time: total display time of self-destruct animation, unit ms''# because we have four self-destruct images, which need to be displayed in turn. First, the effect of animation # self.destroy_image_position represents the chapter of self-destruct picture. Start from zero # if it is greater than or equal to 4, the display of the self-destruct animation is complete Set the self.destroyed variable to True, so it is convenient to call if self.destroy_image_position > = 4: self.destroyed = True return # to load self-destructing images if self.time_passed > = destroy_time / 4: self.image = pygame.image.load (os.path.join (source_dir) Self.destroy_ images [self.pictures _ image_position]) .convert_alpha () self.destroy_image_position + = 1 self.time_passed = 0 else: self.time_passed + = time_passed

Realize the aircraft class and complete the main operation of the aircraft. Aircraft operations include: aircraft position, aircraft bullets, firing bullets and so on.

# aircraft class, inheriting DestroyAnimationMixin It is convenient to use the function class Plan (DestroyAnimationMixin) that displays self-destructing animation: def _ _ init__ (self, image_path=os.path.join (source_dir,'plan.png'), background_size= Param image_path: plane picture 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] # distance the aircraft moves each time self.every_time_move_distance = 0.5 # bullets of the aircraft self.bullets = [] # destroy association attributes Self-destruct related attribute # start self-destruct self.start_destroy = False # self-destruct end self.destroyed = False # self-destruct picture self.destroy_images = ['me_destroy_1.png',' me_destroy_2.png', 'me_destroy_3.png' 'me_destroy_4.png'] # self-destruct picture position self.destroy_image_position = 0 # time from the last time the image was drawn to now self.time_passed = 0 def update (self Direction):''update aircraft position: param direction: aircraft movement direction' 'pass def shut (self, image_path=os.path.join (source_dir) Bullet.png'):''aircraft firing bullets: param image_path: pictures of bullets' 'pass def draw_bullets (self, time_passed) Screen):''draw all the bullets of the plane: param time_passed: time since the last image was drawn: param screen: which window to draw to' 'pass

Realize the class of enemy aircraft and complete the main operation of enemy aircraft. Mainly used to update the location.

# enemy plane class, inherits DestroyAnimationMixin, makes it easy to use the function class Enemy (DestroyAnimationMixin) that displays self-destructing animation: def _ _ init__ (self, image_path=os.path.join (source_dir, 'enemy1.png'), speed=2000, background_size= (480,700)):'': param image_path: enemy plane Picture address: param speed: time it takes for the enemy plane to move the entire window, unit ms That is, speed: param background_size: size of the game window''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 complete self.destroyed = False # self-destruct picture path self.destroy_images = ['enemy1_down1.png',' enemy1_down2.png', 'enemy1_down3.png' 'enemy1_down3.png'] # from the time when the image was last drawn to the present time self.time_passed = 0 # the position of the self-destruct picture in self.destroy_images self.destroy_image_position = 0 def update (self Time_passed):''update the location of the enemy plane: param time_passed: time since the last time the image was drawn: return:' 'pass

Realize the bullet class and complete the main operation of the bullet.

# aircraft bullet class Bullet (): def _ init__ (self, image_path=os.path.join (source_dir,'bullet.png'), background_size= (480,700), plan=None Speed=1000):'': param image_path: picture of the bullet address: param background_size: game window size: param plan: aircraft 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 the bullet hit the enemy plane self.destroyed = False self.position = self._get_position (plan) def _ get_position (self Plan): according to plan, get the location of the bullet: 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 the position of the bullet: param time_passed: the time since the last time the image was drawn' if the bullet goes beyond the screen or hits the enemy plane Set self.position [1] to-100and remove it when plan.draw if self.position [1] + self.image.get_size () [1]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report