Compare commits
9
Commits
81b48d6b58
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
009c591054 | ||
|
|
6050bca933 | ||
|
|
eefb8da9c6 | ||
|
|
805704f0e5 | ||
|
|
90f734158b | ||
|
|
f76bbc6442 | ||
|
|
538af0d98c | ||
|
|
876645f4b0 | ||
|
|
a604dc3b7c |
No files matched your search
Binary file not shown.
@@ -0,0 +1,96 @@
|
|||||||
|
# -*-coding:utf-8 -*- #
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# ProjectName: Game_project
|
||||||
|
# FileName: page.py
|
||||||
|
# Author: Zhi_yuan
|
||||||
|
# Datetime: 2026/7/25 下午10:44
|
||||||
|
# Description:
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
import os
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
from src.config.game_config import data_path
|
||||||
|
|
||||||
|
# 初始化pygame
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
# 初始化音乐
|
||||||
|
pygame.mixer.init()
|
||||||
|
|
||||||
|
# 设置窗口大小
|
||||||
|
size = (800, 600)
|
||||||
|
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.play(-1)
|
||||||
|
|
||||||
|
# 状态值
|
||||||
|
dirty_x = 400
|
||||||
|
dirty_y = 300
|
||||||
|
|
||||||
|
Left_status = False
|
||||||
|
right_status = False
|
||||||
|
up_status = False
|
||||||
|
down_status = False
|
||||||
|
|
||||||
|
# 颜色
|
||||||
|
WHITE = (255, 255, 255)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
BLUE = (0, 0, 255)
|
||||||
|
# 游戏循环
|
||||||
|
running = True
|
||||||
|
while running:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
running = False
|
||||||
|
# 按下按键
|
||||||
|
elif event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
|
||||||
|
Left_status = True
|
||||||
|
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
|
||||||
|
right_status = True
|
||||||
|
elif event.key == pygame.K_UP or event.key == pygame.K_w:
|
||||||
|
up_status = True
|
||||||
|
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
|
||||||
|
down_status = True
|
||||||
|
# 松开按键
|
||||||
|
elif event.type == pygame.KEYUP:
|
||||||
|
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
|
||||||
|
Left_status = False
|
||||||
|
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
|
||||||
|
right_status = False
|
||||||
|
elif event.key == pygame.K_UP or event.key == pygame.K_w:
|
||||||
|
up_status = False
|
||||||
|
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
|
||||||
|
down_status = False
|
||||||
|
elif event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
print("Mouse button pressed at", event.pos)
|
||||||
|
if Left_status and dirty_x >= 0:
|
||||||
|
dirty_x -= 1
|
||||||
|
if right_status and dirty_x <= 780:
|
||||||
|
dirty_x += 1
|
||||||
|
if up_status and dirty_y >= 0:
|
||||||
|
dirty_y -= 1
|
||||||
|
if down_status and dirty_y <= 570:
|
||||||
|
dirty_y += 1
|
||||||
|
# 填充窗口颜色
|
||||||
|
screen.fill((255, 255, 255))
|
||||||
|
|
||||||
|
# 绘制矩形(移动体和固定体)
|
||||||
|
dirty = pygame.draw.rect(screen, BLUE, [dirty_x, dirty_y, 20, 30])
|
||||||
|
enemy = pygame.draw.rect(screen, RED, [40, 60, 10, 10])
|
||||||
|
|
||||||
|
# 碰撞监测
|
||||||
|
if dirty.colliderect(enemy):
|
||||||
|
print("die")
|
||||||
|
dirty_x = 400
|
||||||
|
dirty_y = 300
|
||||||
|
dirty = pygame.draw.rect(screen, BLUE, [dirty_x, dirty_y, 20, 30])
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
pygame.quit()
|
||||||
@@ -13,4 +13,5 @@ def data_path():
|
|||||||
data_path = os.path.join(project_path, "data")
|
data_path = os.path.join(project_path, "data")
|
||||||
return data_path
|
return data_path
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
print(data_path())
|
print(data_path())
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
from src.scenes.main_menu_scene import MainMenuScene
|
||||||
|
from src.scenes.game_scene import GameScene
|
||||||
|
|
||||||
|
|
||||||
|
scene_dict = {
|
||||||
|
"MainMenuScene": MainMenuScene,
|
||||||
|
"GameScene": GameScene,
|
||||||
|
}
|
||||||
@@ -2,15 +2,27 @@ import pygame
|
|||||||
|
|
||||||
class BaseScene:
|
class BaseScene:
|
||||||
def __init__(self, screen: pygame.Surface, clock: pygame.time.Clock):
|
def __init__(self, screen: pygame.Surface, clock: pygame.time.Clock):
|
||||||
|
"""
|
||||||
|
初始化场景,接收并保存画布、计时器
|
||||||
|
"""
|
||||||
self.screen = screen
|
self.screen = screen
|
||||||
self.clock = clock
|
self.clock = clock
|
||||||
self.next_scene = None
|
self.next_scene = None
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
|
"""
|
||||||
|
场景的每帧更新逻辑
|
||||||
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def handle_event(self, events):
|
def handle_event(self, events):
|
||||||
|
"""
|
||||||
|
接收主循环传来的事件并处理
|
||||||
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def switch_to_scene(self, scene_class):
|
def switch_to_scene(self, scene_name):
|
||||||
self.next_scene = scene_class(self.screen, self.clock)
|
"""
|
||||||
|
切换场景
|
||||||
|
"""
|
||||||
|
self.next_scene = scene_name
|
||||||
@@ -1,12 +1,24 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
from src.config.game_config import GameConfig
|
from src.config.game_config import data_path, GameConfig
|
||||||
from src.scenes.base_scene import BaseScene
|
from src.scenes.base_scene import BaseScene
|
||||||
|
|
||||||
|
|
||||||
class GameScene(BaseScene):
|
class GameScene(BaseScene):
|
||||||
|
name = "GameScene"
|
||||||
def __init__(self, screen, clock):
|
def __init__(self, screen, clock):
|
||||||
super().__init__(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_x = 400
|
||||||
self.dirty_y = 300
|
self.dirty_y = 300
|
||||||
|
|
||||||
@@ -21,6 +33,8 @@ class GameScene(BaseScene):
|
|||||||
self.BLUE = (0, 0, 255)
|
self.BLUE = (0, 0, 255)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
|
if self.back_status:
|
||||||
|
self.switch_to_scene("MainMenuScene")
|
||||||
if self.Left_status and self.dirty_x >= 0:
|
if self.Left_status and self.dirty_x >= 0:
|
||||||
self.dirty_x -= 1
|
self.dirty_x -= 1
|
||||||
if self.right_status and self.dirty_x <= GameConfig.window_x - 20:
|
if self.right_status and self.dirty_x <= GameConfig.window_x - 20:
|
||||||
@@ -29,11 +43,26 @@ class GameScene(BaseScene):
|
|||||||
self.dirty_y -= 1
|
self.dirty_y -= 1
|
||||||
if self.down_status and self.dirty_y <= GameConfig.window_y - 30:
|
if self.down_status and self.dirty_y <= GameConfig.window_y - 30:
|
||||||
self.dirty_y += 1
|
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])
|
dirty = pygame.draw.rect(self.screen, self.BLUE, [self.dirty_x, self.dirty_y, 20, 30])
|
||||||
|
|
||||||
def handle_event(self, event):
|
def handle_event(self, event):
|
||||||
|
# 按下按键
|
||||||
if event.type == pygame.KEYDOWN:
|
if event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
|
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
|
self.Left_status = True
|
||||||
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
|
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
|
||||||
self.right_status = True
|
self.right_status = True
|
||||||
@@ -41,6 +70,7 @@ class GameScene(BaseScene):
|
|||||||
self.up_status = True
|
self.up_status = True
|
||||||
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
|
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
|
||||||
self.down_status = True
|
self.down_status = True
|
||||||
|
# 松开按键
|
||||||
elif event.type == pygame.KEYUP:
|
elif event.type == pygame.KEYUP:
|
||||||
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
|
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
|
||||||
self.Left_status = False
|
self.Left_status = False
|
||||||
|
|||||||
@@ -5,44 +5,48 @@ import pygame
|
|||||||
from src.config.color import Color
|
from src.config.color import Color
|
||||||
from src.config.game_config import data_path, GameConfig
|
from src.config.game_config import data_path, GameConfig
|
||||||
from src.scenes.base_scene import BaseScene
|
from src.scenes.base_scene import BaseScene
|
||||||
from src.scenes.game_scene import GameScene
|
|
||||||
|
|
||||||
|
|
||||||
class MainMenuScene(BaseScene):
|
class MainMenuScene(BaseScene):
|
||||||
|
name = "MainMenuScene"
|
||||||
|
"""
|
||||||
|
主菜单场景,显示提示信息,展示雨天特效
|
||||||
|
"""
|
||||||
def __init__(self, screen, clock):
|
def __init__(self, screen, clock):
|
||||||
|
"""
|
||||||
|
初始化函数,初始化按钮按下状态、雨滴列表
|
||||||
|
"""
|
||||||
super().__init__(screen, clock)
|
super().__init__(screen, clock)
|
||||||
self.button_status = False
|
self.button_status = False
|
||||||
self.debug_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_max = 50
|
||||||
self.rain_list = []
|
self.rain_list = []
|
||||||
# 雨滴生命周期
|
# 雨滴生命周期
|
||||||
self.rain_live_max = 6
|
self.rain_live_max = 6
|
||||||
|
|
||||||
# 设置字体
|
|
||||||
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.init_rain(self.rain_max)
|
self.init_rain(self.rain_max)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
# 按键按下则切换场景
|
# 按键按下则切换场景
|
||||||
if self.button_status:
|
if self.button_status:
|
||||||
self.switch_to_scene(GameScene)
|
self.switch_to_scene("GameScene")
|
||||||
|
|
||||||
# 显示提示文本
|
# 显示提示文本
|
||||||
|
|
||||||
screen_rect = self.screen.get_rect() # 获取屏幕矩形
|
screen_rect = self.screen.get_rect() # 获取屏幕矩形
|
||||||
text_rect = self.game_info_text.get_rect() # 获取文本矩形
|
text_rect = self.game_info_text.get_rect() # 获取文本矩形
|
||||||
self.__show(text_rect, (0, 15))
|
self.__show(text_rect, (0, 15))
|
||||||
text_rect.center = screen_rect.center # 将文本居中到屏幕
|
text_rect.center = screen_rect.center # 将文本居中到屏幕
|
||||||
self.screen.blit(self.game_info_text, text_rect.topleft)
|
self.screen.blit(self.game_info_text, text_rect.topleft)
|
||||||
|
|
||||||
|
# 调试信息,debug模式下显示
|
||||||
self.__show(screen_rect)
|
self.__show(screen_rect)
|
||||||
self.__show(text_rect,(0,30))
|
self.__show(text_rect,(0,30))
|
||||||
self.__show(text_rect.center,(0,45))
|
self.__show(text_rect.center,(0,45))
|
||||||
@@ -51,12 +55,18 @@ class MainMenuScene(BaseScene):
|
|||||||
self.rain_fall()
|
self.rain_fall()
|
||||||
|
|
||||||
def handle_event(self, event):
|
def handle_event(self, event):
|
||||||
|
"""
|
||||||
|
接收主循环传来的事件并处理
|
||||||
|
"""
|
||||||
if event.type == pygame.KEYDOWN:
|
if event.type == pygame.KEYDOWN:
|
||||||
self.button_status = True
|
self.button_status = True
|
||||||
elif event.type == pygame.MOUSEBUTTONDOWN:
|
elif event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
return "exit" # 主循环接收返回,为exit时设置running为False,退出游戏
|
return "exit" # 主循环接收返回,为exit时设置running为False,退出游戏
|
||||||
|
|
||||||
def __show(self, info, dest=(0, 0)):
|
def __show(self, info, dest=(0, 0)):
|
||||||
|
"""
|
||||||
|
显示调试文本
|
||||||
|
"""
|
||||||
if not self.debug_status:
|
if not self.debug_status:
|
||||||
return
|
return
|
||||||
if not isinstance(info, str):
|
if not isinstance(info, str):
|
||||||
@@ -65,6 +75,9 @@ class MainMenuScene(BaseScene):
|
|||||||
self.screen.blit(info_text, dest)
|
self.screen.blit(info_text, dest)
|
||||||
|
|
||||||
def __rain_list_append(self, status=0):
|
def __rain_list_append(self, status=0):
|
||||||
|
"""
|
||||||
|
向列表中添加一个雨滴,默认状态为0
|
||||||
|
"""
|
||||||
x = random.randint(0, int(GameConfig.window_x))
|
x = random.randint(0, int(GameConfig.window_x))
|
||||||
y = random.randint(0, int(GameConfig.window_y))
|
y = random.randint(0, int(GameConfig.window_y))
|
||||||
rain = {
|
rain = {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
from src.config.game_config import GameConfig
|
from src.config.game_config import GameConfig
|
||||||
|
from src.config.scenes import scene_dict
|
||||||
from src.scenes.main_menu_scene import MainMenuScene
|
from src.scenes.main_menu_scene import MainMenuScene
|
||||||
|
|
||||||
|
|
||||||
@@ -16,8 +17,10 @@ class Game:
|
|||||||
# 设置计时器
|
# 设置计时器
|
||||||
self.clock = pygame.time.Clock()
|
self.clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
# 场景字典
|
||||||
|
self.game_scenes = scene_dict
|
||||||
# 当前场景
|
# 当前场景
|
||||||
self.scene = MainMenuScene(self.screen, self.clock)
|
self.scene = self.game_scenes["MainMenuScene"](self.screen, self.clock)
|
||||||
|
|
||||||
# 运行循环
|
# 运行循环
|
||||||
self.running = True
|
self.running = True
|
||||||
@@ -35,7 +38,9 @@ class Game:
|
|||||||
|
|
||||||
# 检查是否切换场景
|
# 检查是否切换场景
|
||||||
if self.scene.next_scene is not None:
|
if self.scene.next_scene is not None:
|
||||||
self.scene = self.scene.next_scene
|
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.screen.fill(GameConfig.bg_color)
|
||||||
|
|||||||
Reference in new issue
Block a user