feat: add growth on ball

This commit is contained in:
Kylin_on_Mac 2023-10-12 16:32:03 +08:00
parent 85ac02c435
commit aa3a665de6
3 changed files with 35 additions and 17 deletions

View File

@ -13,9 +13,10 @@ BALL_COLOR = "#FFEB3B"
BALL_VEL = 10 BALL_VEL = 10
BALL_H = 30 BALL_H = 30
BALL_W = 30 BALL_W = 30
BALL_GROWTH_STEP = 10 BALL_GROWTH_SCORE_STEP = 15
BALL_H_MAX = 80 BALL_GROWTH_SIZE_STEP=10
BALL_W_MAX = 80 BALL_GROWTH_VEL_STEP=3
BALL_SIZE_MAX = 100
BALL_VEL_MAX = 25 BALL_VEL_MAX = 25
# food # food

View File

@ -82,7 +82,6 @@ class EasyGame(PaiaGame):
# init game # init game
self.ball = Ball() self.ball = Ball()
self.foods.empty() self.foods.empty()
self.score = 0
# todo validate food count # todo validate food count
self._create_foods(GoodFoodLv1, self._good_food_count[0]) 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) hits = pygame.sprite.spritecollide(self.ball, self.foods, True)
if hits: if hits:
for food in 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) self._create_foods(food.__class__, 1)
if isinstance(food, (GoodFoodLv1,GoodFoodLv2,GoodFoodLv3,)): if isinstance(food, (GoodFoodLv1,GoodFoodLv2,GoodFoodLv3,)):
self.sound_controller.play_eating_good() self.sound_controller.play_eating_good()
@ -142,17 +143,20 @@ class EasyGame(PaiaGame):
to_players_data = {} to_players_data = {}
foods_data = [] foods_data = []
for food in self.foods: 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}) 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 = { data_to_1p = {
"frame": self.frame_count, "frame": self.frame_count,
"ball_x": self.ball.rect.centerx, "ball_x": self.ball.rect.centerx,
"ball_y": self.ball.rect.centery, "ball_y": self.ball.rect.centery,
"foods": foods_data, "foods": foods_data,
"score": self.score, "score": self.ball.score,
"score_to_pass":self._score_to_pass, "score_to_pass":self._score_to_pass,
"status": self.get_game_status() "status": self.get_game_status()
} }
to_players_data[get_ai_name(0)] = data_to_1p to_players_data[get_ai_name(0)] = data_to_1p
@ -193,7 +197,7 @@ class EasyGame(PaiaGame):
@property @property
def is_passed(self): def is_passed(self):
return self.score > self._score_to_pass return self.ball.score > self._score_to_pass
@property @property
def is_running(self): def is_running(self):
@ -235,7 +239,7 @@ class EasyGame(PaiaGame):
] ]
toggle_objs = [ 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" 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"), 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), "player": get_ai_name(0),
"rank": 1, "rank": 1,
"score": self.score, "score": self.ball.score,
"passed": self.is_passed "passed": self.is_passed
} }
] ]

View File

@ -1,6 +1,9 @@
import math
import pygame.sprite 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 games.easy_game.src.foods import Food
from mlgame.view.view_model import create_rect_view_data from mlgame.view.view_model import create_rect_view_data
@ -13,8 +16,8 @@ class Ball(pygame.sprite.Sprite):
self.color = BALL_COLOR self.color = BALL_COLOR
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.rect.center = (400, 300) self.rect.center = (400, 300)
self.score = 0 self._score = 0
self.vel = BALL_VEL self._vel = BALL_VEL
# TODO refactor score # TODO refactor score
# TODO add velocity and size in Ball # TODO add velocity and size in Ball
@ -22,14 +25,14 @@ class Ball(pygame.sprite.Sprite):
def update(self, motion): def update(self, motion):
# for motion in motions: # for motion in motions:
if motion == "UP": if motion == "UP":
self.rect.centery -= self.vel self.rect.centery -= self._vel
elif motion == "DOWN": elif motion == "DOWN":
self.rect.centery += self.vel self.rect.centery += self._vel
elif motion == "LEFT": elif motion == "LEFT":
self.rect.centerx -= self.vel self.rect.centerx -= self._vel
# self.angle += 5 # self.angle += 5
elif motion == "RIGHT": elif motion == "RIGHT":
self.rect.centerx += self.vel self.rect.centerx += self._vel
# self.angle -= 5 # self.angle -= 5
@ -50,4 +53,14 @@ class Ball(pygame.sprite.Sprite):
self.color 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