小言_互联网的博客

python游戏开发超级玛丽,100%真实版

391人阅读  评论(0)

导语🎉

哈喽!哈喽!小编今日游戏更新——超级玛丽

“超级玛丽 《超级马里奥兄弟》”有多少人还记得这款经典游戏嘛?小编先来带大家回忆一下,我们

先认识一下我们的主人公。

永远戴帽子,有着大胡子,还穿着背带裤的马里奥👆

今天小编就带着大家自制一款超级玛丽游戏,还原度超高哦~

正文🎉

一、环境安装

Python3、Pycharm、Pygame模块很多自带的模块等。

pip install -i https://pypi.douban.com/simple/ +模块名。

      准备的图片素材+背景音乐+字体(可修改)


二、开始敲代码🛒

2.1 运行程序:mario_level_1.py。


  
  1. """
  2. This is an attempt to recreate the first level of
  3. Super Mario Bros for the NES.
  4. """
  5. import sys
  6. import pygame as pg
  7. from data.main import main
  8. import cProfile
  9. if __name__== '__main__':
  10. main()
  11. pg.quit()
  12. sys.exit()

2.2 配置音乐文字等setup.py。


  
  1. """
  2. This module initializes the display and creates dictionaries of resources.
  3. """
  4. import os
  5. import pygame as pg
  6. from . import tools
  7. from . import constants as c
  8. ORIGINAL_CAPTION = c.ORIGINAL_CAPTION
  9. os.environ[ 'SDL_VIDEO_CENTERED'] = '1'
  10. pg.init()
  11. pg.event.set_allowed([pg.KEYDOWN, pg.KEYUP, pg.QUIT])
  12. pg.display.set_caption(c.ORIGINAL_CAPTION)
  13. SCREEN = pg.display.set_mode(c.SCREEN_SIZE)
  14. SCREEN_RECT = SCREEN.get_rect()
  15. FONTS = tools.load_all_fonts(os.path.join( "resources", "fonts"))
  16. MUSIC = tools.load_all_music(os.path.join( "resources", "music"))
  17. GFX = tools.load_all_gfx(os.path.join( "resources", "graphics"))
  18. SFX = tools.load_all_sfx(os.path.join( "resources", "sound"))

2.3游戏音乐设置game_sound.py。


  
  1. import pygame as pg
  2. from . import setup
  3. from . import constants as c
  4. class Sound( object):
  5. """Handles all sound for the game"""
  6. def __init__( self, overhead_info):
  7. """Initialize the class"""
  8. self.sfx_dict = setup.SFX
  9. self.music_dict = setup.MUSIC
  10. self.overhead_info = overhead_info
  11. self.game_info = overhead_info.game_info
  12. self.set_music_mixer()
  13. def set_music_mixer( self):
  14. """Sets music for level"""
  15. if self.overhead_info.state == c.LEVEL:
  16. pg.mixer.music.load(self.music_dict[ 'main_theme'])
  17. pg.mixer.music.play()
  18. self.state = c.NORMAL
  19. elif self.overhead_info.state == c.GAME_OVER:
  20. pg.mixer.music.load(self.music_dict[ 'game_over'])
  21. pg.mixer.music.play()
  22. self.state = c.GAME_OVER
  23. def update( self, game_info, mario):
  24. """Updates sound object with game info"""
  25. self.game_info = game_info
  26. self.mario = mario
  27. self.handle_state()
  28. def handle_state( self):
  29. """Handles the state of the soundn object"""
  30. if self.state == c.NORMAL:
  31. if self.mario.dead:
  32. self.play_music( 'death', c.MARIO_DEAD)
  33. elif self.mario.invincible \
  34. and self.mario.losing_invincibility == False:
  35. self.play_music( 'invincible', c.MARIO_INVINCIBLE)
  36. elif self.mario.state == c.FLAGPOLE:
  37. self.play_music( 'flagpole', c.FLAGPOLE)
  38. elif self.overhead_info.time == 100:
  39. self.play_music( 'out_of_time', c.TIME_WARNING)
  40. elif self.state == c.FLAGPOLE:
  41. if self.mario.state == c.WALKING_TO_CASTLE:
  42. self.play_music( 'stage_clear', c.STAGE_CLEAR)
  43. elif self.state == c.STAGE_CLEAR:
  44. if self.mario.in_castle:
  45. self.sfx_dict[ 'count_down'].play()
  46. self.state = c.FAST_COUNT_DOWN
  47. elif self.state == c.FAST_COUNT_DOWN:
  48. if self.overhead_info.time == 0:
  49. self.sfx_dict[ 'count_down'].stop()
  50. self.state = c.WORLD_CLEAR
  51. elif self.state == c. TIME_WARNING:
  52. if pg.mixer.music.get_busy() == 0:
  53. self.play_music( 'main_theme_sped_up', c.SPED_UP_NORMAL)
  54. elif self.mario.dead:
  55. self.play_music( 'death', c.MARIO_DEAD)
  56. elif self.state == c.SPED_UP_NORMAL:
  57. if self.mario.dead:
  58. self.play_music( 'death', c.MARIO_DEAD)
  59. elif self.mario.state == c.FLAGPOLE:
  60. self.play_music( 'flagpole', c.FLAGPOLE)
  61. elif self.state == c.MARIO_INVINCIBLE:
  62. if (self.mario.current_time - self.mario.invincible_start_timer) > 11000:
  63. self.play_music( 'main_theme', c.NORMAL)
  64. elif self.mario.dead:
  65. self.play_music( 'death', c.MARIO_DEAD)
  66. elif self.state == c.WORLD_CLEAR:
  67. pass
  68. elif self.state == c.MARIO_DEAD:
  69. pass
  70. elif self.state == c.GAME_OVER:
  71. pass
  72. def play_music( self, key, state):
  73. """Plays new music"""
  74. pg.mixer.music.load(self.music_dict[key])
  75. pg.mixer.music.play()
  76. self.state = state
  77. def stop_music( self):
  78. """Stops playback"""
  79. pg.mixer.music.stop()

