Compare commits

..
1 Commits
Author SHA1 Message Date
zhiyuan b581f4def2 尝试调整结构,添加各项类组 2026-08-07 22:06:36 +08:00
12 changed files with 44 additions and 340 deletions

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName: Game_project
# FileName: colorconf.py
# Author: Zhi_yuan
# Datetime: 2026/8/7 下午9:21
# Description:
# ---------------------------------------------------------------------------
class Color:
def __init__(self):
self.white = (255, 255, 255)
self.red = (255, 0, 0)
self.blue = (0, 0, 255)
+25
View File
@@ -0,0 +1,25 @@
# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName: Game_project
# FileName: pageconf.py
# Author: Zhi_yuan
# Datetime: 2026/8/7 下午9:34
# Description:
# ---------------------------------------------------------------------------
import pygame
class Page:
def __init__(self):
# pygame初始化
pygame.init()
# 音乐模块初始化
pygame.mixer.init()
# 大小设置
size = (800, 600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("New_game")
if __name__ == "__main__":
Page()
Binary file not shown.
+6 -8
View File
@@ -6,10 +6,8 @@
# Datetime: 2026/7/25 下午10:44
# Description:
# ---------------------------------------------------------------------------
import os
import pygame
from src.config.game_config import data_path
from common.colorconf import Color
# 初始化pygame
pygame.init()
@@ -25,8 +23,7 @@ screen = pygame.display.set_mode(size)
pygame.display.set_caption("New_game")
# 播放并循环音乐
data_path = data_path()
pygame.mixer_music.load(os.path.join(data_path, "vespidaze-upbeat-rpg-battle-460971.mp3"))
pygame.mixer_music.load(r'D:\python_project\Game_project\file\vespidaze-upbeat-rpg-battle-460971.mp3')
pygame.mixer_music.play(-1)
# 状态值
@@ -39,9 +36,10 @@ up_status = False
down_status = False
# 颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
color = Color()
WHITE = color.white
RED = color.red
BLUE = color.blue
# 游戏循环
running = True
while running:
-6
View File
@@ -1,6 +0,0 @@
class Color:
black = (0, 0, 0)
white = (255, 255, 255)
a = (255, 0, 0)
b = (0, 255, 0)
c = (0, 0, 255)
-17
View File
@@ -1,17 +0,0 @@
import os
class GameConfig:
game_name = "Test Game"
window_x = 1920/2
window_y = 1080/2
bg_color = (0, 0, 0)
def data_path():
file_path = os.path.abspath(__file__)
project_path = os.path.dirname(os.path.dirname(os.path.dirname(file_path)))
data_path = os.path.join(project_path, "data")
return data_path
if __name__ == '__main__':
print(data_path())
-8
View File
@@ -1,8 +0,0 @@
from src.scenes.main_menu_scene import MainMenuScene
from src.scenes.game_scene import GameScene
scene_dict = {
"MainMenuScene": MainMenuScene,
"GameScene": GameScene,
}
-28
View File
@@ -1,28 +0,0 @@
import pygame
class BaseScene:
def __init__(self, screen: pygame.Surface, clock: pygame.time.Clock):
"""
初始化场景,接收并保存画布、计时器
"""
self.screen = screen
self.clock = clock
self.next_scene = None
def update(self):
"""
场景的每帧更新逻辑
"""
pass
def handle_event(self, events):
"""
接收主循环传来的事件并处理
"""
pass
def switch_to_scene(self, scene_name):
"""
切换场景
"""
self.next_scene = scene_name
-82
View File
@@ -1,82 +0,0 @@
import os
import pygame
from src.config.game_config import data_path, GameConfig
from src.scenes.base_scene import BaseScene
class GameScene(BaseScene):
name = "GameScene"
def __init__(self, screen, clock):
super().__init__(screen, clock)
# 初始化音乐
pygame.mixer.init()
# 播放并循环音乐
pygame.mixer_music.load(os.path.join(data_path(), "vespidaze-upbeat-rpg-battle-460971.mp3"))
pygame.mixer_music.play(-1)
self.back_status = False
# 状态值
self.dirty_x = 400
self.dirty_y = 300
self.Left_status = False
self.right_status = False
self.up_status = False
self.down_status = False
# 颜色
self.WHITE = (255, 255, 255)
self.RED = (255, 0, 0)
self.BLUE = (0, 0, 255)
def update(self):
if self.back_status:
self.switch_to_scene("MainMenuScene")
if self.Left_status and self.dirty_x >= 0:
self.dirty_x -= 1
if self.right_status and self.dirty_x <= GameConfig.window_x - 20:
self.dirty_x += 1
if self.up_status and self.dirty_y >= 0:
self.dirty_y -= 1
if self.down_status and self.dirty_y <= GameConfig.window_y - 30:
self.dirty_y += 1
# 填充窗口颜色
self.screen.fill((255, 255, 255))
# 绘制矩形(移动体和固定体)
dirty = pygame.draw.rect(self.screen, self.BLUE, [self.dirty_x, self.dirty_y, 20, 30])
enemy = pygame.draw.rect(self.screen, self.RED, [40, 60, 10, 10])
# 碰撞监测
if dirty.colliderect(enemy):
print("die")
self.dirty_x = 400
self.dirty_y = 300
dirty = pygame.draw.rect(self.screen, self.BLUE, [self.dirty_x, self.dirty_y, 20, 30])
def handle_event(self, event):
# 按下按键
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.back_status = True
elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
self.Left_status = True
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
self.right_status = True
elif event.key == pygame.K_UP or event.key == pygame.K_w:
self.up_status = True
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
self.down_status = True
# 松开按键
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
self.Left_status = False
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
self.right_status = False
elif event.key == pygame.K_UP or event.key == pygame.K_w:
self.up_status = False
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
self.down_status = False
-136
View File
@@ -1,136 +0,0 @@
import os
import random
import pygame
from src.config.color import Color
from src.config.game_config import data_path, GameConfig
from src.scenes.base_scene import BaseScene
class MainMenuScene(BaseScene):
name = "MainMenuScene"
"""
主菜单场景,显示提示信息,展示雨天特效
"""
def __init__(self, screen, clock):
"""
初始化函数,初始化按钮按下状态、雨滴列表
"""
super().__init__(screen, clock)
self.button_status = False
self.debug_status = False
# 设置字体
font_file = os.path.join(data_path(),"Dymon-ShouXieTi.otf")
self.font = pygame.font.Font(font_file,16)
# 文本
self.game_info_text = self.font.render('按任意键开始游戏,鼠标点击退出', True, Color.white)
# 雨滴列表
self.rain_max = 50
self.rain_list = []
# 雨滴生命周期
self.rain_live_max = 6
# 初始化雨滴
self.init_rain(self.rain_max)
def update(self):
# 按键按下则切换场景
if self.button_status:
self.switch_to_scene("GameScene")
# 显示提示文本
screen_rect = self.screen.get_rect() # 获取屏幕矩形
text_rect = self.game_info_text.get_rect() # 获取文本矩形
self.__show(text_rect, (0, 15))
text_rect.center = screen_rect.center # 将文本居中到屏幕
self.screen.blit(self.game_info_text, text_rect.topleft)
# 调试信息,debug模式下显示
self.__show(screen_rect)
self.__show(text_rect,(0,30))
self.__show(text_rect.center,(0,45))
# 雨滴坠落
self.rain_fall()
def handle_event(self, event):
"""
接收主循环传来的事件并处理
"""
if event.type == pygame.KEYDOWN:
self.button_status = True
elif event.type == pygame.MOUSEBUTTONDOWN:
return "exit" # 主循环接收返回,为exit时设置running为False,退出游戏
def __show(self, info, dest=(0, 0)):
"""
显示调试文本
"""
if not self.debug_status:
return
if not isinstance(info, str):
info = str(info)
info_text = self.font.render(info, True, Color.white)
self.screen.blit(info_text, dest)
def __rain_list_append(self, status=0):
"""
向列表中添加一个雨滴,默认状态为0
"""
x = random.randint(0, int(GameConfig.window_x))
y = random.randint(0, int(GameConfig.window_y))
rain = {
"dest": (x, y),
"timer": 0,
"status": status
}
self.rain_list.append(rain)
def init_rain(self, max_num):
"""
初始化雨滴列表,保存雨滴落点的坐标
"""
for i in range(max_num):
status = random.randint(1, self.rain_live_max)
self.__rain_list_append(status)
def rain_fall(self):
"""
展示雨滴画面
"""
dt = self.clock.tick()
for rain in self.rain_list:
# 当状态在区间1时绘制雨滴线条
if rain["status"] <= self.rain_live_max - 3:
# 线条长度
line_len = 10*(self.rain_live_max - rain["status"])
# 起点位置
x = rain["dest"][0]
y = rain["dest"][1] - line_len
line_start_point = (x, y)
pygame.draw.line(self.screen, Color.white, line_start_point, rain["dest"], 1)
# 状态在区间2时绘制涟漪
elif self.rain_live_max - 3 < rain["status"]:
point_width = rain["status"] - (self.rain_live_max - 3)
pygame.draw.circle(self.screen, Color.white, rain["dest"], point_width, 1)
# # 椭圆
# rain_point_rect = [rain["dest"][0], rain["dest"][1], rain["status"], rain["status"]]
# pygame.draw.ellipse(self.screen, Color.white, rain_point_rect, 1)
# 雨滴状态每d_status毫秒变化一次
d_status = 200
if rain["timer"] > d_status:
rain["status"] += 1
rain["timer"] -= d_status # 不设为0,避免累计时间误差
# 生命周期走完后销毁并添加新雨滴
if rain["status"] > self.rain_live_max:
self.rain_list.remove(rain)
self.__rain_list_append()
# timer累计每帧时间
rain["timer"] += dt
-55
View File
@@ -1,55 +0,0 @@
import pygame
from src.config.game_config import GameConfig
from src.config.scenes import scene_dict
from src.scenes.main_menu_scene import MainMenuScene
class Game:
def __init__(self):
# 初始化
pygame.init()
# 设置窗口大小
size = (GameConfig.window_x, GameConfig.window_y)
self.screen = pygame.display.set_mode(size, pygame.NOFRAME)
# 设置计时器
self.clock = pygame.time.Clock()
# 场景字典
self.game_scenes = scene_dict
# 当前场景
self.scene = self.game_scenes["MainMenuScene"](self.screen, self.clock)
# 运行循环
self.running = True
self.run()
def run(self):
while self.running:
# 事件处理
for event in pygame.event.get(): # 这里已经获取了事件,事件队列被清空,在场景的update中再次获取将没有事件
if event.type == pygame.QUIT:
self.running = False
result = self.scene.handle_event(event) # 当前场景接收事件
if result == "exit":
self.running = False
# 检查是否切换场景
if self.scene.next_scene is not None:
if self.scene.next_scene in self.game_scenes.keys():
scene_class = self.game_scenes[self.scene.next_scene]
self.scene = scene_class(self.screen, self.clock)
# 清屏
self.screen.fill(GameConfig.bg_color)
# 场景每帧更新逻辑运行
self.scene.update()
# 更新显示
pygame.display.update()
if __name__ == "__main__":
Game()