大家好,我是黑哥喜欢欺负超人的那个黑哥
擅长领域:Python开发一个小游戏
今日重点:一步步分析and越过亚马逊的反爬虫机制
不一会,游戏写好了
效果是这样的
一、如何搭建开发环境环境
windows系统,python3.6+ pip21+
安装游戏依赖模块
pip install pygame
二、完整开发流程
1、代码结构
modules:自己定义的模块
——game.py:主程序
res:存放引用到的图片和音频等等
——music: 音频 资源
——imgs: 图片 资源
——fonts: 字体
cfg.py:为主配置文件
skiing.py:主程序文件
requirements.txt:需要引入的python依赖包
2、详细配置
cfg.py
配置文件中,需要引入os模块,并且配置打开游戏的屏幕大小。将我们需用到的图片、音频和字体文件全部引入到项目中。
'''配置文件'''
import os
'''FPS'''
FPS = 40
'''游戏屏幕大小'''
SCREENSIZE = (640, 640)
'''图片路径'''
SKIER_IMAGE_PATHS = [
os.path.join(os.getcwd(), 'res/imgs/game_forward.png'),
os.path.join(os.getcwd(), 'res/imgs/game_right1.png'),
os.path.join(os.getcwd(), 'res/imgs/game_right2.png'),
os.path.join(os.getcwd(), 'res/imgs/game_left2.png'),
os.path.join(os.getcwd(), 'res/imgs/game_left1.png'),
os.path.join(os.getcwd(), 'res/imgs/game_fall.png')
]
OBSTACLE_PATHS = {
'tree': os.path.join(os.getcwd(), 'res/imgs/tree.png'),
'flag': os.path.join(os.getcwd(), 'res/imgs/flag.png')
}
'''背景音乐路径'''
BGMPATH = os.path.join(os.getcwd(), 'res/music/bgm.mp3')
'''字体路径'''
FONTPATH = os.path.join(os.getcwd(), 'res/font/FZSTK.TTF')
3、动画中引用到的图片![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3bc50d5f42f1b7010fe084507768165c.png)
4、资源文件
包括游戏背景音频、图片和字体设计
res
music:游戏背景音乐
fonts:记分牌呀、显示呀的 相关字体
imgs:游戏动画里的静态图片,所谓动画都是图片
5、主程序
skiing.py
在主程序中,通过读取配置文件,引入项目资源:包括图片、音频等,并从我们的modules里引入所有我们的模块。
''' 类'''
class lexGame():
def __init__(self, screen, sounds, font, lex_imgs, cfg, **kwargs):
self.info = 'Gemlex —— hacklex'
self.screen = screen
self.sounds = sounds
self.font = font
self.lex_imgs = lex_imgs
self.cfg = cfg
self.reset()
'''开始 '''
def start(self):
clock = gamepy.time.Clock()
# 遍历整个 界面更新位置
overall_moving = True
# 指定某些对象个体更新位置
individual_moving = False
# 定义一些必要的变量
lex_selected_xy = None
lex_selected_xy2 = None
swap_again = False
add_score = 0
add_score_showtimes = 10
time_pre = int(time.time())
# 主循环
while True:
for event in gamepy.event.get():
if event.type == gamepy.QUIT or (event.type == gamepy.KEYUP and event.key == gamepy.K_ESCAPE):
gamepy.quit()
sys.exit()
elif event.type == gamepy.MOUSEBUTTONUP:
if (not overall_moving) and (not individual_moving) and (not add_score):
position = gamepy.mouse.get_pos()
if lex_selected_xy is None:
lex_selected_xy = self.checkSelected(position)
else:
lex_selected_xy2 = self.checkSelected(position)
if lex_selected_xy2:
if self.swapGem(lex_selected_xy, l