- 🔔 B站主頁:https://space.bilibili.com/1707990930
- 📣 歡迎🎉點贊👍收藏🌟評論📝如有錯誤請指正!
- 💻 Python&Java領域博主
- 😁 你們的支持是我最大的動力
大家好,我是愛麗
這個Python中的馬里奧游戲代碼是用PyGame庫設計的圖形用戶界面(GUI),
談到游戲的玩法,這是一個單人游戲,玩家(馬里奧)必須躲避從龍身上發出的火球,
每一關都有更多的困難,一旦關卡增加,區域就會變得越來越小,在這個超級馬里奧Python教程中,你可以學習如何用Python制作超級馬里奧游戲,
文章目錄
- 🕹 Python 中的馬里奧游戲:專案資訊
- 🕹 第一步:專案搭建
- Step 1、新建一個專案
- Step 2、創建一個 python 檔案
- Step 3、命名你的 Python 檔案
- Step 4、實際代碼,
- 🕹 匯入模塊
- 🕹 開始游戲的代碼部分
- 🕹 游戲級別
- 🕹 主要模塊
- 🕹 游戲結束
- 🕹 完整的源代碼
- 》》》🎁CSDN直播課&解答群《《《
要開始用Python創建一個馬里奧游戲,請確保你的電腦中安裝了PyCharm IDE,
順便說一下,如果你是Python編程的新手,你不知道什么是可以使用的Python IDE,我這里有一個適合你的零基礎入門到精通的教程,可以添加CSDN官方微信免費獲取:


🕹 Python 中的馬里奧游戲:專案資訊
| 專案名: | 超級馬里奧 |
|---|---|
| 使用的語言: | 基于 Python (GUI) |
| Python 版本(推薦): | 3.x |
| 開發工具: | PyCharm |
| 資料庫: | 沒有 |
| 版本: | 0.1x |
🕹 第一步:專案搭建
Step 1、新建一個專案
首先打開Pycharm IDE,然后創建一個 “專案名稱”,創建好專案名稱后點擊 "創建 "按鈕,

Step 2、創建一個 python 檔案
創建專案名稱后,“右鍵單擊”您的專案名稱,然后單擊“新建”,然后單擊“ python 檔案”,

Step 3、命名你的 Python 檔案
第三次創建python檔案后,在點擊“輸入”之后命名你的python檔案,

Step 4、實際代碼,
你可以自由復制下面給出的代碼并下載下面的完整源代碼,
🕹 匯入模塊
import pygame
import sys
給出的代碼正在匯入所有模塊,
🕹 開始游戲的代碼部分
def start_game():
canvas.fill(BLACK)
start_img = pygame.image.load('start.png')
start_img_rect = start_img.get_rect()
start_img_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
canvas.blit(start_img, start_img_rect)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
game_loop()
pygame.display.update()
在這個模塊中,這是超級馬里奧的開始游戲部分,
輸出:

