From aa3a665de62c4343819716db2442d4ee4ae723ae Mon Sep 17 00:00:00 2001 From: Kylin_on_Mac Date: Thu, 12 Oct 2023 16:32:03 +0800 Subject: [PATCH] feat: add growth on ball --- src/env.py | 7 ++++--- src/game.py | 18 +++++++++++------- src/game_object.py | 27 ++++++++++++++++++++------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/env.py b/src/env.py index f1cae20..be423c8 100644 --- a/src/env.py +++ b/src/env.py @@ -13,9 +13,10 @@ BALL_COLOR = "#FFEB3B" BALL_VEL = 10 BALL_H = 30 BALL_W = 30 -BALL_GROWTH_STEP = 10 -BALL_H_MAX = 80 -BALL_W_MAX = 80 +BALL_GROWTH_SCORE_STEP = 15 +BALL_GROWTH_SIZE_STEP=10 +BALL_GROWTH_VEL_STEP=3 +BALL_SIZE_MAX = 100 BALL_VEL_MAX = 25 # food diff --git a/src/game.py b/src/game.py index 67839d0..e1a7bf4 100644 --- a/src/game.py +++ b/src/game.py @@ -82,7 +82,6 @@ class EasyGame(PaiaGame): # init game self.ball = Ball() self.foods.empty() - self.score = 0 # todo validate food count self._create_foods(GoodFoodLv1, self._good_food_count[0]) @@ -127,7 +126,9 @@ class EasyGame(PaiaGame): hits = pygame.sprite.spritecollide(self.ball, self.foods, True) if hits: for food in hits: - self.score += food.score + # self.ball.score += food.score + self.ball.eat_food(food) + # TODO if growth play special sound self._create_foods(food.__class__, 1) if isinstance(food, (GoodFoodLv1,GoodFoodLv2,GoodFoodLv3,)): self.sound_controller.play_eating_good() @@ -142,17 +143,20 @@ class EasyGame(PaiaGame): to_players_data = {} foods_data = [] for food in self.foods: - # TODO add good food and bad food foods_data.append({"x": food.rect.x, "y": food.rect.y, "type": food.type, "score": food.score}) + # TODO add velocity and ball_size + # TODO change ball to player + data_to_1p = { "frame": self.frame_count, "ball_x": self.ball.rect.centerx, "ball_y": self.ball.rect.centery, "foods": foods_data, - "score": self.score, + "score": self.ball.score, "score_to_pass":self._score_to_pass, "status": self.get_game_status() + } to_players_data[get_ai_name(0)] = data_to_1p @@ -193,7 +197,7 @@ class EasyGame(PaiaGame): @property def is_passed(self): - return self.score > self._score_to_pass + return self.ball.score > self._score_to_pass @property def is_running(self): @@ -235,7 +239,7 @@ class EasyGame(PaiaGame): ] toggle_objs = [ - create_text_view_data(f"Score:{self.score:04d}", 600, 50, "#A5D6A7", "24px Arial BOLD"), + create_text_view_data(f"Score:{self.ball.score:04d}", 600, 50, "#A5D6A7", "24px Arial BOLD"), create_text_view_data(f" Next:{self._score_to_pass:04d}", 600, 100, "#FF4081", "24px Arial BOLD"), create_text_view_data(f" Time:{self._frame_count_down:04d}", 600, 150, "#FF5722", "24px Arial BOLD"), @@ -258,7 +262,7 @@ class EasyGame(PaiaGame): { "player": get_ai_name(0), "rank": 1, - "score": self.score, + "score": self.ball.score, "passed": self.is_passed } ] diff --git a/src/game_object.py b/src/game_object.py index 46848ce..985ded3 100644 --- a/src/game_object.py +++ b/src/game_object.py @@ -1,6 +1,9 @@ +import math + import pygame.sprite -from games.easy_game.src.env import BALL_COLOR, BALL_VEL, BALL_H, BALL_W +from games.easy_game.src.env import BALL_COLOR, BALL_VEL, BALL_H, BALL_W, BALL_GROWTH_SCORE_STEP, BALL_GROWTH_SIZE_STEP, \ + BALL_SIZE_MAX, BALL_GROWTH_VEL_STEP, BALL_VEL_MAX from games.easy_game.src.foods import Food from mlgame.view.view_model import create_rect_view_data @@ -13,8 +16,8 @@ class Ball(pygame.sprite.Sprite): self.color = BALL_COLOR self.rect = self.image.get_rect() self.rect.center = (400, 300) - self.score = 0 - self.vel = BALL_VEL + self._score = 0 + self._vel = BALL_VEL # TODO refactor score # TODO add velocity and size in Ball @@ -22,14 +25,14 @@ class Ball(pygame.sprite.Sprite): def update(self, motion): # for motion in motions: if motion == "UP": - self.rect.centery -= self.vel + self.rect.centery -= self._vel elif motion == "DOWN": - self.rect.centery += self.vel + self.rect.centery += self._vel elif motion == "LEFT": - self.rect.centerx -= self.vel + self.rect.centerx -= self._vel # self.angle += 5 elif motion == "RIGHT": - self.rect.centerx += self.vel + self.rect.centerx += self._vel # self.angle -= 5 @@ -50,4 +53,14 @@ class Ball(pygame.sprite.Sprite): self.color ) + def eat_food(self, food:Food): + self._score+=food.score + lv = math.ceil((self._score+1) / BALL_GROWTH_SCORE_STEP) + self.rect.width = min(BALL_W + lv*BALL_GROWTH_SIZE_STEP,BALL_SIZE_MAX) + self.rect.height = min(BALL_H + lv*BALL_GROWTH_SIZE_STEP,BALL_SIZE_MAX) + self._vel = min(BALL_VEL +lv*BALL_GROWTH_VEL_STEP,BALL_VEL_MAX) + pass + @property + def score(self): + return self._score