The example in this article shares the specific code of python to realize the WeChat airplane game for your reference. The specific content is as follows
import pygame
import random
import sys
# initialization
pygame.init()
pygame.display.set_caption('Plane Rush')#Set window title
screen= pygame.display.set_mode((320,570),0,32)
pygame.mouse.set_visible(False)#Hide cursor
# Load picture
boom1=pygame.image.load('../Game/fly/src/boom1.png')
boom2=pygame.image.load("../Game/fly/src/boom2.png")
bullet=pygame.image.load('../Game/fly/src/bullet.png')
plane=pygame.image.load("../Game/fly/src/plane.png").convert_alpha()
enemy=pygame.image.load('../Game/fly/src/enemy.png')
# Set window icon
pygame.display.set_icon(plane)
background=pygame.image.load("../Game/fly/src/bg_01.png")
start=pygame.image.load('../Game/fly/src/start.png')
pause=pygame.image.load("../Game/fly/src/pause.png")
# pygame.mixer.init()
# Xexplosion=pygame.mixer.Sound("explosion.mp3")
# Xbullet=pygame.mixer.Sound("../Game/fly/sound/bullet.mp3")
# XgameOver=pygame.mixer.Sound("../Game/fly/sound/game_over.mp3")
# Xexplosion.set_volume(1)
# pygame.mixer.music.load("../Game/fly/sound/game_music.mp3")
# pygame.mixer.music.play(-1,0.0)
# pygame.mixer.music.set_volume(0.25)
i=0
# fraction
score=0
font=pygame.font.SysFont('Microsoft Yahei',36)
# bullet
bullets=[]
# Enemy plane
enemies=[]
# Record the explosion location of the enemy aircraft
boomplace=[]
# game over
gameover=False
pygame.mouse.set_pos(200,200)while True:
i +=1if i 200:
i =0
screen.blit(background,(0,0))
# Control the aircraft by mouse
x, y=pygame.mouse.get_pos()
# print(x, y)
x -= plane.get_width()/2
y -= plane.get_height()/2
screen.blit(plane,(x, y))
# Called every 25 frames
if i%25==0:
# Return, press left button, middle button, right button
l,m,r=pygame.mouse.get_pressed()if l ==True:
# Add bullet position
bullets.append([x+plane.get_width()/2, y])
# Xshoot.play()for place in bullets:
# Bullet position is out of bounds
if place[1]<=0:
bullets.remove(place)
# Move the bullet position
for place in bullets:
place[1]-=55
# Draw bullet
for place in bullets:
screen.blit(bullet,(place[0],place[1]))
# The bandit spawns and disappears
if i%199==0:
enemies.append([random.randint(0,320-enemy.get_width()),-enemy.get_width()/2])for place in enemies:if place[1]=600:
enemies.remove(place)for place in enemies:
place[1]+=35for place in enemies:
screen.blit(enemy,(place[0],place[1]))
# Enemy plane explosion
for place in boomplace:if place[2]0:
screen.blit(boom1,(place[0],place[1]))
place[2]-=1
# Bullet collision detection
for bulletplace in bullets:for enemyplace in enemies:if(bulletplace[0] enemyplace[0] and bulletplace[0]< enemyplace[0]+ enemy.get_width()) and \
( bulletplace[1] enemyplace[1] and bulletplace[1]< enemyplace[1]+ enemy.get_height()):
boomflag =75
enemyplace.append(boomflag)
boomplace.append(enemyplace)
enemies.remove(enemyplace)
bullets.remove(bulletplace)
# Sexplosion.play()
score +=1
# Aircraft collision detection
for enemyplace in enemies:if(x +0.7*plane.get_width() enemyplace[0])and(x +0.3*plane.get_width()<enemyplace[0]+ enemy.get_width())\
and(y +0.7*plane.get_height() enemyplace[1])and(y +0.3*plane.get_height()<enemyplace[1]+ enemy.get_height()):
enemies.remove(enemyplace)
screen.blit(pause,(0,0))
screen.blit(boom2,(x, y))
# for place in bullets:
# screen.blit(bullet,(place[0], place[1]))
# for place in enemies:
# screen.blit(enemy,(place[0], place[1]))
text=font.render('Score%d'%score,True,(0,0,0))
screen.blit(text,(200,270))
text=font.render("Right ReStart",True,(0,0,0))
screen.blit(text,(30,310))
pygame.display.update()#Redraw the window
gameover=True
while gameover == True and r == False :
l, m, r = pygame.mouse.get_pressed()for event in pygame.event.get():if event.type == pygame.QUIT:
pygame.quit()exit()
# Reset game
i=0
score=0
bullets=[]
enemies=[]
boomplace=[]
x, y=185-plane.get_width()/2,550- plane.get_height()/2
# Test pause and continue
l,m,r=pygame.mouse.get_pressed()if r == True:
screen.blit(background,(0,0))
screen.blit(start,(0,0))
screen.blit(plane,(x,y))for place in bullets:
screen.blit(bullet,(place[0],place[1]))for place in enemies:
screen.blit(enemy,(place[0],place[1]))for place in boomplace:if place[2]0:
screen.blit(boom1,(place[0],place[1]))
place[2]-=1
text=font.render(u'Score %d'%score,1,(0,0,0))
screen.blit(text,(50,0))if gameover == True:
x,y =185,550
gameover =False
text =font.render(' left to start',True,(0,0,0))
screen.blit(text,(35,300))else:
x,y=pygame.mouse.get_pos()
text=font.render(' left to continue ',True,(0,0,0))
screen.blit(text,(10,300))
pygame.display.update()while True :for event in pygame.event.get():if event.type == pygame.QUIT:
pygame.quit()exit()
l, m, r = pygame.mouse.get_pressed()if l == True:
pygame.mouse.set_pos(x,y)break
text=font.render(u"%d"%score, True,(0,0,0))
screen.blit(text,(50,0))for event in pygame.event.get():if event.type == pygame.QUIT:
pygame.quit()exit()
Effect picture:
For more wonderful articles about python games, please click to view the following topics:
python tetris game collection
Python classic games summary
Python WeChat Jump Jump Game Collection
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts