拯救蘑菇项目源代码抢先看!


🚀 拯救蘑菇项目源代码大揭秘! 🚀


🌟 项目概述

拯救蘑菇项目 — 一场智慧与勇气的冒险之旅,现已全面开源!本项目基于Python开发,旨在打造一个充满趣味与挑战的迷宫探险游戏。欢迎各位开发者参与二次创作,共同探索未知的编程世界!


📜 项目声明

重要提示:本项目为完全开源项目,鼓励大家进行自由创作与改进。但请注意,任何因使用本项目代码而产生的问题,本工作室不承担任何责任。


📚 项目信息

  • 项目管理员:一名程序员、小小程序员、那神明、陈红
  • 指导教师:陈红
  • 文章编写:一名程序员
  • 项目语言:Python

📁 项目文件结构

主文件夹

  • main.py
  • game.mp3

images文件夹

  • button.png
  • door.png
  • ghost.png
  • h_d.png
  • h_l.png
  • h_r.png
  • h_u.png
  • key.png
  • lose.png
  • map.png
  • princess.png
  • start.png
  • win.png

💻 Python程序源代码预览

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import pygame, sys
from pygame.locals import *

class Hero(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.images = [pygame.image.load('images/h_u.png'),
pygame.image.load('images/h_d.png'),
pygame.image.load('images/h_l.png'),
pygame.image.load('images/h_r.png')]
self.image = self.images[3]
self.rect = self.image.get_rect(x=10, y=700)
self.speed = 3
self.direction = 0
self.move = False

def update(self):
if self.move:
if self.direction == 'up' and self.rect.y > 10:
self.rect.y -= self.speed
self.image = self.images[0]
elif self.direction == 'down' and self.rect.y < 720:
self.rect.y += self.speed
self.image = self.images[1]
elif self.direction == 'left' and self.rect.x > 10:
self.rect.x -= self.speed
self.image = self.images[2]
elif self.direction == 'right' and self.rect.x < 1320:
self.rect.x += self.speed
self.image = self.images[3]
self.move = False

def collide_color(self, screen, color):
pixel = pygame.PixelArray(screen)
pixel = pixel[self.rect.x:self.rect.x+self.rect.w,
self.rect.y:self.rect.y+self.rect.h]
return color in pixel

class Ghost(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = pygame.image.load('images/ghost.png')
self.pos = pos
self.rect = self.image.get_rect(x=pos[0], y=pos[1])
self.speed = 2
def update(self):
if self.rect.x > self.pos[2] or self.rect.x < self.pos[0]:
self.speed = -self.speed
self.rect.x += self.speed

class Prop(pygame.sprite.Sprite):
def __init__(self, pos, name):
super().__init__()
self.image = pygame.image.load('images/{}.png'.format(name))
self.rect = self.image.get_rect(x=pos[0], y=pos[1])

def collide(s1, s2):
return pygame.sprite.collide_rect(s1, s2)

pygame.init()
pygame.key.set_repeat(1, 1)
WIDTH = 1400
HEIGHT = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("拯救蘑菇")
bg = [pygame.image.load('images/start.png'),
pygame.image.load('images/map.png'),
pygame.image.load('images/win.png'),
pygame.image.load('images/lose.png')]
clock = pygame.time.Clock()
state = 0
btn = pygame.image.load('images/button.png')
btn_rect = btn.get_rect(x=520, y=390)
pygame.mixer.music.load('game.mp3')
pygame.mixer.music.play(-1)

hero = Hero()
g1 = Ghost((0, 20, 400))
g2 = Ghost((610, 20, 1100))
g3 = Ghost((330, 460, 710))
g4 = Ghost((710, 360, 1270))
key = Prop((330, 360),'key')
door = Prop((1240, 0), 'door')
princess = Prop((1290, 40), 'princess')
group = pygame.sprite.Group()
group.add(hero,
g1, g2, g3, g4,
key, door, princess)

while True:
clock.tick(60)
pygame.display.flip()

if state == 0:
screen.blit(bg[0], (0, 0))
screen.blit(btn, btn_rect)
elif state == 1:
screen.blit(bg[1], (0, 0))
group.draw(screen)
group.update()
if collide(hero, g1) or collide(hero, g2) or collide(hero, g3) or collide(hero, g4):
state = 3
if collide(hero, door) and key in group:
state = 3
if collide(hero, key):
group.remove(door, key)
if collide(hero, princess):
state = 2
# 挑战8-开始
if hero.collide_color(screen, (255, 225, 101)):
state = 3
# 挑战8-结束
elif state == 2:
screen.blit(bg[2], (0, 0))
pygame.mixer.music.fadeout(3000)
elif state == 3:
screen.blit(bg[3], (0, 0))
pygame.mixer.music.fadeout(3000)

for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
mouse = pygame.mouse.get_pos()
if btn_rect.collidepoint(mouse):
state = 1
elif event.type == KEYDOWN:
if event.key == K_UP:
hero.move = True
hero.direction = 'up'
elif event.key == K_DOWN:
hero.move = True
hero.direction = 'down'
elif event.key == K_LEFT:
hero.move = True
hero.direction = 'left'
elif event.key == K_RIGHT:
hero.move = True
hero.direction = 'right'

🔗 友情链接

想要直接获取完整的项目代码及文件?点击下方链接,即刻开启你的冒险之旅!

点此获取代码+文件


快来加入拯救蘑菇的行列,用你的智慧和勇气,解开迷宫的秘密,拯救被困的蘑菇吧! 🎉🎉🎉

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2015-2024 DurkSkyWorkShop
  • Visitors: | Views:

谢谢你对我的支持~

支付宝
微信