首页
/
每日頭條
/
圖文
/
python瑪麗遊戲代碼
python瑪麗遊戲代碼
更新时间:2025-11-14 19:45:17

這篇文章,我們優先介紹超級瑪麗遊戲中的多狀态跳躍,和行走地圖拖動的原理,然後實現。并實現倒計時和金币動态效果(代碼在後面,可以直接複制)

python瑪麗遊戲代碼(Python做超級瑪麗)1

python瑪麗遊戲代碼(Python做超級瑪麗)2

python瑪麗遊戲代碼(Python做超級瑪麗)3

接下來用下面這四張圖,就可以完全懂得遊戲中背景是怎麼會移動的。

圖一

python瑪麗遊戲代碼(Python做超級瑪麗)4

圖二

python瑪麗遊戲代碼(Python做超級瑪麗)5

圖三

python瑪麗遊戲代碼(Python做超級瑪麗)6

圖四

python瑪麗遊戲代碼(Python做超級瑪麗)7

注:由于代碼是我前幾年學習python的時候做的,代碼寫的很擠都整到一個源文件中,大家看的時候仔細。

源代碼:

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import pygame,os,wx

from random import randint

from sys import exit

from pygame.locals import *

pygame.init()

def main():

#獲取屏幕大小

app=wx.App()

WHFRAMES=wx.DisplaySize()

WIDTH=int(WHFRAMES[0]*0.7)

HEIGHT=int(WHFRAMES[1]*0.8)

Timers = 0 #遊戲定時器

TimersSec = 0 #秒

tim_psd = 0

#獲取屏幕大小

screen=pygame.display.set_mode((WIDTH,HEIGHT),0,32)

caption=pygame.display.set_caption("超級馬裡奧")

screen.fill([255,255,255])

mariofont = pygame.font.Font('fonts/poster.ttf',22)

mario_name = mariofont.render("MARIO",True,(84,65,190),None)

#Game_world = mariofont.render("WORLD",True,(84,65,190),None)

Game_moneyX = mariofont.render("X",True,(255,255,128),None)

Game_time = mariofont.render("TIME",True,(84,65,190),None)

money_ic5 = pygame.image.load('images/PTModelSprite_ID21675.png')

money_ic5 = pygame.transform.scale(money_ic5, (25, 25))

money_ic6 = pygame.image.load('images/PTModelSprite_ID21676.png')

money_ic6 = pygame.transform.scale(money_ic6, (10, 25))

money_ic7 = pygame.image.load('images/PTModelSprite_ID21677.png')

money_ic7 = pygame.transform.scale(money_ic7, (25, 25))

money_ic8 = pygame.image.load('images/PTModelSprite_ID21678.png')

money_ic8 = pygame.transform.scale(money_ic8, (25, 25))

money_timers = 0 #圖片輪播定時器

Game_world = pygame.image.load('images/PTModelSprite_ID2478.png')

background = pygame.image.load('images/PTModelSprite_ID35342.png').convert_alpha()

background = pygame.transform.scale(background, (WIDTH, HEIGHT))

Roads = pygame.image.load('images/PTModelSprite_ID3790.png').convert_alpha()

Roads2 = pygame.image.load('images/PTModelSprite_ID4224.png').convert_alpha()

hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()

x,y = 15,HEIGHT-200

inp_flag = -2 #(stop:-1 left drection ,-2 right drection) ,(walk:1 right drection ,2 left drection)

times,times2 = 0,0 #人物動作定時器

move_values,jump_values,jump_values2,jump_values3 = 12,0,0,0 #一步移動的距離 和 跳躍的值1,2

jump_adder,jump_max_point = 0,50 #跳躍累加器 用來累加按鍵的長短 然後判斷跳躍的高度,跳躍的初始值最高點

jump_flag = 0

bg_w_1,bg_w_2 = 0,WIDTH-2 #兩張壁紙 一前一後循環拖動的變量

#播放背景

