import json
import sys
import os
from mario.classes.SpriteSheet import SpriteSheet
from mario.classes.Utils import Utils
import cairo
import wx
class Menu:
def __init__(self, screen, dashboard, level, sound):
self.screen = screen
self.sound = sound
self.start = False
self.in_settings = False
self.state = 0
self.level = level
self.music = True
self.sfx = True
self.current_selected_level = 1
self.level_names = []
self.in_choosing_level = False
self.dashboard = dashboard
self.level_count = 0
self.spritesheet = SpriteSheet("./img/title_screen.png")
self.menu_banner = self.spritesheet.image_at(
0,
60,
2,
color_key=[255, 0, 220],
is_column_row=False,
tile_width=180,
tile_height=88,
)
self.menu_dot = self.spritesheet.image_at(
0, 150, 2, color_key=[255, 0, 220], is_column_row=False
)
self.menu_dot2 = self.spritesheet.image_at(
20, 150, 2, color_key=[255, 0, 220], is_column_row=False
)
self.load_settings("./settings.json")
def update(self, event):
self.check_input(event)
if self.in_choosing_level:
return
self.draw_menu_background()
self.dashboard.update()
if not self.in_settings:
self.draw_menu()
else:
self.draw_settings()
def draw_dot(self):
if self.state == 0:
Utils.blit(self.screen, self.menu_dot, 145, 273)
Utils.blit(self.screen, self.menu_dot2, 145, 313)
Utils.blit(self.screen, self.menu_dot2, 145, 353)
elif self.state == 1:
Utils.blit(self.screen, self.menu_dot, 145, 313)
Utils.blit(self.screen, self.menu_dot2, 145, 273)
Utils.blit(self.screen, self.menu_dot2, 145, 353)
elif self.state == 2:
Utils.blit(self.screen, self.menu_dot, 145, 353)
Utils.blit(self.screen, self.menu_dot2, 145, 273)
Utils.blit(self.screen, self.menu_dot2, 145, 313)
def load_settings(self, url):
try:
with open(url, "r") as json_data:
data = json.load(json_data)
if data["sound"]:
self.music = True
self.sound.play_music(self.sound.soundtrack)
else:
self.music = False
if data["sfx"]:
self.sfx = True
self.sound.allow_sfx = True
else:
self.sound.allow_sfx = False
self.sfx = False
except (IOError, OSError):
self.music = False
self.sound.allow_sfx = False
self.sfx = False
self.save_settings("./settings.json")
def save_settings(self, url):
data = {"sound": self.music, "sfx": self.sfx}
with open(url, "w") as outfile:
json.dump(data, outfile)
def draw_menu(self):
self.draw_dot()
self.dashboard.draw_text("CHOOSE LEVEL", 180, 280, 24)
self.dashboard.draw_text("SETTINGS", 180, 320, 24)
self.dashboard.draw_text("EXIT", 180, 360, 24)
def draw_menu_background(self, with_banner=True):
for y in range(0, 13):
for x in range(0, 20):
Utils.blit(self.screen, self.level.sprites.spriteCollection.get("sky").image, x * 32, y * 32)
for y in range(13, 15):
for x in range(0, 20):
Utils.blit(self.screen, self.level.sprites.spriteCollection.get("ground").image, x * 32, y * 32)
if with_banner:
Utils.blit(self.screen, self.menu_banner, 150, 80)
Utils.blit(self.screen, self.level.sprites.spriteCollection.get("mario_idle").image, 2 * 32, 12 * 32)
Utils.blit(self.screen, self.level.sprites.spriteCollection.get("bush_1").image, 14 * 32, 12 * 32)
Utils.blit(self.screen, self.level.sprites.spriteCollection.get("bush_2").image, 15 * 32, 12 * 32)
Utils.blit(self.screen, self.level.sprites.spriteCollection.get("bush_2").image, 16 * 32, 12 * 32)
Utils.blit(self.screen, self.level.sprites.spriteCollection.get("bush_2").image, 17 * 32, 12 * 32)
Utils.blit(self.screen, self.level.sprites.spriteCollection.get("bush_3").image, 18 * 32, 12 * 32)
Utils.blit(self.screen, self.level.sprites.spriteCollection.get("goomba-1").image, 18.5 * 32, 12 * 32)
def draw_settings(self):
self.draw_dot()
self.dashboard.draw_text("MUSIC", 180, 280, 24)
if self.music:
self.dashboard.draw_text("ON", 340, 280, 24)
else:
self.dashboard.draw_text("OFF", 340, 280, 24)
self.dashboard.draw_text("SFX", 180, 320, 24)
if self.sfx:
self.dashboard.draw_text("ON", 340, 320, 24)
else:
self.dashboard.draw_text("OFF", 340, 320, 24)
self.dashboard.draw_text("BACK", 180, 360, 24)
def choose_level(self):
self.draw_menu_background(False)
self.in_choosing_level = True
self.level_names = self.load_level_names()
self.draw_level_chooser()
def draw_border(self, x, y, width, height, color, thickness):
ctx = cairo.Context(self.screen)
ctx.set_source_rgb(*color)
# ctx.set_line_width(thickness)
ctx.rectangle(x, y, width, thickness)
ctx.rectangle(x, y + width, width, thickness)
ctx.rectangle(x, y, thickness, width)
ctx.rectangle(x + width, y, thickness, width + thickness)
ctx.fill()
def draw_level_chooser(self):
j = 0
offset = 75
text_offset = 90
for i, level_name in enumerate(self.load_level_names()):
if self.current_selected_level == i + 1:
color = (255, 255, 255)
else:
color = (150, 150, 150)
if i < 3:
self.dashboard.draw_text(level_name, 175 * i + text_offset, 100, 12)
self.draw_border(175 * i + offset, 55, 125, 75, color, 5)
else:
self.dashboard.draw_text(level_name, 175 * j + text_offset, 250, 12)
self.draw_border(175 * j + offset, 210, 125, 75, color, 5)
j += 1
def load_level_names(self):
files = []
res = []
for r, d, f in os.walk("./levels"):
for file in f:
files.append(os.path.join(r, file))
for f in files:
res.append(os.path.split(f)[1].split(".")[0])
self.level_count = len(res)
return res
def check_input(self, event):
event_type = event.GetEventType()
if event_type == wx.EVT_CLOSE.typeId:
sys.exit()
if event_type == wx.EVT_KEY_DOWN.typeId:
keycode = event.GetKeyCode()
if keycode == wx.WXK_ESCAPE:
if self.in_choosing_level or self.in_settings:
self.in_choosing_level = False
self.in_settings = False
self.__init__(self.screen, self.dashboard, self.level, self.sound)
else:
sys.exit()
elif keycode == wx.WXK_UP or keycode == ord("k"):
if self.in_choosing_level:
if self.current_selected_level > 3:
self.current_selected_level -= 3
self.draw_level_chooser()
if self.state > 0:
self.state -= 1
elif keycode == wx.WXK_DOWN or keycode == ord("j"):
if self.in_choosing_level:
if self.current_selected_level + 3 <= sel
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
烂尾,待完成 main.py 和一些关于窗口上下文及事件处理的 TODO 部分,此代码仅用作记录。 部分过程记录:https://blog.csdn.net/SmileBasic/article/details/138549966 wxPython 对于写游戏并不是一个好的选择,对碰撞检测、声音播放、精灵图等功能都没有完善的封装支持。用它写游戏可以学习巩固一些游戏的基础知识,并帮助了解 wxPython 的 API 的使用和一些原理,更好地编写 GUI 程序。 高 Fork 项目也可能会是烂代码,以后记得先选结构良好的。 原项目: https://github.com/mx0c/super-mario-python
资源推荐
资源详情
资源评论
收起资源包目录
mario.zip (74个子文件)
mario
classes
Font.py 1KB
__init__.py 0B
EntityCollider.py 2KB
Level.py 8KB
Pause.py 2KB
GaussianBlur.py 580B
Animation.py 1KB
Collider.py 3KB
Dashboard.py 2KB
Camera.py 511B
Sprite.py 907B
Utils.py 2KB
Menu.py 10KB
Maths.py 89B
Sprites.py 3KB
Tile.py 756B
Sound.py 2KB
Input.py 3KB
SpriteSheet.py 2KB
README 632B
main.py 6B
runtest.py 219B
img
1.PNG 23KB
tiles.png 38KB
2.PNG 17KB
title_screen.png 6KB
characters.png 29KB
4.PNG 154KB
characters.gif 23KB
Items.png 46KB
pics.png 272KB
koopas.png 54KB
3.PNG 19KB
font.png 16KB
pictures.PNG 110KB
yoshis.png 51KB
sfx
small_jump.ogg 7KB
brick-bump.ogg 7KB
powerup.ogg 11KB
pipe.ogg 10KB
stomp.ogg 6KB
powerup_appears.ogg 8KB
kick.ogg 5KB
main_theme.ogg 2.76MB
death.wav 118KB
bump.ogg 5KB
coin.ogg 8KB
entities
Mushroom.py 2KB
__init__.py 0B
RandomBox.py 2KB
Goomba.py 3KB
CoinBrick.py 1KB
Koopa.py 3KB
EntityBase.py 1KB
Mario.py 7KB
CoinBox.py 2KB
Coin.py 638B
Item.py 2KB
test
test_SpriteSheet.py 2KB
sprites
Animations.json 1KB
BackgroundSprites.json 3KB
Mario.json 3KB
translate_rules.json 681B
RedMushroom.json 287B
ItemAnimations.json 724B
Goomba.json 660B
Koopa.json 844B
levels
Level1-1.json 3KB
Level1-2.json 2KB
traits
__init__.py 0B
jump.py 1KB
bounce.py 458B
leftrightwalk.py 751B
go.py 3KB
共 74 条
- 1
资源评论
SmileBasic
- 粉丝: 350
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 【岗位职责说明书】100000709 财务高级主管.doc
- 【岗位职责说明书】100000708 人力资源高级主管.doc
- 【岗位职责说明书】100000713 安全保卫高级主管.doc
- 【岗位职责说明书】100000803 综合行政主管.doc
- 【岗位职责说明书】100000802 财务部副经理(分公司).doc
- 【岗位职责说明书】100000800 室(职能部室)副经理.doc
- 【岗位职责说明书】100000807 工商法律事务主管.doc
- 【岗位职责说明书】100000808 档案管理主管.doc
- 【岗位职责说明书】100000809 信息管理主管.doc
- 【岗位职责说明书】100000818 会计主管.doc
- 【岗位职责说明书】100000814 绩效考核主管.doc
- 【岗位职责说明书】100000820 审计主管.doc
- 【岗位职责说明书】100000813 薪酬主管.doc
- 【岗位职责说明书】100000812 培训主管.doc
- 【岗位职责说明书】100000815 员工关系主管.doc
- 【岗位职责说明书】100000816 社会保险主管.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功