当前位置:   article > 正文

python:点击就能玩的超级玛丽小游戏(附有源码)_马里奥链接点开即玩

马里奥链接点开即玩

程序代码

  1. import pygame,os,wx
  2. from random import randint
  3. from sys import exit
  4. from pygame.locals import *
  5. pygame.init()
  6. def main():
  7. #获取屏幕大小
  8. app=wx.App()
  9. WHFRAMES=wx.DisplaySize()
  10. WIDTH=int(WHFRAMES[0]*0.7)
  11. HEIGHT=int(WHFRAMES[1]*0.8)
  12. Timers = 0 #游戏定时器
  13. TimersSec = 0 #秒
  14. tim_psd = 0
  15. #获取屏幕大小
  16. screen=pygame.display.set_mode((WIDTH,HEIGHT),0,32)
  17. caption=pygame.display.set_caption("超级马里奥")
  18. screen.fill([255,255,255])
  19. mariofont = pygame.font.Font('fonts/poster.ttf',22)
  20. mario_name = mariofont.render("MARIO",True,(84,65,190),None)
  21. #Game_world = mariofont.render("WORLD",True,(84,65,190),None)
  22. Game_moneyX = mariofont.render("X",True,(255,255,128),None)
  23. Game_time = mariofont.render("TIME",True,(84,65,190),None)
  24. money_ic5 = pygame.image.load('images/PTModelSprite_ID21675.png')
  25. money_ic5 = pygame.transform.scale(money_ic5, (25, 25))
  26. money_ic6 = pygame.image.load('images/PTModelSprite_ID21676.png')
  27. money_ic6 = pygame.transform.scale(money_ic6, (10, 25))
  28. money_ic7 = pygame.image.load('images/PTModelSprite_ID21677.png')
  29. money_ic7 = pygame.transform.scale(money_ic7, (25, 25))
  30. money_ic8 = pygame.image.load('images/PTModelSprite_ID21678.png')
  31. money_ic8 = pygame.transform.scale(money_ic8, (25, 25))
  32. money_timers = 0 #图片轮播定时器
  33. Game_world = pygame.image.load('images/PTModelSprite_ID2478.png')
  34. background = pygame.image.load('images/PTModelSprite_ID35342.png').convert_alpha()
  35. background = pygame.transform.scale(background, (WIDTH, HEIGHT))
  36. Roads = pygame.image.load('images/PTModelSprite_ID3790.png').convert_alpha()
  37. Roads2 = pygame.image.load('images/PTModelSprite_ID4224.png').convert_alpha()
  38. hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()
  39. x,y = 15,HEIGHT-200
  40. inp_flag = -2 #(stop:-1 left drection ,-2 right drection) ,(walk:1 right drection ,2 left drection)
  41. times,times2 = 0,0 #人物动作定时器
  42. move_values,jump_values,jump_values2,jump_values3 = 12,0,0,0 #一步移动的距离 和 跳跃的值1,2
  43. jump_adder,jump_max_point = 0,50 #跳跃累加器 用来累加按键的长短 然后判断跳跃的高度,跳跃的初始值最高点
  44. jump_flag = 0
  45. bg_w_1,bg_w_2 = 0,WIDTH-2 #两张壁纸 一前一后循环拖动的变量
  46. #播放背景
  47. #播放背景
  48. #游戏信息数据定义
  49. score = 0
  50. money = 0
  51. world = 11
  52. time = 400
  53. Gdata = [score,money,world,time]
  54. #游戏信息数据定义
  55. #初始化函数
  56. def 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):#数据初始化
  57. #游戏初始化数据
  58. inp_flag = -2 #(stop:-1 left drection ,-2 right drection) ,(walk:1 right drection ,2 left drection)
  59. x,y = 15,HEIGHT-200 #马里奥坐标
  60. times,times2 = 0,0 #人物动作定时器
  61. move_values,jump_values,jump_values2,jump_values3 = 12,0,0,0 #一步移动的距离 和 跳跃的值1,2
  62. jump_adder,jump_max_point = 0,50 #跳跃累加器 用来累加按键的长短 然后判断跳跃的高度,跳跃的初始值最高点
  63. jump_flag = 0
  64. tim_psd = 0
  65. bg_w_1,bg_w_2 = 0,WIDTH-2 #两张壁纸 一前一后循环拖动的变量
  66. Timers = 0 #游戏定时器
  67. score = 0 #开始分数
  68. money = 0 #开始金钱
  69. world = 11 #世界关卡第一关1-1 = 11
  70. time = 400 #游戏总时间
  71. TimersSec = 0 #游戏里的秒
  72. Gdata = [score,money,world,time]
  73. #游戏初始化数据
  74. return 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
  75. #初始化函数
  76. 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 = \
  77. 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)#数据初始化主调函数
  78. clock = pygame.time.Clock()
  79. pygame.key.set_repeat(55)
  80. pygame.display.flip()
  81. def WalkAction(times,times2,inp_flag,hero):
  82. #walk action
  83. if y < HEIGHT -200: #如果在空中 为跳跃图片
  84. if inp_flag == 1: #right
  85. hero = pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()
  86. if inp_flag == 2: #left
  87. hero = pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()
  88. hero = pygame.transform.flip(hero, True, False)
  89. else:
  90. if inp_flag == 1: #right
  91. times += 2
  92. if times < 20:
  93. hero = pygame.image.load('images/PTModelSprite_ID34256.png').convert_alpha()
  94. elif times < 20:
  95. hero = pygame.image.load('images/PTModelSprite_ID34257..png').convert_alpha()
  96. elif times < 40:
  97. hero = pygame.image.load('images/PTModelSprite_ID34258.png').convert_alpha()
  98. elif times < 60:
  99. hero = pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()
  100. elif times < 80:
  101. hero = pygame.image.load('images/PTModelSprite_ID34260.png').convert_alpha()
  102. elif times < 100:
  103. hero = pygame.image.load('images/PTModelSprite_ID34261.png').convert_alpha()
  104. elif times < 120:
  105. hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()
  106. elif times < 140:
  107. times = 0
  108. if inp_flag == 2: #left
  109. times2 += 2
  110. if times2 < 20:
  111. hero = pygame.image.load('images/PTModelSprite_ID34256.png').convert_alpha()
  112. hero = pygame.transform.flip(hero, True, False)
  113. elif times2 < 20:
  114. hero = pygame.image.load('images/PTModelSprite_ID34257..png').convert_alpha()
  115. hero = pygame.transform.flip(hero, True, False)
  116. elif times2 < 40:
  117. hero = pygame.image.load('images/PTModelSprite_ID34258.png').convert_alpha()
  118. hero = pygame.transform.flip(hero, True, False)
  119. elif times2 < 60:
  120. hero = pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()
  121. hero = pygame.transform.flip(hero, True, False)
  122. elif times2 < 80:
  123. hero = pygame.image.load('images/PTModelSprite_ID34260.png').convert_alpha()
  124. hero = pygame.transform.flip(hero, True, False)
  125. elif times2 < 100:
  126. hero = pygame.image.load('images/PTModelSprite_ID34261.png').convert_alpha()
  127. hero = pygame.transform.flip(hero, True, False)
  128. elif times2 < 120:
  129. hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()
  130. hero = pygame.transform.flip(hero, True, False)
  131. elif times2 < 140:
  132. times2 = 0
  133. elif inp_flag == -1:
  134. hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()
  135. hero = pygame.transform.flip(hero, True, False)
  136. times2 = 0
  137. elif inp_flag == -2:
  138. hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()
  139. times2 = 0
  140. return times,times2,inp_flag,hero
  141. def HeroHeightIs(): #判断角色是否为地面y轴
  142. if y >= HEIGHT-200:
  143. return False
  144. else: #这是在控制的状况
  145. return True
  146. def Reset_max_point(jump_max_point): #在地面重设默认跳跃的最高点(还原)
  147. if y >= (HEIGHT-200):
  148. jump_max_point = 50 #默认最高点是 50
  149. return jump_max_point
  150. def jump_leftScreenBgnotMove(x):
  151. if x<(WIDTH/4):
  152. if jump_max_point == 50 :
  153. if inp_flag == 1:
  154. x+=(2.7)
  155. if inp_flag == 2:
  156. x-=(2.7)
  157. elif jump_max_point == 100 :
  158. if inp_flag == 1:
  159. x+=(0.27)
  160. if inp_flag == 2:
  161. x-=(0.27)
  162. return x
  163. def Screen_MoneyIc(screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers) : #绘制第二项 金钱图标
  164. money_timers += 1
  165. if money_timers < 15 :
  166. screen.blit(money_ic5,(WIDTH*0.24,25)) #绘制第二项 金钱图标1
  167. elif money_timers < 40 :
  168. screen.blit(money_ic6,(WIDTH*0.24+7.5,25)) #绘制第二项 金钱图标2
  169. elif money_timers < 55 :
  170. screen.blit(money_ic7,(WIDTH*0.24,25)) #绘制第二项 金钱图标3
  171. elif money_timers < 80 :
  172. screen.blit(money_ic8,(WIDTH*0.24,25)) #绘制第二项 金钱图标4
  173. else:
  174. money_timers = 0
  175. return screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers
  176. def Game_Timers(TimersSec,Gdata,time_passed,tim_psd) : #游戏定时器
  177. tim_psd += time_passed
  178. if tim_psd >= 1000 : #为1秒的时候
  179. TimersSec += 1
  180. tim_psd = 0
  181. Gdata[3] = 400 - TimersSec #游戏所剩时间
  182. return TimersSec,Gdata,time_passed,tim_psd
  183. while True:
  184. #事件检测
  185. for event in pygame.event.get():
  186. if event.type == pygame.QUIT:
  187. exit()
  188. if event.type == KEYDOWN:
  189. keys=pygame.key.get_pressed()
  190. if keys[pygame.K_a]:
  191. if event.key == K_w and inp_flag == 0:
  192. if y <= HEIGHT-200: #看y坐标 必须在起点
  193. jump_flag = 3 #按了上 和 向前
  194. if y >= HEIGHT-200:#如果角色在平地才走动 后退 左
  195. #if bg_w_1==0:
  196. #x-=5
  197. x-=(move_values+3.5)
  198. inp_flag = 2
  199. if keys[pygame.K_d]:
  200. if event.key == K_w and inp_flag == 0:
  201. if y <= HEIGHT-200: #看y坐标 必须在起点
  202. jump_flag = 2 #按了上 和 向前
  203. if y >= HEIGHT-200:#如果角色在平地才走动 前景 右
  204. if x<(WIDTH/4): #角色还在屏幕左边 可移动
  205. x+=(move_values+3.5)
  206. inp_flag = 1
  207. if keys[pygame.K_w]: #jump
  208. jump_flag = 1 #仅仅是按了跳跃
  209. jump_adder += 1 #跳跃累加器
  210. if event.key == pygame.K_d and (jump_flag == 1):
  211. if y == HEIGHT-200: #看y坐标 必须在起点
  212. jump_flag = 2 #按了上 和 向前
  213. if event.key == pygame.K_a and (jump_flag == 1):
  214. if y == HEIGHT-200: #看y坐标 必须在起点
  215. jump_flag = 3 #按了上 和 向后
  216. if keys[pygame.K_p]: #重启
  217. score,money,world,time,Gdata,TimersSec,Timers,x,y,inp_flag,times,times2,move_values,\
  218. jump_values,jump_values2,jump_values3,jump_adder,jump_max_point,jump_flag,bg_w_1,bg_w_2,tim_psd = \
  219. game_initializaion(score,money,world,time,Gdata,TimersSec,Timers,x,y,inp_flag,times,times2,\
  220. move_values,jump_values,jump_values2,jump_values3,jump_adder,jump_max_point,jump_flag,bg_w_1,\
  221. bg_w_2,tim_psd)
  222. if event.type == KEYUP:
  223. if keys[pygame.K_a]:
  224. inp_flag = -1
  225. if keys[pygame.K_d]:
  226. inp_flag = -2
  227. if keys[pygame.K_w]:
  228. if jump_adder < 4 : #如果松开按键没有达到jump_adder跳跃累加器的值 (那么就他们置零)
  229. jump_adder = 0
  230. ##在地面时 重设默认跳跃的最高点(还原)
  231. jump_max_point = Reset_max_point(jump_max_point)
  232. #jump action 1
  233. if jump_flag == 1: #只有跳跃
  234. #让其他方式跳跃值为 0
  235. jump_values2 = 0
  236. jump_values3 = 0
  237. #------
  238. #持续按键跳跃的结构
  239. if jump_adder >=4 :
  240. jump_max_point = 100 #第二次跳跃最大值
  241. jump_adder = 0
  242. #------
  243. jump_values+=1.25
  244. if jump_values <= jump_max_point:
  245. y -= 5
  246. x = jump_leftScreenBgnotMove(x)
  247. if jump_max_point == 100:#跳跃的高度不同 y坐标的速度也要慢点
  248. y += 1.5
  249. x = jump_leftScreenBgnotMove(x)
  250. elif jump_values <= jump_max_point+8:
  251. pass
  252. elif jump_values <=jump_max_point*2+8:
  253. if HeroHeightIs(): #如果角色在控制 就继续加y轴的值 1
  254. y += 5
  255. x = jump_leftScreenBgnotMove(x)
  256. if jump_max_point == 100:#跳跃的高度不同 y坐标的速度也要慢点
  257. y -= 1.5
  258. x = jump_leftScreenBgnotMove(x)
  259. else:
  260. y = HEIGHT-200
  261. jump_flag = 0
  262. jump_values = 0
  263. #wall detection
  264. if x<=0:
  265. x=0
  266. if x+hero.get_width()>WIDTH:
  267. x=WIDTH-hero.get_width()
  268. #角色的动作 函数
  269. times,times2,inp_flag,hero = WalkAction(times,times2,inp_flag,hero)
  270. #1 .bg move---blit
  271. screen.blit(background,(bg_w_2,0))
  272. screen.blit(background,(bg_w_1,0))
  273. #绘制信息
  274. screen.blit(mario_name,(WIDTH*0.03,3))#绘制第一项 名字
  275. screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers = \
  276. Screen_MoneyIc(screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers) #绘制第二项 金钱图标
  277. screen.blit(Game_moneyX,(WIDTH*0.28,24))#绘制第二项 x
  278. screen.blit(Game_world,(WIDTH*0.5-Game_world.get_width()/2,3))#绘制第三项 世界地图
  279. screen.blit(Game_time,(WIDTH*0.84,3))#绘制第四项 游戏时间
  280. for DATAi in range(4):
  281. Game_data = mariofont.render("%s"% Gdata[DATAi],True,(255,255,128),None) #综合绘制: 分数 金币 关卡 游戏时间
  282. if DATAi != 2:
  283. screen.blit(Game_data,(WIDTH*(0.03+DATAi*0.27),24))
  284. elif DATAi == 2:
  285. Game_data = mariofont.render("%s-%s"% (Gdata[DATAi]/10,Gdata[DATAi]%10),True,(255,255,128),None) #综合绘制: 分数 金币 关卡 游戏时间
  286. screen.blit(Game_data,(WIDTH*0.5-Game_data.get_width()/2,15))
  287. #绘制信息
  288. #2 .bg move--panel
  289. #if inp_flag == 2: #往左走 壁纸向右拖动
  290. # bg_w_1+=move_values/4
  291. # bg_w_2+=move_values/4
  292. if inp_flag == 1 and x>=(WIDTH/4):#往右走 壁纸向左拖动
  293. bg_w_1-=(move_values/4-0.5)
  294. bg_w_2-=(move_values/4-0.5)
  295. if bg_w_1>=0:
  296. bg_w_1,bg_w_2 = 0,WIDTH-2
  297. if bg_w_1<-WIDTH:
  298. bg_w_1,bg_w_2 = 0,WIDTH-2
  299. screen.blit(hero,(x,y))
  300. pygame.time.delay(2) #毫秒
  301. time_passed = clock.tick()
  302. TimersSec,Gdata,time_passed,tim_psd = Game_Timers(TimersSec,Gdata,time_passed,tim_psd) #游戏定时
  303. pygame.display.update()
  304. if __name__ == '__main__':
  305. main()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/215896
推荐阅读
相关标签
  

闽ICP备14008679号