#播放背景

#遊戲信息數據定義

score = 0

money = 0

world = 11

time = 400

Gdata = [score,money,world,time]

#遊戲信息數據定義

#初始化函數

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):#數據初始化

#遊戲初始化數據

inp_flag = -2 #(stop:-1 left drection ,-2 right drection) ,(walk:1 right drection ,2 left drection)

x,y = 15,HEIGHT-200 #馬裡奧坐标

times,times2 = 0,0 #人物動作定時器

move_values,jump_values,jump_values2,jump_values3 = 12,0,0,0 #一步移動的距離 和 跳躍的值1,2

jump_adder,jump_max_point = 0,50 #跳躍累加器 用來累加按鍵的長短 然後判斷跳躍的高度,跳躍的初始值最高點

jump_flag = 0

tim_psd = 0

bg_w_1,bg_w_2 = 0,WIDTH-2 #兩張壁紙 一前一後循環拖動的變量

Timers = 0 #遊戲定時器

score = 0 #開始分數

money = 0 #開始金錢

world = 11 #世界關卡第一關1-1 = 11

time = 400#遊戲總時間

TimersSec = 0 #遊戲裡的秒

Gdata = [score,money,world,time]

#遊戲初始化數據

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

#初始化函數

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 =

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)#數據初始化主調函數

clock = pygame.time.Clock()

pygame.key.set_repeat(55)

pygame.display.flip()

def WalkAction(times,times2,inp_flag,hero):

#walk action

if y < HEIGHT -200: #如果在空中 為跳躍圖片

if inp_flag == 1: #right

hero = pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()

if inp_flag == 2: #left

hero = pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

else:

if inp_flag == 1: #right

times = 2

if times < 20:

hero = pygame.image.load('images/PTModelSprite_ID34256.png').convert_alpha()

elif times < 20:

hero = pygame.image.load('images/PTModelSprite_ID34257..png').convert_alpha()

elif times < 40:

hero = pygame.image.load('images/PTModelSprite_ID34258.png').convert_alpha()

elif times < 60:

hero = pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()

elif times < 80:

hero = pygame.image.load('images/PTModelSprite_ID34260.png').convert_alpha()

elif times < 100:

hero = pygame.image.load('images/PTModelSprite_ID34261.png').convert_alpha()

elif times < 120:

hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()

elif times < 140:

times = 0

if inp_flag == 2: #left

times2 = 2

if times2 < 20:

