单向场景切换版本 #1
No files matched your search
@@ -1,8 +1,9 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
class BaseScene:
|
class BaseScene:
|
||||||
def __init__(self, screen: pygame.Surface):
|
def __init__(self, screen: pygame.Surface, clock: pygame.time.Clock):
|
||||||
self.screen = screen
|
self.screen = screen
|
||||||
|
self.clock = clock
|
||||||
self.next_scene = None
|
self.next_scene = None
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
import random
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
from src.config.color import Color
|
from src.config.color import Color
|
||||||
@@ -8,11 +9,17 @@ from src.scenes.game_scene import GameScene
|
|||||||
|
|
||||||
|
|
||||||
class MainMenuScene(BaseScene):
|
class MainMenuScene(BaseScene):
|
||||||
def __init__(self, screen):
|
def __init__(self, screen, clock):
|
||||||
super().__init__(screen)
|
super().__init__(screen, clock)
|
||||||
self.button_status = False
|
self.button_status = False
|
||||||
self.debug_status = False
|
self.debug_status = False
|
||||||
|
|
||||||
|
# 雨滴列表
|
||||||
|
self.rain_max = 50
|
||||||
|
self.rain_list = []
|
||||||
|
# 雨滴生命周期
|
||||||
|
self.rain_live_max = 6
|
||||||
|
|
||||||
# 设置字体
|
# 设置字体
|
||||||
font_file = os.path.join(data_path(),"Dymon-ShouXieTi.otf")
|
font_file = os.path.join(data_path(),"Dymon-ShouXieTi.otf")
|
||||||
self.font = pygame.font.Font(font_file,16)
|
self.font = pygame.font.Font(font_file,16)
|
||||||
@@ -20,6 +27,9 @@ class MainMenuScene(BaseScene):
|
|||||||
# 文本
|
# 文本
|
||||||
self.game_info_text = self.font.render('按任意键开始游戏,鼠标点击退出', True, Color.white)
|
self.game_info_text = self.font.render('按任意键开始游戏,鼠标点击退出', True, Color.white)
|
||||||
|
|
||||||
|
# 初始化雨滴
|
||||||
|
self.init_rain(self.rain_max)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
# 按键按下则切换场景
|
# 按键按下则切换场景
|
||||||
if self.button_status:
|
if self.button_status:
|
||||||
@@ -37,6 +47,9 @@ class MainMenuScene(BaseScene):
|
|||||||
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))
|
||||||
|
|
||||||
|
# 雨滴坠落
|
||||||
|
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
|
||||||
@@ -50,3 +63,61 @@ class MainMenuScene(BaseScene):
|
|||||||
info = str(info)
|
info = str(info)
|
||||||
info_text = self.font.render(info, True, Color.white)
|
info_text = self.font.render(info, True, Color.white)
|
||||||
self.screen.blit(info_text, dest)
|
self.screen.blit(info_text, dest)
|
||||||
|
|
||||||
|
def __rain_list_append(self, status=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
|
||||||
@@ -13,8 +13,11 @@ class Game:
|
|||||||
size = (GameConfig.window_x, GameConfig.window_y)
|
size = (GameConfig.window_x, GameConfig.window_y)
|
||||||
self.screen = pygame.display.set_mode(size, pygame.NOFRAME)
|
self.screen = pygame.display.set_mode(size, pygame.NOFRAME)
|
||||||
|
|
||||||
|
# 设置计时器
|
||||||
|
self.clock = pygame.time.Clock()
|
||||||
|
|
||||||
# 当前场景
|
# 当前场景
|
||||||
self.scene = MainMenuScene(self.screen)
|
self.scene = MainMenuScene(self.screen, self.clock)
|
||||||
|
|
||||||
# 运行循环
|
# 运行循环
|
||||||
self.running = True
|
self.running = True
|
||||||
|
|||||||
Reference in new issue
Block a user