程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

誰還不會用python做小游戲了?做個馬裡奧分分鐘解決

編輯:Python

一、前言

嗨喽,大家好呀!這裡是小熊貓

在你的童年記憶裡,是否有一個蹦跳、頂蘑菇的小人已經被遺忘?

馬裡奧是靠吃蘑菇成長,聞名世界的超級巨星。特征是大鼻子、頭戴帽子、身穿背帶工作服、還留著胡子。

帽子加背帶工作服、大鼻子和胡子等特征,離英雄的形象相差甚遠。

再加上少許肥胖的身材,稍不留神可能就會把我們的英雄馬裡奧當成在便利店打工的中年大叔。

但是形象上所帶來的個性和親切感,卻在玩家的心中根深蒂固

如果你的童年也曾被魔性的 燈燈燈燈燈燈燈洗腦那就接著來懷舊一番吧~

今天我就帶著大家自制一款超級瑪麗游戲,還原度超高哦~

還在等什麼動動手就能擁有屬於自己的”超級瑪麗“游戲呢,趕快學起來吧~

二、所需准備

demo

graphics

三、代碼

__author__ = 'marble_xu'
DEBUG = False
DEBUG_START_X = 110
DEBUG_START_y = 538
SCREEN_HEIGHT = 600
SCREEN_WIDTH = 800
SCREEN_SIZE = (SCREEN_WIDTH,SCREEN_HEIGHT)
ORIGINAL_CAPTION = "超級瑪麗 問題解答源碼領取Q群:660193417"
## COLORS ##
# R G B
GRAY = (100, 100, 100)
NAVYBLUE = ( 60, 60, 100)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
FOREST_GREEN = ( 31, 162, 35)
BLUE = ( 0, 0, 255)
SKY_BLUE = ( 39, 145, 251)
YELLOW = (255, 255, 0)
ORANGE = (255, 128, 0)
PURPLE = (255, 0, 255)
CYAN = ( 0, 255, 255)
BLACK = ( 0, 0, 0)
NEAR_BLACK = ( 19, 15, 48)
COMBLUE = (233, 232, 255)
GOLD = (255, 215, 0)
BGCOLOR = WHITE
SIZE_MULTIPLIER = 2.5
BRICK_SIZE_MULTIPLIER = 2.69
BACKGROUND_MULTIPLER = 2.679
GROUND_HEIGHT = SCREEN_HEIGHT - 62
GAME_TIME_OUT = 301
#STATES FOR ENTIRE GAME
MAIN_MENU = 'main menu'
LOAD_SCREEN = 'load screen'
TIME_OUT = 'time out'
GAME_OVER = 'game over'
LEVEL = 'level'
#MAIN MENU CURSOR STATES
PLAYER1 = '1 PLAYER GAME'
PLAYER2 = '2 PLAYER GAME'
#GAME INFO DICTIONARY KEYS
COIN_TOTAL = 'coin total'
SCORE = 'score'
TOP_SCORE = 'top score'
LIVES = 'lives'
CURRENT_TIME = 'current time'
LEVEL_NUM = 'level num'
PLAYER_NAME = 'player name'
PLAYER_MARIO = 'mario'
PLAYER_LUIGI = 'luigi'
#MAP COMPONENTS
MAP_IMAGE = 'image_name'
MAP_MAPS = 'maps'
SUB_MAP = 'sub_map'
MAP_GROUND = 'ground'
MAP_PIPE = 'pipe'
PIPE_TYPE_NONE = 0
PIPE_TYPE_IN = 1 # can go down in the pipe
PIPE_TYPE_HORIZONTAL = 2 # can go right in the pipe
MAP_STEP = 'step'
MAP_BRICK = 'brick'
BRICK_NUM = 'brick_num'
TYPE_NONE = 0
TYPE_COIN = 1
TYPE_STAR = 2
MAP_BOX = 'box'
TYPE_MUSHROOM = 3
TYPE_FIREFLOWER = 4
TYPE_FIREBALL = 5
TYPE_LIFEMUSHROOM = 6
MAP_ENEMY = 'enemy'
ENEMY_TYPE_GOOMBA = 0
ENEMY_TYPE_KOOPA = 1
ENEMY_TYPE_FLY_KOOPA = 2
ENEMY_TYPE_PIRANHA = 3
ENEMY_TYPE_FIRESTICK = 4
ENEMY_TYPE_FIRE_KOOPA = 5
ENEMY_RANGE = 'range'
MAP_CHECKPOINT = 'checkpoint'
ENEMY_GROUPID = 'enemy_groupid'
MAP_INDEX = 'map_index'
CHECKPOINT_TYPE_ENEMY = 0
CHECKPOINT_TYPE_FLAG = 1
CHECKPOINT_TYPE_CASTLE = 2
CHECKPOINT_TYPE_MUSHROOM = 3
CHECKPOINT_TYPE_PIPE = 4 # trigger player to go right in a pipe
CHECKPOINT_TYPE_PIPE_UP = 5 # trigger player to another map and go up out of a pipe
CHECKPOINT_TYPE_MAP = 6 # trigger player to go to another map
CHECKPOINT_TYPE_BOSS = 7 # defeat the boss
MAP_FLAGPOLE = 'flagpole'
FLAGPOLE_TYPE_FLAG = 0
FLAGPOLE_TYPE_POLE = 1
FLAGPOLE_TYPE_TOP = 2
MAP_SLIDER = 'slider'
HORIZONTAL = 0
VERTICAL = 1
VELOCITY = 'velocity'
MAP_COIN = 'coin'
#COMPONENT COLOR
COLOR = 'color'
COLOR_TYPE_ORANGE = 0
COLOR_TYPE_GREEN = 1
COLOR_TYPE_RED = 2
#BRICK STATES
RESTING = 'resting'
BUMPED = 'bumped'
OPENED = 'opened'
#MUSHROOM STATES
REVEAL = 'reveal'
SLIDE = 'slide'
#Player FRAMES
PLAYER_FRAMES = 'image_frames'
RIGHT_SMALL_NORMAL = 'right_small_normal'
RIGHT_BIG_NORMAL = 'right_big_normal'
RIGHT_BIG_FIRE = 'right_big_fire'
#PLAYER States
STAND = 'standing'
WALK = 'walk'
JUMP = 'jump'
FALL = 'fall'
FLY = 'fly'
SMALL_TO_BIG = 'small to big'
BIG_TO_FIRE = 'big to fire'
BIG_TO_SMALL = 'big to small'
FLAGPOLE = 'flag pole'
WALK_AUTO = 'walk auto' # not handle key input in this state
END_OF_LEVEL_FALL = 'end of level fall'
IN_CASTLE = 'in castle'
DOWN_TO_PIPE = 'down to pipe'
UP_OUT_PIPE = 'up out of pipe'
#PLAYER FORCES
PLAYER_SPEED = 'speed'
WALK_ACCEL = 'walk_accel'
RUN_ACCEL = 'run_accel'
JUMP_VEL = 'jump_velocity'
MAX_Y_VEL = 'max_y_velocity'
MAX_RUN_SPEED = 'max_run_speed'
MAX_WALK_SPEED = 'max_walk_speed'
SMALL_TURNAROUND = .35
JUMP_GRAVITY = .31
GRAVITY = 1.01
#LIST of ENEMIES
GOOMBA = 'goomba'
KOOPA = 'koopa'
FLY_KOOPA = 'fly koopa'
FIRE_KOOPA = 'fire koopa'
FIRE = 'fire'
PIRANHA = 'piranha'
FIRESTICK = 'firestick'
#GOOMBA Stuff
LEFT = 'left'
RIGHT = 'right'
JUMPED_ON = 'jumped on'
DEATH_JUMP = 'death jump'
#KOOPA STUFF
SHELL_SLIDE = 'shell slide'
#FLAG STATE
TOP_OF_POLE = 'top of pole'
SLIDE_DOWN = 'slide down'
BOTTOM_OF_POLE = 'bottom of pole'
#FIREBALL STATE
FLYING = 'flying'
BOUNCING = 'bouncing'
EXPLODING = 'exploding'
#IMAGE SHEET
ENEMY_SHEET = 'smb_enemies_sheet'
ITEM_SHEET = 'item_objects'
__author__ = 'marble_xu'
import pygame as pg
from .. import setup, tools
from .. import constants as c
from . import coin, powerup
class Box(pg.sprite.Sprite):
def __init__(self, x, y, type, group=None, name=c.MAP_BOX):
pg.sprite.Sprite.__init__(self)
self.frames = []
self.frame_index = 0
self.load_frames()
self.image = self.frames[self.frame_index]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.rest_height = y
self.animation_timer = 0
self.first_half = True # First half of animation cycle
self.state = c.RESTING
self.y_vel = 0
self.gravity = 1.2
self.type = type
self.group = group
self.name = name
def load_frames(self):
sheet = setup.GFX['tile_set']
frame_rect_list = [(384, 0, 16, 16), (400, 0, 16, 16),
(416, 0, 16, 16), (400, 0, 16, 16), (432, 0, 16, 16)]
for frame_rect in frame_rect_list:
self.frames.append(tools.get_image(sheet, *frame_rect,
c.BLACK, c.BRICK_SIZE_MULTIPLIER))
def update(self, game_info):
self.current_time = game_info[c.CURRENT_TIME]
if self.state == c.RESTING:
self.resting()
elif self.state == c.BUMPED:
self.bumped()
def resting(self):
time_list = [375, 125, 125, 125]
if (self.current_time - self.animation_timer) > time_list[self.frame_index]:
self.frame_index += 1
if self.frame_index == 4:
self.frame_index = 0
self.animation_timer = self.current_time
self.image = self.frames[self.frame_index]
def bumped(self):
self.rect.y += self.y_vel
self.y_vel += self.gravity
if self.rect.y > self.rest_height + 5:
self.rect.y = self.rest_height
self.state = c.OPENED
if self.type == c.TYPE_MUSHROOM:
self.group.add(powerup.Mushroom(self.rect.centerx, self.rect.y))
elif self.type == c.TYPE_FIREFLOWER:
self.group.add(powerup.FireFlower(self.rect.centerx, self.rect.y))
elif self.type == c.TYPE_LIFEMUSHROOM:
self.group.add(powerup.LifeMushroom(self.rect.centerx, self.rect.y))
self.frame_index = 4
self.image = self.frames[self.frame_index]
def start_bump(self, score_group):
self.y_vel = -6
self.state = c.BUMPED
if self.type == c.TYPE_COIN:
self.group.add(coin.Coin(self.rect.centerx, self.rect.y, score_group))
import pygame as pg
from source.main import main
if __name__=='__main__':
main()
pg.quit()

點擊此處領取完整源碼🧡🤎

(https://jq.qq.com/?_wv=1027&k=ONMKhFSZ)

由於代碼太多太多了如下圖所示(一部分):所以還是大家自己拿完整的代碼哈!

四、效果展示(僅部分)

4.1 運行效果頁

4.2 開局頁面

4.3 吃蘑菇變大效果

4.4 吃了太陽花效果

尾語

Thank you Mario! But our princess is 1n another castle! ”
謝謝你馬裡奧,但我們的公主在另一座城堡裡!

對游戲感興趣的小伙伴兒趕緊自己動手造一個吧~

你們的支持是我最大的動力!!感謝陪伴~

記得三連哦~ 歡迎大家閱讀往期的文章呀

我是小熊貓,咱下篇文章見(*◡‿◡)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved