前言
嗨喽~大家好呀,这里是魔王呐 ❤ ~!
本游戏完整源码、素材: 点击此处跳转文末名片获取
咳咳,又是一款新的小游戏,就是大家熟悉的王者~
来看我用python来实现高(di)配版的王者
是一款拿到代码运行后,可直接玩的游戏,是摸鱼必备的小游戏
小声说,我玩了一下午…
开发环境:
首先我们先来安装一下运行代码的软件(对没安装的小白说)
-
版 本: python 3.8
-
编辑器: pycharm 2022.3.2 专业版
必备素材
主要代码
导入模块
import pygame
import os.path
import csv
import setting as set
import live
import game_event
import gameui as gi
import startupui as si
安装第三方模块方法:win + R 输入cmd 输入安装命令 pip install 模块名
(如果你觉得安装速度比较慢, 你可以切换国内镜像源)
完整代码👉【点击文末名片领取】【或者看代码中数字】
程序主函数
def run_game():#初始化pygame库pygame.init()702813599 ### 源码领取#创建时钟对象(控制帧率)clock=pygame.time.Clock()#实例化设置类,用于导入游戏设置setting=set.Setting()#设置游戏窗口screen=pygame.display.set_mode((setting.screen_width,setting.screen_height))pygame.display.set_caption(setting.screen_caption)
设置不同的组,用于分别处理各种物品间的关系
#玩家组
group_player=pygame.sprite.Group()
#玩家的攻击组
group_attack=pygame.sprite.Group()
#敌人组
group_enemy=pygame.sprite.Group()
#敌人的攻击组
group_enemy_attack=pygame.sprite.Group()
实例化ui对象
#showinfo用于在游戏内显示人物血条等信息showinfo=gi.Info(setting,screen)#人物选择按钮yi_button=si.MonkeyKingButton(screen,setting)monkey_button=si.YiButton(screen,setting)fox_button=si.FoxButton(screen,setting)bin_button=si.BinButton(screen,setting)
游戏开始界面的按钮
pve_button=si.PVEButton(screen,setting)pvp_button=si.PVPButton(screen,setting)702813599 ### 源码领取endless_button=si.EndlessButton(screen,setting)control_button=si.ControlButton(screen,setting)memory_button=si.RecordButton(screen,setting)cooling_button=si.CoolingButton(screen,setting)
游戏背景
select_button=si.SelectButton(screen,setting)win_button=si.WinButton(screen,setting)dead_button=si.DeadButton(screen,setting)
玩家当前选择的人物标记
player_button_1=si.PlayerButton1(screen,setting)player_button_2=si.PlayerButton2(screen,setting)#空白按钮none_button=si.NoneButton(screen,setting)#空白图像none_info=gi.ExInfo(screen,none_button,setting.introduce_none)
介绍按钮作用的图像
pve_info=gi.ExInfo(screen,pve_button,setting.introduce_pve)pvp_info=gi.ExInfo(screen,pvp_button,setting.introduce_pvp)endless_info=gi.ExInfo(screen,endless_button,setting.introduce_endless)control_info=gi.ExInfo(screen,control_button,setting.introduce_control)record_info=gi.ExInfo(screen,memory_button,setting.introduce_record)cooling_info=gi.ExInfo(screen,cooling_button,setting.introduce_cooling)
按钮组(绘制时,在前的按钮会被在后的按钮覆盖)
buttons=[select_button,yi_button,monkey_button,fox_button,bin_button,pve_button,pvp_button,endless_button,cooling_button,control_button,memory_button,dead_button,win_button]
标签按钮组
choose_buttons=[player_button_1,player_button_2]
介绍按钮作用的图像组
button_info_dict={none_button:none_info,pve_button:pve_info,pvp_button:pvp_info,endless_button:endless_info,control_button:control_info,memory_button:record_info,cooling_button:cooling_info}#当前显示的图像列表info_label=[]#存储模拟刚体运动的列表rigidbody_list=[]#玩家实例,初始化为战士player_1=live.MonkeyKing(setting,screen)player_2=live.MonkeyKing(setting,screen)
702813599 ### 源码领取if not os.path.exists(setting.record_path):#如果游戏记录文件不存在就新创建一个with open(setting.record_path,'w',newline="") as f:writer=csv.writer(f)header=["Time","Mode","Winner","1st Score","2st Score","Duration(s)","1st Player","2nd Player","isCooling"]writer.writerow(header)
游戏主循环
while True: #绘制背景screen.blit(setting.screen_surface_background,(0,0))#设置游戏帧率clock.tick(setting.fps)#检测键盘鼠标事件 game_event.check_event(setting,screen,group_player,group_attack,group_enemy,group_enemy_attack,buttons,showinfo,button_info_dict,info_label)
更新当前选择人物的标签
game_event.update_choose(setting,buttons,choose_buttons)
游戏运行,非玩家对抗模式
if (setting.game_active and (setting.game_mode==0 or setting.game_mode==2)):
人物初始化
if(not setting.isinit):if setting.player_1!=None:player_1=setting.player_1group_player.add(player_1)if setting.player_2!=None:player_2=setting.player_2group_player.add(player_2) setting.isinit=True#游戏计时器setting.timer+=1#更新玩家group_player.update()#生成敌人game_event.generate_enemies(setting,group_enemy,screen)
更新敌人,玩家的攻击,敌人的攻击,玩家状态等
game_event.update_enemies(setting,showinfo,screen,group_player,group_enemy,group_attack,group_enemy_attack)game_event.update_attacks(setting,screen,group_attack,group_enemy,rigidbody_list)
702813599 ### 源码领取 game_event.update_enemy_attacks(setting,screen,group_player,group_enemy_attack,rigidbody_list)game_event.update_state(setting,showinfo)game_event.update_rigidbody(setting,rigidbody_list)
胜利条件
if setting.timer>=60*setting.fps and not group_enemy.spritedict and setting.game_mode==0:game_event.game_win(setting,showinfo,group_enemy,group_attack,group_enemy_attack)setting.timer=0
失败条件
if setting.isinit and ((setting.player_1!=None and setting.health_1<=0) or (setting.player_2!=None and setting.health_2<=0)): game_event.game_dead(setting,showinfo,group_enemy,group_attack,group_enemy_attack)setting.timer=0
玩家对抗模式
elif setting.game_active and setting.game_mode==1:
人物初始化
if(not setting.isinit):if setting.player_1!=None and setting.player_2!=None:player_1=setting.player_1group_player.add(player_1)player_2=setting.player_2group_player.add(player_2) setting.isinit=True
游戏计时器
setting.timer+=1
更新玩家
player_1.update()
player_2.update()
更新玩家的攻击,信息显示和物理模拟
game_event.update_attacks_pvp(setting,screen,group_attack,rigidbody_list)game_event.update_state(setting,showinfo)game_event.update_rigidbody(setting,rigidbody_list)
玩家1胜利条件
if setting.isinit and setting.health_2<=0:setting.score_1+=1game_event.game_win(setting,showinfo,group_enemy,group_attack,group_enemy_attack)setting.timer=0
玩家2胜利条件
if setting.isinit and setting.health_1<=0:setting.score_2+=1game_event.game_win(setting,showinfo,group_enemy,group_attack,group_enemy_attack)setting.timer=0
根据上述更新的结果绘制整个游戏窗口
game_event.update_screen(setting,screen,group_player,group_attack,group_enemy,group_enemy_attack,showinfo,buttons,info_label,choose_buttons) #运行游戏
run_game()
效果展示
好了,这就是我们的效果啦~技术就这么多将就看看吧 😎
尾语 💝
要成功,先发疯,下定决心往前冲!
学习是需要长期坚持的,一步一个脚印地走向未来!
未来的你一定会感谢今天学习的你。
—— 心灵鸡汤
本文章到这里就结束啦~感兴趣的小伙伴可以复制代码去试试哦 😝