hero = pygame.image.load('images/PTModelSprite_ID34256.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

elif times2 < 20:

hero = pygame.image.load('images/PTModelSprite_ID34257..png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

elif times2 < 40:

hero = pygame.image.load('images/PTModelSprite_ID34258.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

elif times2 < 60:

hero = pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

elif times2 < 80:

hero = pygame.image.load('images/PTModelSprite_ID34260.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

elif times2 < 100:

hero = pygame.image.load('images/PTModelSprite_ID34261.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

elif times2 < 120:

hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

elif times2 < 140:

times2 = 0

elif inp_flag == -1:

hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

times2 = 0

elif inp_flag == -2:

hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()

times2 = 0

return times,times2,inp_flag,hero

def HeroHeightIs(): #判斷角色是否為地面y軸

if y >= HEIGHT-200:

return False

else:#這是在控制的狀況

return True

def Reset_max_point(jump_max_point):#在地面重設默認跳躍的最高點(還原)

if y >= (HEIGHT-200):

jump_max_point = 50 #默認最高點是 50

return jump_max_point

def jump_leftScreenBgnotMove(x):

if x<(WIDTH/4):

if jump_max_point == 50 :

if inp_flag == 1:

x =(2.7)

if inp_flag == 2:

x-=(2.7)

elif jump_max_point == 100 :

if inp_flag == 1:

x =(0.27)

if inp_flag == 2:

x-=(0.27)

return x

def Screen_MoneyIc(screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers) : #繪制第二項 金錢圖标

money_timers = 1

if money_timers < 15 :

screen.blit(money_ic5,(WIDTH*0.24,25)) #繪制第二項 金錢圖标1

elif money_timers < 40 :

screen.blit(money_ic6,(WIDTH*0.24 7.5,25)) #繪制第二項 金錢圖标2

elif money_timers < 55 :

screen.blit(money_ic7,(WIDTH*0.24,25)) #繪制第二項 金錢圖标3

elif money_timers < 80 :

screen.blit(money_ic8,(WIDTH*0.24,25)) #繪制第二項 金錢圖标4

else:

money_timers = 0

return screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers

def Game_Timers(TimersSec,Gdata,time_passed,tim_psd) : #遊戲定時器

tim_psd = time_passed

if tim_psd >= 1000 : #為1秒的時候

TimersSec = 1

tim_psd = 0

Gdata[3] = 400 - TimersSec #遊戲所剩時間

return TimersSec,Gdata,time_passed,tim_psd

while True:

#事件檢測

for event in pygame.event.get():

if event.type == pygame.QUIT:

exit()

if event.type == KEYDOWN:

keys=pygame.key.get_pressed()

if keys[pygame.K_a]:

if event.key == K_w and inp_flag == 0:

if y <= HEIGHT-200: #看y坐标 必須在起點

jump_flag = 3 #按了上 和 向前

if y >= HEIGHT-200:#如果角色在平地才走動 後退 左

#if bg_w_1==0:

#x-=5

x-=(move_values 3.5)

inp_flag = 2

if keys[pygame.K_d]:

if event.key == K_w and inp_flag == 0:

if y <= HEIGHT-200: #看y坐标 必須在起點

jump_flag = 2 #按了上 和 向前

if y >= HEIGHT-200:#如果角色在平地才走動 前景 右

if x<(WIDTH/4): #角色還在屏幕左邊 可移動

x =(move_values 3.5)

inp_flag = 1

if keys[pygame.K_w]: #jump

jump_flag = 1 #僅僅是按了跳躍

jump_adder = 1 #跳躍累加器

if event.key == pygame.K_d and (jump_flag == 1):

if y == HEIGHT-200: #看y坐标 必須在起點

jump_flag = 2 #按了上 和 向前

if event.key == pygame.K_a and (jump_flag == 1):

if y == HEIGHT-200: #看y坐标 必須在起點

jump_flag = 3 #按了上 和 向後

if keys[pygame.K_p]: #重啟

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 =

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)

if event.type == KEYUP:

if keys[pygame.K_a]:

inp_flag = -1

if keys[pygame.K_d]:

inp_flag = -2

if keys[pygame.K_w]:

if jump_adder < 4 : #如果松開按鍵沒有達到jump_adder跳躍累加器的值 (那麼就他們置零)

jump_adder = 0

##在地面時 重設默認跳躍的最高點(還原)

jump_max_point = Reset_max_point(jump_max_point)

#jump action 1

if jump_flag == 1: #隻有跳躍

#讓其他方式跳躍值為 0

jump_values2 = 0

jump_values3 = 0

#------

#持續按鍵跳躍的結構

if jump_adder >=4 :

jump_max_point = 100#第二次跳躍最大值

jump_adder = 0

#------

jump_values =1.25

if jump_values <= jump_max_point:

y -= 5

x = jump_leftScreenBgnotMove(x)

if jump_max_point == 100:#跳躍的高度不同 y坐标的速度也要慢點

y = 1.5

x = jump_leftScreenBgnotMove(x)

elif jump_values <= jump_max_point 8:

pass

elif jump_values <=jump_max_point*2 8:

if HeroHeightIs(): #如果角色在控制 就繼續加y軸的值 1

y = 5

x = jump_leftScreenBgnotMove(x)

if jump_max_point == 100:#跳躍的高度不同 y坐标的速度也要慢點

y -= 1.5

x = jump_leftScreenBgnotMove(x)

else:

y = HEIGHT-200

jump_flag = 0

jump_values = 0

#wall detection

if x<=0:

x=0

if x hero.get_width()>WIDTH:

x=WIDTH-hero.get_width()

#角色的動作 函數

times,times2,inp_flag,hero = WalkAction(times,times2,inp_flag,hero)

#1 .bg move---blit

screen.blit(background,(bg_w_2,0))

screen.blit(background,(bg_w_1,0))

#繪制信息

screen.blit(mario_name,(WIDTH*0.03,3))#繪制第一項 名字

screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers =

Screen_MoneyIc(screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers) #繪制第二項 金錢圖标

screen.blit(Game_moneyX,(WIDTH*0.28,24))#繪制第二項 x

screen.blit(Game_world,(WIDTH*0.5-Game_world.get_width()/2,3))#繪制第三項 世界地圖

screen.blit(Game_time,(WIDTH*0.84,3))#繪制第四項 遊戲時間

for DATAi in range(4):

Game_data = mariofont.render("%s"% Gdata[DATAi],True,(255,255,128),None) #綜合繪制: 分數 金币 關卡 遊戲時間

if DATAi != 2:

screen.blit(Game_data,(WIDTH*(0.03 DATAi*0.27),24))

elif DATAi == 2:

Game_data = mariofont.render("%s-%s"% (Gdata[DATAi]/10,Gdata[DATAi]),True,(255,255,128),None) #綜合繪制: 分數 金币 關卡 遊戲時間

screen.blit(Game_data,(WIDTH*0.5-Game_data.get_width()/2,15))

#繪制信息

#2 .bg move--panel

#if inp_flag == 2: #往左走 壁紙向右拖動

#bg_w_1 =move_values/4

#bg_w_2 =move_values/4

if inp_flag == 1 and x>=(WIDTH/4):#往右走 壁紙向左拖動

bg_w_1-=(move_values/4-0.5)

bg_w_2-=(m#!/usr/bin/env python

# -*- coding:utf-8 -*-

import pygame,os,wx

from random import randint

from sys import exit

from pygame.locals import *

pygame.init()

def main():

#獲取屏幕大小

app=wx.App()

WHFRAMES=wx.DisplaySize()

WIDTH=int(WHFRAMES[0]*0.7)

HEIGHT=int(WHFRAMES[1]*0.8)

Timers = 0 #遊戲定時器

TimersSec = 0 #秒

tim_psd = 0

#獲取屏幕大小

screen=pygame.display.set_mode((WIDTH,HEIGHT),0,32)

caption=pygame.display.set_caption("超級馬裡奧")

screen.fill([255,255,255])

mariofont = pygame.font.Font('fonts/poster.ttf',22)

mario_name = mariofont.render("MARIO",True,(84,65,190),None)

#Game_world = mariofont.render("WORLD",True,(84,65,190),None)

Game_moneyX = mariofont.render("X",True,(255,255,128),None)

Game_time = mariofont.render("TIME",True,(84,65,190),None)

money_ic5 = pygame.image.load('images/PTModelSprite_ID21675.png')

money_ic5 = pygame.transform.scale(money_ic5, (25, 25))

money_ic6 = pygame.image.load('images/PTModelSprite_ID21676.png')

money_ic6 = pygame.transform.scale(money_ic6, (10, 25))

money_ic7 = pygame.image.load('images/PTModelSprite_ID21677.png')

money_ic7 = pygame.transform.scale(money_ic7, (25, 25))

money_ic8 = pygame.image.load('images/PTModelSprite_ID21678.png')

money_ic8 = pygame.transform.scale(money_ic8, (25, 25))

money_timers = 0 #圖片輪播定時器

Game_world = pygame.image.load('images/PTModelSprite_ID2478.png')

background = pygame.image.load('images/PTModelSprite_ID35342.png').convert_alpha()

background = pygame.transform.scale(background, (WIDTH, HEIGHT))

Roads = pygame.image.load('images/PTModelSprite_ID3790.png').convert_alpha()

Roads2 = pygame.image.load('images/PTModelSprite_ID4224.png').convert_alpha()

hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()

x,y = 15,HEIGHT-200

inp_flag = -2 #(stop:-1 left drection ,-2 right drection) ,(walk:1 right drection ,2 left drection)

times,times2 = 0,0 #人物動作定時器

move_values,jump_values,jump_values2,jump_values3 = 12,0,0,0 #一步移動的距離 和 跳躍的值1,2

jump_adder,jump_max_point = 0,50 #跳躍累加器 用來累加按鍵的長短 然後判斷跳躍的高度,跳躍的初始值最高點

jump_flag = 0

bg_w_1,bg_w_2 = 0,WIDTH-2 #兩張壁紙 一前一後循環拖動的變量

#播放背景

#播放背景

#遊戲信息數據定義

score = 0

money = 0

world = 11

time = 400

Gdata = [score,money,world,time]

#遊戲信息數據定義

#初始化函數

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):#數據初始化

#遊戲初始化數據

inp_flag = -2 #(stop:-1 left drection ,-2 right drection) ,(walk:1 right drection ,2 left drection)

x,y = 15,HEIGHT-200 #馬裡奧坐标

times,times2 = 0,0 #人物動作定時器

move_values,jump_values,jump_values2,jump_values3 = 12,0,0,0 #一步移動的距離 和 跳躍的值1,2

jump_adder,jump_max_point = 0,50 #跳躍累加器 用來累加按鍵的長短 然後判斷跳躍的高度,跳躍的初始值最高點

jump_flag = 0

tim_psd = 0

bg_w_1,bg_w_2 = 0,WIDTH-2 #兩張壁紙 一前一後循環拖動的變量

Timers = 0 #遊戲定時器

score = 0 #開始分數

money = 0 #開始金錢

world = 11 #世界關卡第一關1-1 = 11

time = 400#遊戲總時間

TimersSec = 0 #遊戲裡的秒

Gdata = [score,money,world,time]

#遊戲初始化數據

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

#初始化函數

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 =

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)#數據初始化主調函數

clock = pygame.time.Clock()

pygame.key.set_repeat(55)

pygame.display.flip()

def WalkAction(times,times2,inp_flag,hero):

#walk action

if y < HEIGHT -200: #如果在空中 為跳躍圖片

if inp_flag == 1: #right

hero = pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()

if inp_flag == 2: #left

hero = pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

else:

if inp_flag == 1: #right

times = 2

if times < 20:

hero = pygame.image.load('images/PTModelSprite_ID34256.png').convert_alpha()

elif times < 20:

hero = pygame.image.load('images/PTModelSprite_ID34257..png').convert_alpha()

elif times < 40:

hero = pygame.image.load('images/PTModelSprite_ID34258.png').convert_alpha()

elif times < 60:

hero = pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()

elif times < 80:

hero = pygame.image.load('images/PTModelSprite_ID34260.png').convert_alpha()

elif times < 100:

hero = pygame.image.load('images/PTModelSprite_ID34261.png').convert_alpha()

elif times < 120:

hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()

elif times < 140:

times = 0

if inp_flag == 2: #left

times2 = 2

if times2 < 20:

hero = pygame.image.load('images/PTModelSprite_ID34256.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

elif times2 < 20:

hero = pygame.image.load('images/PTModelSprite_ID34257..png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

elif times2 < 40:

hero = pygame.image.load('images/PTModelSprite_ID34258.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

elif times2 < 60:

hero = pygame.image.load('images/PTModelSprite_ID34259.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

elif times2 < 80:

hero = pygame.image.load('images/PTModelSprite_ID34260.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

elif times2 < 100:

hero = pygame.image.load('images/PTModelSprite_ID34261.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

elif times2 < 120:

hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

elif times2 < 140:

times2 = 0

elif inp_flag == -1:

hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()

hero = pygame.transform.flip(hero, True, False)

times2 = 0

elif inp_flag == -2:

hero = pygame.image.load('images/PTModelSprite_ID34297.png').convert_alpha()

times2 = 0

return times,times2,inp_flag,hero

def HeroHeightIs(): #判斷角色是否為地面y軸

if y >= HEIGHT-200:

return False

else:#這是在控制的狀況

return True

def Reset_max_point(jump_max_point):#在地面重設默認跳躍的最高點(還原)

if y >= (HEIGHT-200):

jump_max_point = 50 #默認最高點是 50

return jump_max_point

def jump_leftScreenBgnotMove(x):

if x<(WIDTH/4):

if jump_max_point == 50 :

if inp_flag == 1:

x =(2.7)

if inp_flag == 2:

x-=(2.7)

elif jump_max_point == 100 :

if inp_flag == 1:

x =(0.27)

if inp_flag == 2:

x-=(0.27)

return x

def Screen_MoneyIc(screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers) : #繪制第二項 金錢圖标

money_timers = 1

if money_timers < 15 :

screen.blit(money_ic5,(WIDTH*0.24,25)) #繪制第二項 金錢圖标1

elif money_timers < 40 :

screen.blit(money_ic6,(WIDTH*0.24 7.5,25)) #繪制第二項 金錢圖标2

elif money_timers < 55 :

screen.blit(money_ic7,(WIDTH*0.24,25)) #繪制第二項 金錢圖标3

elif money_timers < 80 :

screen.blit(money_ic8,(WIDTH*0.24,25)) #繪制第二項 金錢圖标4

else:

money_timers = 0

return screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers

def Game_Timers(TimersSec,Gdata,time_passed,tim_psd) : #遊戲定時器

tim_psd = time_passed

if tim_psd >= 1000 : #為1秒的時候

TimersSec = 1

tim_psd = 0

Gdata[3] = 400 - TimersSec #遊戲所剩時間

return TimersSec,Gdata,time_passed,tim_psd

while True:

#事件檢測

for event in pygame.event.get():

if event.type == pygame.QUIT:

exit()

if event.type == KEYDOWN:

keys=pygame.key.get_pressed()

if keys[pygame.K_a]:

if event.key == K_w and inp_flag == 0:

if y <= HEIGHT-200: #看y坐标 必須在起點

jump_flag = 3 #按了上 和 向前

if y >= HEIGHT-200:#如果角色在平地才走動 後退 左

#if bg_w_1==0:

#x-=5

x-=(move_values 3.5)

inp_flag = 2

if keys[pygame.K_d]:

if event.key == K_w and inp_flag == 0:

if y <= HEIGHT-200: #看y坐标 必須在起點

jump_flag = 2 #按了上 和 向前

if y >= HEIGHT-200:#如果角色在平地才走動 前景 右

if x<(WIDTH/4): #角色還在屏幕左邊 可移動

x =(move_values 3.5)

inp_flag = 1

if keys[pygame.K_w]: #jump

jump_flag = 1 #僅僅是按了跳躍

jump_adder = 1 #跳躍累加器

if event.key == pygame.K_d and (jump_flag == 1):

if y == HEIGHT-200: #看y坐标 必須在起點

jump_flag = 2 #按了上 和 向前

if event.key == pygame.K_a and (jump_flag == 1):

if y == HEIGHT-200: #看y坐标 必須在起點

jump_flag = 3 #按了上 和 向後

if keys[pygame.K_p]: #重啟

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 =

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)

if event.type == KEYUP:

if keys[pygame.K_a]:

inp_flag = -1

if keys[pygame.K_d]:

inp_flag = -2

if keys[pygame.K_w]:

if jump_adder < 4 : #如果松開按鍵沒有達到jump_adder跳躍累加器的值 (那麼就他們置零)

jump_adder = 0

##在地面時 重設默認跳躍的最高點(還原)

jump_max_point = Reset_max_point(jump_max_point)

#jump action 1

if jump_flag == 1: #隻有跳躍

#讓其他方式跳躍值為 0

jump_values2 = 0

jump_values3 = 0

#------

#持續按鍵跳躍的結構

if jump_adder >=4 :

jump_max_point = 100#第二次跳躍最大值

jump_adder = 0

#------

jump_values =1.25

if jump_values <= jump_max_point:

y -= 5

x = jump_leftScreenBgnotMove(x)

if jump_max_point == 100:#跳躍的高度不同 y坐标的速度也要慢點

y = 1.5

x = jump_leftScreenBgnotMove(x)

elif jump_values <= jump_max_point 8:

pass

elif jump_values <=jump_max_point*2 8:

if HeroHeightIs(): #如果角色在控制 就繼續加y軸的值 1

y = 5

x = jump_leftScreenBgnotMove(x)

if jump_max_point == 100:#跳躍的高度不同 y坐标的速度也要慢點

y -= 1.5

x = jump_leftScreenBgnotMove(x)

else:

y = HEIGHT-200

jump_flag = 0

jump_values = 0

#wall detection

if x<=0:

x=0

if x hero.get_width()>WIDTH:

x=WIDTH-hero.get_width()

#角色的動作 函數

times,times2,inp_flag,hero = WalkAction(times,times2,inp_flag,hero)

#1 .bg move---blit

screen.blit(background,(bg_w_2,0))

screen.blit(background,(bg_w_1,0))

#繪制信息

screen.blit(mario_name,(WIDTH*0.03,3))#繪制第一項 名字

screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers =

Screen_MoneyIc(screen,money_ic5,money_ic6,money_ic7,money_ic8,money_timers) #繪制第二項 金錢圖标

screen.blit(Game_moneyX,(WIDTH*0.28,24))#繪制第二項 x

screen.blit(Game_world,(WIDTH*0.5-Game_world.get_width()/2,3))#繪制第三項 世界地圖

screen.blit(Game_time,(WIDTH*0.84,3))#繪制第四項 遊戲時間

for DATAi in range(4):

Game_data = mariofont.render("%s"% Gdata[DATAi],True,(255,255,128),None) #綜合繪制: 分數 金币 關卡 遊戲時間

if DATAi != 2:

screen.blit(Game_data,(WIDTH*(0.03 DATAi*0.27),24))

elif DATAi == 2:

Game_data = mariofont.render("%s-%s"% (Gdata[DATAi]/10,Gdata[DATAi]),True,(255,255,128),None) #綜合繪制: 分數 金币 關卡 遊戲時間

screen.blit(Game_data,(WIDTH*0.5-Game_data.get_width()/2,15))

#繪制信息

#2 .bg move--panel

#if inp_flag == 2: #往左走 壁紙向右拖動

#bg_w_1 =move_values/4

#bg_w_2 =move_values/4

if inp_flag == 1 and x>=(WIDTH/4):#往右走 壁紙向左拖動

bg_w_1-=(move_values/4-0.5)

bg_w_2-=(move_values/4-0.5)

if bg_w_1>=0:

bg_w_1,bg_w_2 = 0,WIDTH-2

if bg_w_1<-WIDTH:

bg_w_1,bg_w_2 = 0,WIDTH-2

screen.blit(hero,(x,y))

pygame.time.delay(2) #毫秒

time_passed = clock.tick()

TimersSec,Gdata,time_passed,tim_psd = Game_Timers(TimersSec,Gdata,time_passed,tim_psd) #遊戲定時

pygame.display.update()

if __name__ == '__main__':

main()ove_values/4-0.5)

if bg_w_1>=0:

bg_w_1,bg_w_2 = 0,WIDTH-2

if bg_w_1<-WIDTH:

bg_w_1,bg_w_2 = 0,WIDTH-2

screen.blit(hero,(x,y))

pygame.time.delay(2) #毫秒

time_passed = clock.tick()

TimersSec,Gdata,time_passed,tim_psd = Game_Timers(TimersSec,Gdata,time_passed,tim_psd) #遊戲定時

pygame.display.update()

if __name__ == '__main__':

main()

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
上坂堇配音龍女(知名聲優上坂堇加入)
上坂堇配音龍女(知名聲優上坂堇加入)
  美食拟人手遊《食之契約》本周公開了全新飨靈角色,受到了玩家們的廣泛關注和轉發,特别還邀請了知名聲優上坂堇加入為它配音,讓我們來看看《食之契約》缇爾菈大陸的大陸中這次加入了什麼樣的新飨靈角色呢?   【祈願庇佑 禦節料理】      美味的禦節料理相當于是日本人的年夜飯,它會用多層的盒子裝置,每一層裝置的不同的食物擁有着不同的寓意。第一層主要會有一些象征喜...
2025-11-14
貝侬鮮果(貝侬來了好解紛)
貝侬鮮果(貝侬來了好解紛)
     圖為法官與“貝侬”在火盆邊,向被告釋明案件的利害關系。記者 吳 琪 攝   春寒料峭,細雨蒙蒙。2月8日清晨5點,廣西壯族自治區河池市宜州區同德鄉闆高村的蒙軍(化名)要趕往200多公裡外的馬山縣古零鎮。趁着春節期間,他要向包工頭林明(化名)追回拖欠了近三年的工資。   2019年下半年,他接到林明發包的建築工程後,便帶着十餘名建築工人進場施工。期間...
2025-11-14
最新韓國古裝高分b級電影推薦(豆瓣8.3分的R級片)
最新韓國古裝高分b級電影推薦(豆瓣8.3分的R級片)
  美輪美奂的西部風景,動聽悅耳的音樂,六則寓意深刻的故事娓娓道來,看科恩兄弟的新作,再一次被故事的強烈宿命感震撼到,看完回味無窮。   時隔兩年,伊桑·科恩和喬爾·科恩終于又帶來一部新作《巴斯特·斯克魯格斯的歌謠》,看片名差點以為是紀錄片,又或者歌舞片。      《巴斯特·斯克魯格斯的歌謠》在豆瓣評分高達8.3分,好于92%的西部片,好于92%的喜劇片,...
2025-11-14
和田玉羊脂玉怎麼識别真假的方法(和田玉的謠言一)
和田玉羊脂玉怎麼識别真假的方法(和田玉的謠言一)
  一說到和田玉,大家想到最多的詞語便是“羊脂玉”,好多商家也說自己的玉是羊脂玉,那到底什麼是羊脂玉,真的很稀缺珍貴嗎?   關于“羊脂玉”,大家有許多不了解,市場上也有很多羊脂玉的謠言,下面我用5句話給大家說清楚“羊脂玉”的前世今生。   1、在三國時代,曹丕曾在《與鐘繇謝玉玦書》中曰:“竊見玉書,稱美玉白如截肪,黑譬純漆,赤拟雞冠,黃侔蒸栗” 這是史料中...
2025-11-14
上海首套房契稅交多少錢(上海首套房契稅優惠繼續實施)
上海首套房契稅交多少錢(上海首套房契稅優惠繼續實施)
     9月1日起,《契稅法》正式施行。   關于之前的上海住房契稅優惠是否繼續?成為近期大家重點關注的話題。終于,财政部官網最新公告為我們揭開了這個答案。   8月30日,财政部稅政司網站發布了《關于契稅法實施後有關優惠政策銜接問題的公告》(财政部 稅務總局公告2021年第29号),《公告》明确了《中華人民共和國契稅法》實施後繼續執行的契稅優惠政策。  ...
2025-11-14
Copyright 2023-2025 - www.tftnews.com All Rights Reserved