2.4取得的分数


  
  1. import pygame as pg
  2. from .. import setup
  3. from .. import constants as c
  4. class Digit(pg.sprite.Sprite):
  5. """Individual digit for score"""
  6. def __init__( self, image):
  7. super(Digit, self).__init__()
  8. self.image = image
  9. self.rect = image.get_rect()
  10. class Score( object):
  11. """Scores that appear, float up, and disappear"""
  12. def __init__( self, x, y, score, flag_pole=False):
  13. self.x = x
  14. self.y = y
  15. if flag_pole:
  16. self.y_vel = - 4
  17. else:
  18. self.y_vel = - 3
  19. self.sprite_sheet = setup.GFX[ 'item_objects']
  20. self.create_image_dict()
  21. self.score_string = str(score)
  22. self.create_digit_list()
  23. self.flag_pole_score = flag_pole
  24. def create_image_dict( self):
  25. """Creates the dictionary for all the number 图片 needed"""
  26. self.image_dict = {}
  27. image0 = self.get_image( 1, 168, 3, 8)
  28. image1 = self.get_image( 5, 168, 3, 8)
  29. image2 = self.get_image( 8, 168, 4, 8)
  30. image4 = self.get_image( 12, 168, 4, 8)
  31. image5 = self.get_image( 16, 168, 5, 8)
  32. image8 = self.get_image( 20, 168, 4, 8)
  33. image9 = self.get_image( 32, 168, 5, 8)
  34. image10 = self.get_image( 37, 168, 6, 8)
  35. image11 = self.get_image( 43, 168, 5, 8)
  36. self.image_dict[ '0'] = image0
  37. self.image_dict[ '1'] = image1
  38. self.image_dict[ '2'] = image2
  39. self.image_dict[ '4'] = image4
  40. self.image_dict[ '5'] = image5
  41. self.image_dict[ '8'] = image8
  42. self.image_dict[ '3'] = image9
  43. self.image_dict[ '7'] = image10
  44. self.image_dict[ '9'] = image11
  45. def get_image( self, x, y, width, height):
  46. """Extracts image from sprite sheet"""
  47. image = pg.Surface([width, height]).convert()
  48. rect = image.get_rect()
  49. image.blit(self.sprite_sheet, ( 0, 0), (x, y, width, height))
  50. image.set_colorkey(c.BLACK)
  51. image = pg.transform.scale(image,
  52. ( int(rect.width*c.BRICK_SIZE_MULTIPLIER),
  53. int(rect.height*c.BRICK_SIZE_MULTIPLIER)))
  54. return image
  55. def create_digit_list( self):
  56. """Creates the group of 图片 based on score received"""
  57. self.digit_list = []
  58. self.digit_group = pg.sprite.Group()
  59. for digit in self.score_string:
  60. self.digit_list.append(Digit(self.image_dict[digit]))
  61. self.set_rects_for_images()
  62. def set_rects_for_images( self):
  63. """Set the rect attributes for each image in self.image_list"""
  64. for i, digit in enumerate(self.digit_list):
  65. digit.rect = digit.image.get_rect()
  66. digit.rect.x = self.x + (i * 10)
  67. digit.rect.y = self.y
  68. def update( self, score_list, level_info):
  69. """Updates score movement"""
  70. for number in self.digit_list:
  71. number.rect.y += self.y_vel
  72. if score_list:
  73. self.check_to_delete_floating_scores(score_list, level_info)
  74. if self.flag_pole_score:
  75. if self.digit_list[ 0].rect.y <= 120:
  76. self.y_vel = 0
  77. def draw( self, screen):
  78. """Draws score numbers onto screen"""
  79. for digit in self.digit_list:
  80. screen.blit(digit.image, digit.rect)
  81. def check_to_delete_floating_scores( self, score_list, level_info):
  82. """Check if scores need to be deleted"""
  83. for i, score in enumerate(score_list):
  84. if int(score.score_string) == 1000:
  85. if (score.y - score.digit_list[ 0].rect.y) > 130:
  86. score_list.pop(i)
  87. else:
  88. if (score.y - score.digit_list[ 0].rect.y) > 75:
  89. score_list.pop(i)

三、完整的游戏

由于代码太多太多了如下图所示:所以还是放在文末自己拿完整的代码哈!

四、效果展示(仅部分)

 结尾🎉:

以上是文章的全部内容。

最后完整代码已经打包整理好了,有需要的小伙伴,可以点击这行字体,要么私信小编!


转载:https://blog.csdn.net/a55656aq/article/details/121797109
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场