python实现超级玛丽游戏

小编给大家分享一下python实现超级玛丽游戏,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

我们优先介绍超级玛丽游戏中的多状态跳跃,和行走地图拖动的原理,然后实现。并实现倒计时和金币动态效果

python实现超级玛丽游戏

python实现超级玛丽游戏

python实现超级玛丽游戏

接下来用下面这四张图,就可以完全懂得游戏中背景是怎么会移动的。

图1

python实现超级玛丽游戏

图2

python实现超级玛丽游戏

图3

python实现超级玛丽游戏

图4

python实现超级玛丽游戏

由于代码是我前几年学习python的时候做的,代码写的很挤都整到一个源文件中,大家看的时候仔细。

然后上源代码:

#!/usr/bin/envpython
#-*-coding:utf-8-*-
importpygame,os,wx
fromrandomimportrandint
fromsysimportexit
frompygame.localsimport*
pygame.init()

defmain():
#获取屏幕大小
app=wx.App()
WHFRAMES=wx.DisplaySize()
WIDTH=int(WHFRAMES[0]*0.7)
HEIGHT=int(WHFRAMES[1]*0.8)
Timers=0#游戏定时器
TimersSec=0#秒
tim_psd=0
#获取屏幕大小
screen=pygame.display.set_mode((WIDTH,HEIGHT),0,32)
caption=pygame.display.set_caption("超级马里奥")
screen.fill([255,255,255])
mariofont=pygame.font.Font('fonts/poster.ttf',22)
mario_name=mariofont.render("MARIO",True,(84,65,190),None)
#Game_world=mariofont.render("WORLD",True,(84,65,190),None)
Game_moneyX=mariofont.render("X",True,(255,255,128),None)
Game_time=mariofont.render("TIME",True,(84,65,190),None)

money_ic5=pygame.image.load('images/PTModelSprite_ID21675.png')
money_ic5=pygame.transform.scale(money_ic5,(25,25))
money_ic6=pygame.image.load('images/PTModelSprite_ID21676.png')
money_ic6=pygame.transform.scale(money_ic6,(10,25))
money_ic7=pygame.image.load('images/PTModelSprite_ID21677.png')
money_ic7=pygame.transform.scale(money_ic7,(25,25))
money_ic8=pygame.image.load('images/PTModelSprite_ID21678.png')
money_ic8=pygame.transform.scale(money_ic8,(25,25))
money_timers=0#图片轮播定时器

Game_world=pygame.image.load('images/PTModelSprite_ID2478.png')

background=pygame.image.load('images/PTModelSprite_ID35342.png').convert_alpha()
background=pygame.transform.scale(background,(WIDTH,HEIGHT))

Roads=pygame.image.load('images/PTModelSprite_ID3790.png').convert_alpha()
Roads2=pygame.image.load('images/PTModelSprite_ID4224.png').convert_alpha()

hero=pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()
x,y=15,HEIGHT-200
inp_flag=-2#(stop:-1leftdrection,-2rightdrection),(walk:1rightdrection,2leftdrection)
times,times2=0,0#人物动作定时器
move_values,jump_values,jump_values2,jump_values3=12,0,0,0#一步移动的距离和跳跃的值1,2
jump_adder,jump_max_point=0,50#跳跃累加器用来累加按键的长短然后判断跳跃的高度,跳跃的初始值最高点
jump_flag=0
bg_w_1,bg_w_2=0,WIDTH-2#两张壁纸一前一后循环拖动的变量

#播放背景

#播放背景



#游戏信息数据定义
score=0
money=0
world=11
time=400
Gdata=[score,money,world,time]
#游戏信息数据定义