🕹 游戲級別
def check_level(SCORE):
global LEVEL
if SCORE in range(0, 10):
cactus_img_rect.bottom = 50
fire_img_rect.top = WINDOW_HEIGHT - 50
LEVEL = 1
elif SCORE in range(10, 20):
cactus_img_rect.bottom = 100
fire_img_rect.top = WINDOW_HEIGHT - 100
LEVEL = 2
elif SCORE in range(20, 30):
cactus_img_rect.bottom = 150
fire_img_rect.top = WINDOW_HEIGHT - 150
LEVEL = 3
elif SCORE > 30:
cactus_img_rect.bottom = 200
fire_img_rect.top = WINDOW_HEIGHT - 200
LEVEL = 4
如果你通過給定級別的挑戰,則在此模塊中這是游戲的級別,
🕹 主要模塊
def game_loop():
while True:
global dragon
dragon = Dragon()
flames = Flames()
mario = Mario()
add_new_flame_counter = 0
global SCORE
SCORE = 0
global HIGH_SCORE
flames_list = []
pygame.mixer.music.load('mario_theme.wav')
pygame.mixer.music.play(-1, 0.0)
while True:
canvas.fill(BLACK)
check_level(SCORE)
dragon.update()
add_new_flame_counter += 1
if add_new_flame_counter == ADD_NEW_FLAME_RATE:
add_new_flame_counter = 0
new_flame = Flames()
flames_list.append(new_flame)
for f in flames_list:
if f.flames_img_rect.left <= 0:
flames_list.remove(f)
SCORE += 1
f.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
mario.up = True
mario.down = False
elif event.key == pygame.K_DOWN:
mario.down = True
mario.up = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
mario.up = False
mario.down = True
elif event.key == pygame.K_DOWN:
mario.down = True
mario.up = False
score_font = font.render('Score:'+str(SCORE), True, GREEN)
score_font_rect = score_font.get_rect()
score_font_rect.center = (200, cactus_img_rect.bottom + score_font_rect.height/2)
canvas.blit(score_font, score_font_rect)
level_font = font.render('Level:'+str(LEVEL), True, GREEN)
level_font_rect = level_font.get_rect()
level_font_rect.center = (500, cactus_img_rect.bottom + score_font_rect.height/2)
canvas.blit(level_font, level_font_rect)
top_score_font = font.render('Top Score:'+str(topscore.high_score),True,GREEN)
top_score_font_rect = top_score_font.get_rect()
top_score_font_rect.center = (800, cactus_img_rect.bottom + score_font_rect.height/2)
canvas.blit(top_score_font, top_score_font_rect)
canvas.blit(cactus_img, cactus_img_rect)
canvas.blit(fire_img, fire_img_rect)
mario.update()
for f in flames_list:
if f.flames_img_rect.colliderect(mario.mario_img_rect):
game_over()
if SCORE > mario.mario_score:
mario.mario_score = SCORE
pygame.display.update()
CLOCK.tick(FPS)
在這個模塊中,這是游戲的主要模塊,由布林值和其他回圈和條件組成,
🕹 游戲結束
def game_over():
pygame.mixer.music.stop()
music = pygame.mixer.Sound('mario_dies.wav')
music.play()
topscore.top_score(SCORE)
game_over_img = pygame.image.load('end.png')
game_over_img_rect = game_over_img.get_rect()
game_over_img_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
canvas.blit(game_over_img, game_over_img_rect)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
music.stop()
game_loop()
pygame.display.update()
輸出界面:

🕹 完整的源代碼
import pygame
import sys
pygame.init()
WINDOW_WIDTH = 1200
WINDOW_HEIGHT = 600
FPS = 20
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
ADD_NEW_FLAME_RATE = 25
cactus_img = pygame.image.load('cactus_bricks.png')
cactus_img_rect = cactus_img.get_rect()
cactus_img_rect.left = 0
fire_img = pygame.image.load('fire_bricks.png')
fire_img_rect = fire_img.get_rect()
fire_img_rect.left = 0
CLOCK = pygame.time.Clock()
font = pygame.font.SysFont('forte', 20)
canvas = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Mario')
class Topscore:
def __init__(self):
self.high_score = 0
def top_score(self, score):
if score > self.high_score:
self.high_score = score
return self.high_score
topscore = Topscore()
class Dragon:
dragon_velocity = 10
def __init__(self):
self.dragon_img = pygame.image.load('dragon.png')
self.dragon_img_rect = self.dragon_img.get_rect()
self.dragon_img_rect.width -= 10
self.dragon_img_rect.height -= 10
self.dragon_img_rect.top = WINDOW_HEIGHT/2
self.dragon_img_rect.right = WINDOW_WIDTH
self.up = True
self.down = False
def update(self):
canvas.blit(self.dragon_img, self.dragon_img_rect)
if self.dragon_img_rect.top <= cactus_img_rect.bottom:
self.up = False
self.down = True
elif self.dragon_img_rect.bottom >= fire_img_rect.top:
self.up = True
self.down = False
if self.up:
self.dragon_img_rect.top -= self.dragon_velocity
elif self.down:
self.dragon_img_rect.top += self.dragon_velocity
class Flames:
flames_velocity = 20
def __init__(self):
self.flames = pygame.image.load('fireball.png')
self.flames_img = pygame.transform.scale(self.flames, (20, 20))
self.flames_img_rect = self.flames_img.get_rect()
self.flames_img_rect.right = dragon.dragon_img_rect.left
self.flames_img_rect.top = dragon.dragon_img_rect.top + 30
def update(self):
canvas.blit(self.flames_img, self.flames_img_rect)
if self.flames_img_rect.left > 0:
self.flames_img_rect.left -= self.flames_velocity
class Mario:
velocity = 10
def __init__(self):
self.mario_img = pygame.image.load('maryo.png')
self.mario_img_rect = self.mario_img.get_rect()
self.mario_img_rect.left = 20
self.mario_img_rect.top = WINDOW_HEIGHT/2 - 100
self.down = True
self.up = False
def update(self):
canvas.blit(self.mario_img, self.mario_img_rect)
if self.mario_img_rect.top <= cactus_img_rect.bottom:
game_over()
if SCORE > self.mario_score:
self.mario_score = SCORE
if self.mario_img_rect.bottom >= fire_img_rect.top:
game_over()
if SCORE > self.mario_score:
self.mario_score = SCORE
if self.up:
self.mario_img_rect.top -= 10
if self.down:
self.mario_img_rect.bottom += 10
def game_over():
pygame.mixer.music.stop()
music = pygame.mixer.Sound('mario_dies.wav')
music.play()
topscore.top_score(SCORE)
game_over_img = pygame.image.load('end.png')
game_over_img_rect = game_over_img.get_rect()
game_over_img_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
canvas.blit(game_over_img, game_over_img_rect)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
music.stop()
game_loop()
pygame.display.update()
def start_game():
canvas.fill(BLACK)
start_img = pygame.image.load('start.png')
start_img_rect = start_img.get_rect()
start_img_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
canvas.blit(start_img, start_img_rect)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
game_loop()
pygame.display.update()
def check_level(SCORE):
global LEVEL
if SCORE in range(0, 10):
cactus_img_rect.bottom = 50
fire_img_rect.top = WINDOW_HEIGHT - 50
LEVEL = 1
elif SCORE in range(10, 20):
cactus_img_rect.bottom = 100
fire_img_rect.top = WINDOW_HEIGHT - 100
LEVEL = 2
elif SCORE in range(20, 30):
cactus_img_rect.bottom = 150
fire_img_rect.top = WINDOW_HEIGHT - 150
LEVEL = 3
elif SCORE > 30:
cactus_img_rect.bottom = 200
fire_img_rect.top = WINDOW_HEIGHT - 200
LEVEL = 4
def game_loop():
while True:
global dragon
dragon = Dragon()
flames = Flames()
mario = Mario()
add_new_flame_counter = 0
global SCORE
SCORE = 0
global HIGH_SCORE
flames_list = []
pygame.mixer.music.load('mario_theme.wav')
pygame.mixer.music.play(-1, 0.0)
while True:
canvas.fill(BLACK)
check_level(SCORE)
dragon.update()
add_new_flame_counter += 1
if add_new_flame_counter == ADD_NEW_FLAME_RATE:
add_new_flame_counter = 0
new_flame = Flames()
flames_list.append(new_flame)
for f in flames_list:
if f.flames_img_rect.left <= 0:
flames_list.remove(f)
SCORE += 1
f.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
mario.up = True
mario.down = False
elif event.key == pygame.K_DOWN:
mario.down = True
mario.up = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
mario.up = False
mario.down = True
elif event.key == pygame.K_DOWN:
mario.down = True
mario.up = False
score_font = font.render('Score:'+str(SCORE), True, GREEN)
score_font_rect = score_font.get_rect()
score_font_rect.center = (200, cactus_img_rect.bottom + score_font_rect.height/2)
canvas.blit(score_font, score_font_rect)
level_font = font.render('Level:'+str(LEVEL), True, GREEN)
level_font_rect = level_font.get_rect()
level_font_rect.center = (500, cactus_img_rect.bottom + score_font_rect.height/2)
canvas.blit(level_font, level_font_rect)
top_score_font = font.render('Top Score:'+str(topscore.high_score),True,GREEN)
top_score_font_rect = top_score_font.get_rect()
top_score_font_rect.center = (800, cactus_img_rect.bottom + score_font_rect.height/2)
canvas.blit(top_score_font, top_score_font_rect)
canvas.blit(cactus_img, cactus_img_rect)
canvas.blit(fire_img, fire_img_rect)
mario.update()
for f in flames_list:
if f.flames_img_rect.colliderect(mario.mario_img_rect):
game_over()
if SCORE > mario.mario_score:
mario.mario_score = SCORE
pygame.display.update()
CLOCK.tick(FPS)
start_game()
》》》🎁CSDN直播課&解答群《《《

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/377228.html
標籤:其他
上一篇:2021總結之游戲行業的行業觀感
