interface

This commit is contained in:
yanshaoting 2024-02-23 12:23:37 +08:00
parent fd7e60109e
commit 0ee2a52a06
4 changed files with 58 additions and 23 deletions

View File

@ -39,6 +39,7 @@ game = SwimmingSquid(
### 角色升級機制 ### 角色升級機制
角色初始等級皆為 1隨著得分增加升 / 降級。等級將會影響角色長寬與移動速度,各等級對應資料如下: 角色初始等級皆為 1隨著得分增加升 / 降級。等級將會影響角色長寬與移動速度,各等級對應資料如下:
| Lv| 角色寬度 | 角色高度 |移動速度| | Lv| 角色寬度 | 角色高度 |移動速度|
| --- | ------- | ------ | ------| | --- | ------- | ------ | ------|
| 1 | 30 | 45 |25| | 1 | 30 | 45 |25|

View File

@ -1,5 +1,5 @@
{ {
"time_to_play": 10, "time_to_play": 300,
"playground_size_w":200, "playground_size_w":200,
"playground_size_h":200, "playground_size_h":200,
"score_to_pass": 10, "score_to_pass": 10,

View File

@ -44,6 +44,7 @@ class SwimmingSquid(PaiaGame):
self.scene = Scene(width=WIDTH, height=HEIGHT, color=BG_COLOR, bias_x=0, bias_y=0) self.scene = Scene(width=WIDTH, height=HEIGHT, color=BG_COLOR, bias_x=0, bias_y=0)
self._level = level self._level = level
self._level_file = level_file self._level_file = level_file
self._used_file = ""
self.foods = pygame.sprite.Group() self.foods = pygame.sprite.Group()
self.sound_controller = SoundController(sound) self.sound_controller = SoundController(sound)
self._collision_mode = False self._collision_mode = False
@ -55,6 +56,7 @@ class SwimmingSquid(PaiaGame):
try: try:
with open(level_file_path) as f: with open(level_file_path) as f:
game_params = LevelParams(**json.load(f)) game_params = LevelParams(**json.load(f))
self._used_file = level_file_path
except: except:
# If the file doesn't exist, use default parameters # If the file doesn't exist, use default parameters
@ -64,6 +66,7 @@ class SwimmingSquid(PaiaGame):
game_params = LevelParams(**json.load(f)) game_params = LevelParams(**json.load(f))
self._level = 1 self._level = 1
self._level_file = "" self._level_file = ""
self._used_file = "001.json"
finally: finally:
# set game params # set game params
self.playground = pygame.Rect( self.playground = pygame.Rect(
@ -111,8 +114,8 @@ class SwimmingSquid(PaiaGame):
action_2 = "NONE" action_2 = "NONE"
self.squid1.update(action_1) self.squid1.update(self.frame_count, action_1)
self.squid2.update(action_2) self.squid2.update(self.frame_count, action_2)
revise_ball(self.squid1, self.playground) revise_ball(self.squid1, self.playground)
revise_ball(self.squid2, self.playground) revise_ball(self.squid2, self.playground)
# update sprite # update sprite
@ -235,7 +238,6 @@ class SwimmingSquid(PaiaGame):
elif self.is_passed: elif self.is_passed:
status = GameStatus.GAME_PASS status = GameStatus.GAME_PASS
else: else:
print("GAME_OVER")
status = GameStatus.GAME_OVER status = GameStatus.GAME_OVER
return status return status
@ -248,13 +250,11 @@ class SwimmingSquid(PaiaGame):
self.sound_controller.play_fail() self.sound_controller.play_fail()
self._init_game() self._init_game()
pass
def _init_game(self): def _init_game(self):
if path.isfile(self._level_file): if path.isfile(self._level_file):
# set by injected file # set by injected file
self._init_game_by_file(self._level_file) self._init_game_by_file(self._level_file)
pass
else: else:
level_file_path = os.path.join(LEVEL_PATH, f"{self._level:03d}.json") level_file_path = os.path.join(LEVEL_PATH, f"{self._level:03d}.json")
self._init_game_by_file(level_file_path) self._init_game_by_file(level_file_path)
@ -266,7 +266,9 @@ class SwimmingSquid(PaiaGame):
self._frame_limit += 600 self._frame_limit += 600
self._score_to_pass += 50 self._score_to_pass += 50
self._overtime_count += 1 self._overtime_count += 1
print("同分延長賽")
return False return False
# print("達成目標分數")
return True return True
else: else:
return False return False
@ -277,12 +279,15 @@ class SwimmingSquid(PaiaGame):
if self.squid1.score == self.squid2.score and self._overtime_count < 3: # 延長賽 if self.squid1.score == self.squid2.score and self._overtime_count < 3: # 延長賽
self._frame_limit += 300 self._frame_limit += 300
self._overtime_count += 1 self._overtime_count += 1
print("超時延長賽")
return False return False
else: else:
print("時間到")
return True return True
else: else:
return False return False
@property @property
def is_running(self): def is_running(self):
@ -345,22 +350,26 @@ class SwimmingSquid(PaiaGame):
foregrounds = [ foregrounds = [
] ]
star_string_1P = '+' * self.squid1.lv
star_string_2P = '+' * self.squid2.lv
toggle_objs = [ toggle_objs = [
create_text_view_data(f"Timer : {self._frame_count_down:04d}", 705, 50, "#EEEEEE", "20px Consolas BOLD"), create_text_view_data(f"Timer:{self._frame_count_down:04d}", 745, 40, "#EEEEEE", "20px Consolas BOLD"),
create_text_view_data(f"Goal : {self._score_to_pass:04d} pt", 705, 80, "#EEEEEE", "20px Consolas BOLD"), # create_text_view_data(f"", 785, 80, "#EEEEEE", "18px Consolas BOLD"),
create_text_view_data("1P", 705, 130, "#EEEEEE", "22px Consolas BOLD"), create_text_view_data(f"File :{os.path.basename(self._used_file)}", 745, 80, "#EEEEEE", "20px Consolas BOLD"),
create_text_view_data(f"Lv: {star_string_1P}", 705, 160, "#EEEEEE", "20px Consolas BOLD"), # create_text_view_data(f"File :{self._level_file}", 605, 80, "#EEEEEE", "10px Consolas BOLD"),
create_text_view_data(f"To Lv up: {LEVEL_THRESHOLDS[self.squid1.lv - 1]-self.squid1.score :04d} pt", 705, 190, "#EEEEEE", "20px Consolas BOLD"), create_text_view_data(f"Goal :{self._score_to_pass:04d} pt", 745, 120, "#EEEEEE", "20px Consolas BOLD"),
create_text_view_data(f"Vel : {self.squid1.vel:4d}", 705, 220, "#EEEEEE", "20px Consolas BOLD"), # create_text_view_data(f"", 785, 140, "#EEEEEE", "18px Consolas BOLD"),
create_text_view_data(f"1P Score: {self.squid1.score:04d} pt", 705, 250, "#EEEEEE", "20px Consolas BOLD"), create_image_view_data("squid1", 705, 180, 76, 114),
create_text_view_data("2P", 705, 310, "#EEEEEE", "22px Consolas BOLD"), # create_text_view_data("1P", 705, 130, "#EEEEEE", "22px Consolas BOLD"),
create_text_view_data(f"Lv: {star_string_2P}", 705, 340, "#EEEEEE", "20px Consolas BOLD"), create_text_view_data(f"Lv : {self.squid1.lv}", 785, 180, "#EEEEEE", "16px Consolas BOLD"),
create_text_view_data(f"To Lv up: {LEVEL_THRESHOLDS[self.squid2.lv - 1] - self.squid2.score :04d} pt", 705, create_text_view_data(f"Next Lv: {LEVEL_THRESHOLDS[self.squid1.lv - 1]-self.squid1.score :04d} pt", 785, 210, "#EEEEEE", "16px Consolas BOLD"),
370, "#EEEEEE", "20px Consolas BOLD"), create_text_view_data(f"Vel : {self.squid1.vel:2d}", 785, 240, "#EEEEEE", "16px Consolas BOLD"),
create_text_view_data(f"Vel : {self.squid2.vel:4d}", 705, 400, "#EEEEEE", "20px Consolas BOLD"), create_text_view_data(f"Score : {self.squid1.score:04d} pt", 785, 270, "#EEEEEE", "16px Consolas BOLD"),
create_text_view_data(f"2P Score: {self.squid2.score:04d} pt", 705, 430, "#EEEEEE", "20px Consolas BOLD"), # create_text_view_data("2P", 705, 310, "#EEEEEE", "22px Consolas BOLD"),
create_image_view_data("squid2", 705, 390, 76, 114),
create_text_view_data(f"Lv : {self.squid2.lv}", 785, 390, "#EEEEEE", "16px Consolas BOLD"),
create_text_view_data(f"Next Lv: {LEVEL_THRESHOLDS[self.squid2.lv - 1] - self.squid2.score :04d} pt", 785,
420, "#EEEEEE", "16px Consolas BOLD"),
create_text_view_data(f"Vel : {self.squid2.vel:2d}", 785, 450, "#EEEEEE", "16px Consolas BOLD"),
create_text_view_data(f"Score : {self.squid2.score:04d} pt", 785, 480, "#EEEEEE", "16px Consolas BOLD"),
] ]
scene_progress = create_scene_progress_data( scene_progress = create_scene_progress_data(

View File

@ -1,4 +1,6 @@
import math import math
import random
import pydantic import pydantic
import pygame.sprite import pygame.sprite
@ -44,9 +46,27 @@ class Squid(pygame.sprite.Sprite):
self.rank = 1 self.rank = 1
self.angle = 0 self.angle = 0
self._last_collision = 0 self._last_collision = 0
self._collision_dir = None
self._motion = None
def update(self, motion): def update(self, frame, motion):
# for motion in motions: # for motion in motions:
self._motion = motion
if frame - self._last_collision <=3:
# 反彈
if self._collision_dir == "UP":
self.rect.centery += self._vel
elif self._collision_dir == "DOWN":
self.rect.centery -= self._vel
elif self._collision_dir == "LEFT":
self.rect.centerx += self._vel
self.angle = self.ANGLE_TO_RIGHT
elif self._collision_dir == "RIGHT":
self.rect.centerx -= self._vel
self.angle = self.ANGLE_TO_LEFT
else:
self.angle = 0
return 0
if motion == "UP": if motion == "UP":
self.rect.centery -= self._vel self.rect.centery -= self._vel
elif motion == "DOWN": elif motion == "DOWN":
@ -93,10 +113,15 @@ class Squid(pygame.sprite.Sprite):
self._lv = new_lv self._lv = new_lv
def collision_between_squids(self, collision_score, frame, sound_controller: SoundController): def collision_between_squids(self, collision_score, frame, sound_controller: SoundController):
if frame - self._last_collision > 30: if frame - self._last_collision > 3:
self._score += collision_score self._score += collision_score
self._last_collision = frame self._last_collision = frame
sound_controller.play_collision() sound_controller.play_collision()
if self._motion != "NONE":
self._collision_dir = self._motion
else:
self._collision_dir = random.choice(["UP", "DOWN", "RIGHT", "LEFT"])
new_lv = get_current_level(self._score) new_lv = get_current_level(self._score)