#初始化函数
defgame_initializaion(score,money,world,time,Gdata,TimersSec,Timers,x,y,inp_flag,times,times2,move_values,jump_values,jump_values2,jump_values3,jump_adder,jump_max_point,jump_flag,bg_w_1,bg_w_2,tim_psd):#数据初始化
#游戏初始化数据
inp_flag=-2#(stop:-1leftdrection,-2rightdrection),(walk:1rightdrection,2leftdrection)
x,y=15,HEIGHT-200#马里奥坐标
times,times2=0,0#人物动作定时器
move_values,jump_values,jump_values2,jump_values3=12,0,0,0#一步移动的距离和跳跃的值1,2
jump_adder,jump_max_point=0,50#跳跃累加器用来累加按键的长短然后判断跳跃的高度,跳跃的初始值最高点
jump_flag=0
tim_psd=0
bg_w_1,bg_w_2=0,WIDTH-2#两张壁纸一前一后循环拖动的变量
Timers=0#游戏定时器
score=0#开始分数
money=0#开始金钱
world=11#世界关卡第一关1-1=11
time=400#游戏总时间
TimersSec=0#游戏里的秒
Gdata=[score,money,world,time]
#游戏初始化数据
returnscore,money,world,time,Gdata,TimersSec,Timers,x,y,inp_flag,times,times2,move_values,jump_values,jump_values2,jump_values3,jump_adder,jump_max_point,jump_flag,bg_w_1,bg_w_2,tim_psd
#初始化函数

score,money,world,time,Gdata,TimersSec,Timers,x,y,inp_flag,times,times2,move_values,jump_values,jump_values2,jump_values3,jump_adder,jump_max_point,jump_flag,bg_w_1,bg_w_2,tim_psd=\
game_initializaion(score,money,world,time,Gdata,TimersSec,Timers,x,y,inp_flag,times,times2,move_values,jump_values,jump_values2,jump_values3,jump_adder,jump_max_point,jump_flag,bg_w_1,bg_w_2,tim_psd)#数据初始化主调函数

clock=pygame.time.Clock()
pygame.key.set_repeat(55)
pygame.display.flip()

defWalkAction(times,times2,inp_flag,hero):
#walkaction
ify<HEIGHT-200:#如果在空中为跳跃图片
ifinp_flag==1:#right
hero=pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()
ifinp_flag==2:#left
hero=pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()
hero=pygame.transform.flip(hero,True,False)
else:
ifinp_flag==1:#right
times+=2
iftimes<20:
hero=pygame.image.load('images/PTModelSprite_ID34256.png').convert_alpha()
eliftimes<20:
hero=pygame.image.load('images/PTModelSprite_ID34257..png').convert_alpha()
eliftimes<40:
hero=pygame.image.load('images/PTModelSprite_ID34258.png').convert_alpha()
eliftimes<60:
hero=pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()
eliftimes<80:
hero=pygame.image.load('images/PTModelSprite_ID34260.png').convert_alpha()
eliftimes<100:
hero=pygame.image.load('images/PTModelSprite_ID34261.png').convert_alpha()
eliftimes<120:
hero=pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()
eliftimes<140:
times=0
ifinp_flag==2:#left
times2+=2
iftimes2<20:
hero=pygame.image.load('images/PTModelSprite_ID34256.png').convert_alpha()
hero=pygame.transform.flip(hero,True,False)
eliftimes2<20:
hero=pygame.image.load('images/PTModelSprite_ID34257..png').convert_alpha()
hero=pygame.transform.flip(hero,True,False)
eliftimes2<40:
hero=pygame.image.load('images/PTModelSprite_ID34258.png').convert_alpha()
hero=pygame.transform.flip(hero,True,False)
eliftimes2<60:
hero=pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()
hero=pygame.transform.flip(hero,True,False)
eliftimes2<80:
hero=pygame.image.load('images/PTModelSprite_ID34260.png').convert_alpha()
hero=pygame.transform.flip(hero,True,False)
eliftimes2<100:
hero=pygame.image.load('images/PTModelSprite_ID34261.png').convert_alpha()
hero=pygame.transform.flip(hero,True,False)
eliftimes2<120:
hero=pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()
hero=pygame.transform.flip(hero,True,False)
eliftimes2<140:
times2=0
elifinp_flag==-1:
hero=pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()
hero=pygame.transform.flip(hero,True,False)
times2=0
elifinp_flag==-2:
hero=pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()
times2=0

