当前位置:网站首页>PyGame aircraft war game background implementation
PyGame aircraft war game background implementation
2022-07-27 18:46:00 【Rise from the road】
Aircraft war game background
List of articles
One . Thought analysis
1. The goal is
The idea of background alternating rolling is determined
Show game background
1. The idea of background alternating rolling is determined
function Lesson preparation code , Observe Display effect of background image :- After the game starts , background image Meeting Continuously Down Move
- stay Visually The plane that produces heroes keeps flying upward illusion —— The routine commonly used in many Parkour games
- The background of the game Constantly changing
- The protagonist of the game The position remains the same
2. Analysis of implementation ideas
- Create two background images
- The first 1 Zhang Completely coincident with the screen
- The first 2 Zhang Zai Right above the screen
- Two images Move downward together
- self.rect.y += self.speed
- When Any background sprite Of rect.y >= The height of the screen That has been Move to the bottom of the screen
- take Move to the image at the bottom of the screen Set to Right above the screen
- rect.y = -rect.height
3. Design background class
- Initialization method
- Direct designation Background image
- is_alt Determine whether it is another image
- False Express The first image , Need to coincide with the screen
- True Express Another image , Right above the screen
- update() Method
- Judge Whether to move out of the screen , If it is , Set the image to Right above the screen , So as to achieve Alternate scrolling
Inherit If the method provided by the parent class , Can't meet the needs of subclasses :
- Derive a subclass
- For specific requirements in subclasses , Override parent method , And expand
Two . Show game background
1 . The basic implementation of background wizard
- stay plane_sprites newly build Background Inherited from GameSprite
class Background(GameSprite):
""" Game background wizard """
def update(self):
# 1. Call the method implementation of the parent class
super().update()
# 2. Determine whether to move out of the screen , If you move out of the screen , Set the image to the top of the screen
if self.rect.y >= SCREEN_RECT.height:
self.rect.y = -self.rect.height
2 . stay plane_main.py Show background sprites in
- stay __create_sprites Create in method spirit and Spirit group
- stay __update_sprites In the method , Give Way Spirit group call update() and draw() Method
__create_sprites Method
def __create_sprites(self):
# Create background sprites and Sprite groups
bg1 = Background("./images/background.png")
bg2 = Background("./images/background.png")
bg2.rect.y = -bg2.rect.height
self.back_group = pygame.sprite.Group(bg1, bg2)
__update_sprites Method
def __update_sprites(self):
self.back_group.update()
self.back_group.draw(self.screen)
3、 ... and . Use the initialization method , Simplify the creation of background sprites
reflection —— What kind of problems exist in the code completed in the previous summary ? Can it be simplified ?
- In the main program , Create two background sprites , Passed in the same image file path
- establish the second Background Genie when , In the main program , Set the image position of the background sprite
reflection —— spirit initial position Set up , should The main program is responsible for ? still The elves themselves are responsible for ?
answer —— The elves themselves are responsible for
According to the principle of object-oriented design , The responsibility of the object should be , Encapsulated inside the code of the class
Try to simplify the code call of the program caller
Initialization method
- Direct designation Background image
- is_alt Determine whether it is another image
- False Express The first image , Need to coincide with the screen
- True Express Another image , Right above the screen
stay plane_sprites.py To realize Background Of Initialization method
def __init__(self, is_alt=False):
image_name = "./images/background.png"
super().__init__(image_name)
# Determine whether to alternate pictures , If it is , Set the picture to the top of the screen
if is_alt:
self.rect.y = -self.rect.height
modify plane_main Of __create_sprites Method
# Create background sprites and Sprite groups
bg1 = Background()
bg2 = Background(True)
self.back_group = pygame.sprite.Group(bg1, bg2)
Four . Summary
This chapter outlines the construction of the background of the aircraft war game , Use object-oriented thinking to realize , Generally speaking, it's not difficult , Just some grammar to remember , It will be reused later .
Where are the deficiencies or better suggestions , Welcome to make complaints about Tucao , If you don't understand anything, you can confide in me , I will reply one by one. , Thank you for your approval , Thank you for your support !
边栏推荐
- USB充电式暖手宝芯片-DLTAP602SC-杰力科创
- 常用词词性表
- An analysis of CPU explosion of a smart logistics WCS system in.Net
- Solution to invalid SQL Server connection to server
- Let's move forward together, the 10th anniversary of Google play!
- Wechat applet wechat payment overview
- RSA encryption and decryption (compatible with wechat applet environment)
- 输入框blur事件与click事件冲突问题
- I was forced to optimize the API gateway query interface
- Must the MySQL query column be consistent with the group by field?
猜你喜欢

nacos显示服务注册地址错误

Generate PDM file from Navicat export table

Random talk on GIS data (V) - geographic coordinate system

Log4j 史诗级漏洞,京东这样的大厂都中招了

USB充电式暖手宝芯片-DLTAP602SC-杰力科创

这样的API网关查询接口优化,我是被迫的

Error launching IDEA

「MySQL那些事」一文详解索引原理

Must the MySQL query column be consistent with the group by field?

LED学习护眼台灯触摸芯片-DLT8T10S-杰力科创
随机推荐
Have you ever stumbled on MySQL's order by
阿里架构师耗时280个小时整理的1015页分布式全栈小册,轻松入手分布式系统
网红RGB镜子灯触摸芯片-DLT8S15B-杰力科创
EN 1155 building hardware swing door opener - CE certification
阿里p8总结的10条 SQL 优化方案(非常实用)
Installation and deployment of zabbix6.0
Knowledge map - Jieba, pyhanlp, smoothnlp tools to realize Chinese word segmentation (part of speech)
File path read
"MySQL things" explains the indexing principle in detail
Set the arc button to be displayed in the middle
使用jieba、pyhanlp工具实现关键字词句的提取
内网的公司邮箱服务器怎么发外部邮件
2021.7.31 note view
Why don't you like it? It's easy to send mail in ci/cd
Modify placeholder style in input
RSA encryption and decryption (compatible with wechat applet environment)
2021.7.12 internal and external connection of notes
Visual studio code installation tutorial (super detailed)
机器学习分类任务效果评估指标大全(包含ROC和AUC)
2021.8.9笔记 request