经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
Python实现超级玛丽游戏系列教程01玛丽登场
来源:cnblogs  作者:师者乐享  时间:2020/12/21 14:46:24  对本文有异议

配套视频教程

配套视频教程

项目代码

项目代码

最终效果

搭建项目结构

定义游戏常量

  1. SCREEN_HEIGHT = 600
  2. SCREEN_WIDTH = 800
  3. SCREEN_SIZE = (SCREEN_WIDTH,SCREEN_HEIGHT)
  4. ORIGINAL_CAPTION = "SuperMario"
  5. GFX = None
  6. ## COLORS ##
  7. # R G B
  8. GRAY = (100, 100, 100)
  9. NAVYBLUE = ( 60, 60, 100)
  10. WHITE = (255, 255, 255)
  11. RED = (255, 0, 0)
  12. GREEN = ( 0, 255, 0)
  13. FOREST_GREEN = ( 31, 162, 35)
  14. BLUE = ( 0, 0, 255)
  15. YELLOW = (255, 255, 0)
  16. ORANGE = (255, 128, 0)
  17. PURPLE = (255, 0, 255)
  18. CYAN = ( 0, 255, 255)
  19. BLACK = ( 0, 0, 0)
  20. NEAR_BLACK = ( 19, 15, 48)
  21. COMBLUE = (233, 232, 255)
  22. GOLD = (255, 215, 0)
  23. BGCOLOR = WHITE
  24. SIZE_MULTIPLIER = 2.5

初始化游戏窗口

tools.py

  1. import pygame as pg
  2. from . import constants as c
  3. class Control(object):
  4. def __init__(self, caption):
  5. pg.init()
  6. pg.display.set_caption(c.ORIGINAL_CAPTION)
  7. self.screen = pg.display.set_mode(c.SCREEN_SIZE)
  8. self.done = False
  9. self.clock = pg.time.Clock()
  10. self.caption = caption
  11. self.fps = 60
  12. self.show_fps = True
  13. self.keys = pg.key.get_pressed()
  14. def update(self):
  15. pg.display.get_surface().fill(c.BGCOLOR)
  16. def event_loop(self):
  17. for event in pg.event.get():
  18. if event.type == pg.QUIT:
  19. self.done = True
  20. elif event.type == pg.KEYDOWN:
  21. self.keys = pg.key.get_pressed()
  22. self.toggle_show_fps(event.key)
  23. elif event.type == pg.KEYUP:
  24. self.keys = pg.key.get_pressed()
  25. def toggle_show_fps(self, key):
  26. if key == pg.K_F5:
  27. self.show_fps = not self.show_fps
  28. def main(self):
  29. """Main loop for entire program"""
  30. while not self.done:
  31. self.event_loop()
  32. self.update()
  33. pg.display.update()
  34. self.clock.tick(self.fps)
  35. if self.show_fps:
  36. fps = self.clock.get_fps()
  37. with_fps = "{} - {:.2f} FPS".format(self.caption, fps)
  38. pg.display.set_caption(with_fps)

mario_level_1.py

  1. import sys
  2. import pygame as pg
  3. from data import tools
  4. from data import constants as c
  5. if __name__=='__main__':
  6. control = tools.Control(c.ORIGINAL_CAPTION)
  7. control.main()
  8. pg.quit()
  9. sys.exit()

玛丽登场

level1.py

  1. import pygame as pg
  2. from .. import constants as c
  3. from .. components import mario
  4. class Level1:
  5. def __init__(self):
  6. self.startup()
  7. def startup(self):
  8. self.mario = mario.Mario()
  9. self.setup_mario_location()
  10. self.all_sprites = pg.sprite.Group(self.mario)
  11. def setup_mario_location(self):
  12. self.mario.rect.x = 80
  13. self.mario.rect.bottom = c.SCREEN_HEIGHT - self.mario.rect.height
  14. def update(self, surface):
  15. """Updates level"""
  16. pg.display.get_surface().fill(c.BGCOLOR)
  17. self.all_sprites.draw(surface)

mario.py

  1. import pygame as pg
  2. from .. import constants as c
  3. class Mario(pg.sprite.Sprite):
  4. def __init__(self):
  5. pg.sprite.Sprite.__init__(self)
  6. self.sprite_sheet = c.GFX['mario_bros']
  7. self.right_frames = []
  8. self.left_frames = []
  9. self.frame_index = 0
  10. self.load_from_sheet()
  11. self.image = self.right_frames[self.frame_index]
  12. self.rect = self.image.get_rect()
  13. def get_image(self, x, y, width, height):
  14. image = pg.Surface([width, height]).convert()
  15. rect = image.get_rect()
  16. image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))
  17. image.set_colorkey(c.BLACK)
  18. image = pg.transform.scale(image,
  19. (int(rect.width * c.SIZE_MULTIPLIER),
  20. int(rect.height * c.SIZE_MULTIPLIER)))
  21. return image
  22. def load_from_sheet(self):
  23. self.right_frames.append(
  24. self.get_image(178, 32, 12, 16)) # right

tools.py修改

原文链接:http://www.cnblogs.com/neuedu/p/python-super-mario-from-scratch.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号