returntimes,times2,inp_flag,hero


defHeroHeightIs():#判断角色是否为地面y轴
ify>=HEIGHT-200:
returnFalse
else:#这是在控制的状况
returnTrue


defReset_max_point(jump_max_point):#在地面重设默认跳跃的最高点(还原)
ify>=(HEIGHT-200):
jump_max_point=50#默认最高点是50
returnjump_max_point



defjump_leftScreenBgnotMove(x):
ifx<(WIDTH/4):
ifjump_max_point==50:
ifinp_flag==1:
x+=(2.7)
ifinp_flag==2:
x-=(2.7)
elifjump_max_point==100:
ifinp_flag==1:
x+=(0.27)
ifinp_flag==2:
x-=(0.27)
returnx

defScreen_MoneyIc(screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers):#绘制第二项金钱图标

money_timers+=1
ifmoney_timers<15:
screen.blit(money_ic5,(WIDTH*0.24,25))#绘制第二项金钱图标1
elifmoney_timers<40:
screen.blit(money_ic6,(WIDTH*0.24+7.5,25))#绘制第二项金钱图标2
elifmoney_timers<55:
screen.blit(money_ic7,(WIDTH*0.24,25))#绘制第二项金钱图标3
elifmoney_timers<80:
screen.blit(money_ic8,(WIDTH*0.24,25))#绘制第二项金钱图标4
else:
money_timers=0
returnscreen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers


defGame_Timers(TimersSec,Gdata,time_passed,tim_psd):#游戏定时器

tim_psd+=time_passed
iftim_psd>=1000:#为1秒的时候
TimersSec+=1
tim_psd=0
Gdata[3]=400-TimersSec#游戏所剩时间

returnTimersSec,Gdata,time_passed,tim_psd


whileTrue:

#事件检测
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
exit()

ifevent.type==KEYDOWN:
keys=pygame.key.get_pressed()
ifkeys[pygame.K_a]:
ifevent.key==K_wandinp_flag==0:
ify<=HEIGHT-200:#看y坐标必须在起点
jump_flag=3#按了上和向前
ify>=HEIGHT-200:#如果角色在平地才走动后退左
#ifbg_w_1==0:
#x-=5
x-=(move_values+3.5)
inp_flag=2

ifkeys[pygame.K_d]:
ifevent.key==K_wandinp_flag==0:
ify<=HEIGHT-200:#看y坐标必须在起点
jump_flag=2#按了上和向前
ify>=HEIGHT-200:#如果角色在平地才走动前景右
ifx<(WIDTH/4):#角色还在屏幕左边可移动
x+=(move_values+3.5)
inp_flag=1

ifkeys[pygame.K_w]:#jump
jump_flag=1#仅仅是按了跳跃
jump_adder+=1#跳跃累加器
ifevent.key==pygame.K_dand(jump_flag==1):
ify==HEIGHT-200:#看y坐标必须在起点
jump_flag=2#按了上和向前
ifevent.key==pygame.K_aand(jump_flag==1):
ify==HEIGHT-200:#看y坐标必须在起点
jump_flag=3#按了上和向后

ifkeys[pygame.K_p]:#重启
score,money,world,time,Gdata,TimersSec,Timers,x,y,inp_flag,times,times2,move_values,\
jump_values,jump_values2,jump_values3,jump_adder,jump_max_point,jump_flag,bg_w_1,bg_w_2,tim_psd=\
game_initializaion(score,money,world,time,Gdata,TimersSec,Timers,x,y,inp_flag,times,times2,\
move_values,jump_values,jump_values2,jump_values3,jump_adder,jump_max_point,jump_flag,bg_w_1,\
bg_w_2,tim_psd)


ifevent.type==KEYUP:
ifkeys[pygame.K_a]:
inp_flag=-1
ifkeys[pygame.K_d]:
inp_flag=-2
ifkeys[pygame.K_w]:
ifjump_adder<4:#如果松开按键没有达到jump_adder跳跃累加器的值(那么就他们置零)
jump_adder=0

