feat: add 10 levels and add enter next level when pass game

This commit is contained in:
Kylin_on_Mac 2023-09-22 17:11:54 +08:00
parent 3ec08f8510
commit d396ecc9f1
12 changed files with 146 additions and 33 deletions

View File

@ -1,10 +1,10 @@
{
"time_to_play": 1200,
"time_to_play": 600,
"playground_size": [
100,
200
],
"score_to_pass": 20,
"score_to_pass": 10,
"green_food_count": 3,
"black_food_count": 0
}

10
levels/002.json Normal file
View File

@ -0,0 +1,10 @@
{
"time_to_play": 600,
"playground_size": [
200,
200
],
"score_to_pass": 15,
"green_food_count": 5,
"black_food_count": 0
}

10
levels/003.json Normal file
View File

@ -0,0 +1,10 @@
{
"time_to_play": 1000,
"playground_size": [
300,
300
],
"score_to_pass": 15,
"green_food_count": 10,
"black_food_count": 0
}

10
levels/004.json Normal file
View File

@ -0,0 +1,10 @@
{
"time_to_play": 1000,
"playground_size": [
300,
300
],
"score_to_pass": 15,
"green_food_count": 7,
"black_food_count": 3
}

10
levels/005.json Normal file
View File

@ -0,0 +1,10 @@
{
"time_to_play": 1000,
"playground_size": [
300,
300
],
"score_to_pass": 15,
"green_food_count": 7,
"black_food_count": 7
}

10
levels/006.json Normal file
View File

@ -0,0 +1,10 @@
{
"time_to_play": 1000,
"playground_size": [
400,
400
],
"score_to_pass": 15,
"green_food_count": 7,
"black_food_count": 10
}

10
levels/007.json Normal file
View File

@ -0,0 +1,10 @@
{
"time_to_play": 1000,
"playground_size": [
400,
400
],
"score_to_pass": 20,
"green_food_count": 7,
"black_food_count": 13
}

10
levels/008.json Normal file
View File

@ -0,0 +1,10 @@
{
"time_to_play": 1200,
"playground_size": [
500,
500
],
"score_to_pass": 20,
"green_food_count": 10,
"black_food_count": 15
}

10
levels/009.json Normal file
View File

@ -0,0 +1,10 @@
{
"time_to_play": 1200,
"playground_size": [
600,
500
],
"score_to_pass": 25,
"green_food_count": 15,
"black_food_count": 30
}

10
levels/010.json Normal file
View File

@ -0,0 +1,10 @@
{
"time_to_play": 1200,
"playground_size": [
700,
600
],
"score_to_pass": 25,
"green_food_count": 20,
"black_food_count": 40
}

View File

@ -36,46 +36,64 @@ class EasyGame(PaiaGame):
self.game_result_state = GameResultState.FAIL
self.scene = Scene(width=WIDTH, height=HEIGHT, color=BG_COLOR, bias_x=0, bias_y=0)
self._level = level
if level != -1:
with open(os.path.join(LEVEL_PATH, f"{level:03d}.json")) as f:
game_params = json.load(f)
self.playground_w = int(game_params["playground_size"][0])
self.playground_h = int(game_params["playground_size"][1])
self.playground = pygame.Rect(
0, 0,
self.playground_w,
self.playground_h
)
self.green_food_count = int(game_params["green_food_count"])
self.black_food_count = int(game_params["black_food_count"])
self.score_to_win = int(game_params["score_to_pass"])
self.frame_limit = int(game_params["time_to_play"])
self.set_game_params_by_level(level)
pass
else:
self.playground_w = int(playground_size[0])
self.playground_h = int(playground_size[1])
self._playground_w = int(playground_size[0])
self._playground_h = int(playground_size[1])
self.playground = pygame.Rect(
0, 0,
self.playground_w,
self.playground_h
self._playground_w,
self._playground_h
)
self.green_food_count = green_food_count
self.black_food_count = black_food_count
self.score_to_win = score_to_pass
self.frame_limit = time_to_play
self._green_food_count = green_food_count
self._black_food_count = black_food_count
self._score_to_pass = score_to_pass
self._frame_limit = time_to_play
self.playground.center = (WIDTH / 2, HEIGHT / 2)
self.foods = pygame.sprite.Group()
self.init_game()
def set_game_params_by_level(self, level):
level_file_path =os.path.join(LEVEL_PATH, f"{level:03d}.json")
if os.path.exists(level_file_path):
# If the file exists, load parameters from the file
with open(level_file_path) as f:
game_params = json.load(f)
else:
# If the file doesn't exist, use default parameters
with open(os.path.join(LEVEL_PATH, "001.json")) as f:
game_params = json.load(f)
self._level=1
self._playground_w = int(game_params["playground_size"][0])
self._playground_h = int(game_params["playground_size"][1])
self.playground = pygame.Rect(
0, 0,
self._playground_w,
self._playground_h
)
self._green_food_count = int(game_params["green_food_count"])
self._black_food_count = int(game_params["black_food_count"])
self._score_to_pass = int(game_params["score_to_pass"])
self._frame_limit = int(game_params["time_to_play"])
self.playground.center = (WIDTH / 2, HEIGHT / 2)
def init_game(self):
self.ball = Ball()
self.foods.empty()
self.score = 0
self._create_foods(self.green_food_count, FoodTypeEnum.GREEN)
self._create_foods(self.black_food_count, FoodTypeEnum.BLACK)
self._create_foods(self._green_food_count, FoodTypeEnum.GREEN)
self._create_foods(self._black_food_count, FoodTypeEnum.BLACK)
self.frame_count = 0
self._frame_count_down = self.frame_limit
self._frame_count_down = self._frame_limit
def update(self, commands):
# handle command
@ -104,7 +122,7 @@ class EasyGame(PaiaGame):
# self._timer = round(time.time() - self._begin_time, 3)
self.frame_count += 1
self._frame_count_down = self.frame_limit - self.frame_count
self._frame_count_down = self._frame_limit - self.frame_count
# self.draw()
if not self.is_running:
@ -137,20 +155,25 @@ class EasyGame(PaiaGame):
if self.is_running:
status = GameStatus.GAME_ALIVE
elif self.score > self.score_to_win:
elif self.score > self._score_to_pass:
status = GameStatus.GAME_PASS
else:
status = GameStatus.GAME_OVER
return status
def reset(self):
if self.score > self._score_to_pass and self._level != -1:
# win and use level will enter next level
self._level += 1
self.set_game_params_by_level(self._level)
self.init_game()
pass
@property
def is_running(self):
return self.frame_count < self.frame_limit
return self.frame_count < self._frame_limit
def get_scene_init_data(self):
"""
@ -207,7 +230,7 @@ class EasyGame(PaiaGame):
"player": get_ai_name(0),
"rank": 1,
"score": self.score,
"passed": self.score > self.score_to_win
"passed": self.score > self._score_to_pass
}
]

View File

@ -8,9 +8,9 @@ FOOD_COLOR_MAP = {FoodTypeEnum.GREEN: "#009688",
BALL_COLOR = "#FFEB3B"
BALL_VEL = 10.5
BALL_H = 50
BALL_H = 30
BALL_W = 50
BALL_W = 30
class Ball(pygame.sprite.Sprite):