当前位置:网站首页>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 !
边栏推荐
- Knowledge map pyhanlp realizes named body recognition (with named body recognition code)
- Log4j epic loopholes, big companies like jd.com have been recruited
- Let's move forward together, the 10th anniversary of Google play!
- Quick access to website media resources
- 微信小程序多文件上传
- LED带风扇护眼学习台灯触摸芯片-DLT8S12A
- JDBC learning day1:jdbc
- Meituan Er Mian: why does redis have sentinels?
- rsa加解密(兼容微信小程序环境)
- RuntimeError: output with shape [1, 256, 256] doesn‘t match the broadcast shape [3, 256, 256]【报错】
猜你喜欢

怎么会不喜欢呢,CI/CD中轻松发送邮件

家用静音驱蚊灯芯片-DLTAP703SD-杰力科创

知识图谱 — pyhanlp实现命名体识别(附命名体识别代码)

Visual Studio Code安装教程(超详细)

多功能无线遥控艾灸仪芯片-DLTAP703SD

20000 words + 30 pictures | what's the use of chatting about MySQL undo log, redo log and binlog?

2021.7.12 internal and external connection of notes

2021.7.18笔记 mysql数据类型

Visual studio code installation tutorial (super detailed)

Canvas draws graphics according to coordinate points
随机推荐
Login page tablelayout
Wechat applet multi file upload
智能失眠治疗仪产品-DLT8P68SA-杰力科创
微信小程序获取openId, sessionKey, unionId
2021.7.31笔记 视图
2021.8.9 note request
Meituan Er Mian: why does redis have sentinels?
The second parameter of fragmenttransaction.replace reports an error
图文结合,完美解释MySQL逻辑备份的实现流程
The song of the virtual idol was originally generated in this way!
Idea packaging war package and war package location
Wechat applet wechat payment overview
USB充电式暖手宝芯片-DLTAP602SC-杰力科创
Knowledge map - Jieba, pyhanlp, smoothnlp tools to realize Chinese word segmentation (part of speech)
兆骑科创海内外引进高层次人才,创新创业项目对接
mysql基础语句
"MySQL things" explains the indexing principle in detail
RSA encryption and decryption (compatible with wechat applet environment)
rsa加解密(兼容微信小程序环境)
2021.8.7 note Servlet