##在地面时重设默认跳跃的最高点(还原)
jump_max_point=Reset_max_point(jump_max_point)

#jumpaction1
ifjump_flag==1:#只有跳跃
#让其他方式跳跃值为0
jump_values2=0
jump_values3=0
#------
#持续按键跳跃的结构
ifjump_adder>=4:
jump_max_point=100#第二次跳跃最大值
jump_adder=0
#------
jump_values+=1.25
ifjump_values<=jump_max_point:
y-=5

x=jump_leftScreenBgnotMove(x)

ifjump_max_point==100:#跳跃的高度不同y坐标的速度也要慢点
y+=1.5
x=jump_leftScreenBgnotMove(x)

elifjump_values<=jump_max_point+8:
pass
elifjump_values<=jump_max_point*2+8:
ifHeroHeightIs():#如果角色在控制就继续加y轴的值1
y+=5

x=jump_leftScreenBgnotMove(x)

ifjump_max_point==100:#跳跃的高度不同y坐标的速度也要慢点
y-=1.5
x=jump_leftScreenBgnotMove(x)

else:
y=HEIGHT-200
jump_flag=0
jump_values=0


#walldetection
ifx<=0:
x=0
ifx+hero.get_width()>WIDTH:
x=WIDTH-hero.get_width()


#角色的动作函数
times,times2,inp_flag,hero=WalkAction(times,times2,inp_flag,hero)

#1.bgmove---blit
screen.blit(background,(bg_w_2,0))
screen.blit(background,(bg_w_1,0))

#绘制信息

screen.blit(mario_name,(WIDTH*0.03,3))#绘制第一项名字

screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers=\
Screen_MoneyIc(screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers)#绘制第二项金钱图标

screen.blit(Game_moneyX,(WIDTH*0.28,24))#绘制第二项x
screen.blit(Game_world,(WIDTH*0.5-Game_world.get_width()/2,3))#绘制第三项世界地图
screen.blit(Game_time,(WIDTH*0.84,3))#绘制第四项游戏时间

forDATAiinrange(4):
Game_data=mariofont.render("%s"%Gdata[DATAi],True,(255,255,128),None)#综合绘制:分数金币关卡游戏时间
ifDATAi!=2:
screen.blit(Game_data,(WIDTH*(0.03+DATAi*0.27),24))
elifDATAi==2:
Game_data=mariofont.render("%s-%s"%(Gdata[DATAi]/10,Gdata[DATAi]%10),True,(255,255,128),None)#综合绘制:分数金币关卡游戏时间
screen.blit(Game_data,(WIDTH*0.5-Game_data.get_width()/2,15))

#绘制信息

#2.bgmove--panel
#ifinp_flag==2:#往左走壁纸向右拖动
#bg_w_1+=move_values/4
#bg_w_2+=move_values/4
ifinp_flag==1andx>=(WIDTH/4):#往右走壁纸向左拖动
bg_w_1-=(move_values/4-0.5)
bg_w_2-=(move_values/4-0.5)

ifbg_w_1>=0:
bg_w_1,bg_w_2=0,WIDTH-2
ifbg_w_1<-WIDTH:
bg_w_1,bg_w_2=0,WIDTH-2

screen.blit(hero,(x,y))
pygame.time.delay(2)#毫秒

time_passed=clock.tick()
TimersSec,Gdata,time_passed,tim_psd=Game_Timers(TimersSec,Gdata,time_passed,tim_psd)#游戏定时

pygame.display.update()

if__name__=='__main__':
main()

以上是“python实现超级玛丽游戏”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注恰卡编程网行业资讯频道!

发布于 2021-03-24 01:19:51
收藏
分享
海报
0 条评论
161
上一篇:python实现超级马里奥的案例 下一篇:python怎么实现用户名密码校验
目录

    推荐阅读

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码