refac: add collision method
This commit is contained in:
parent
42adfd907a
commit
87335c3b34
|
@ -0,0 +1,30 @@
|
||||||
|
import pygame.sprite
|
||||||
|
|
||||||
|
from games.easy_game.src.env import FoodTypeEnum, FOOD_COLOR_MAP
|
||||||
|
from mlgame.view.view_model import create_rect_view_data
|
||||||
|
|
||||||
|
|
||||||
|
class Food(pygame.sprite.Sprite):
|
||||||
|
def __init__(self, group, type: FoodTypeEnum):
|
||||||
|
pygame.sprite.Sprite.__init__(self, group)
|
||||||
|
self.image = pygame.Surface([8, 8])
|
||||||
|
self.type = type
|
||||||
|
self.score = 0
|
||||||
|
self.color = FOOD_COLOR_MAP[type]
|
||||||
|
|
||||||
|
self.rect = self.image.get_rect()
|
||||||
|
self.angle = 0
|
||||||
|
|
||||||
|
def update(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
def game_object_data(self):
|
||||||
|
return create_rect_view_data(
|
||||||
|
"food",
|
||||||
|
self.rect.x,
|
||||||
|
self.rect.y,
|
||||||
|
self.rect.width,
|
||||||
|
self.rect.height,
|
||||||
|
self.color
|
||||||
|
)
|
34
src/game.py
34
src/game.py
|
@ -10,7 +10,8 @@ from mlgame.view.decorator import check_game_progress, check_game_result
|
||||||
from mlgame.view.view_model import *
|
from mlgame.view.view_model import *
|
||||||
from .env import *
|
from .env import *
|
||||||
from .env import FoodTypeEnum
|
from .env import FoodTypeEnum
|
||||||
from .game_object import Ball, Food
|
from .game_object import Ball
|
||||||
|
from .foods import Food
|
||||||
from .sound_controller import SoundController
|
from .sound_controller import SoundController
|
||||||
|
|
||||||
|
|
||||||
|
@ -103,20 +104,8 @@ class EasyGame(PaiaGame):
|
||||||
self.foods.update()
|
self.foods.update()
|
||||||
|
|
||||||
# handle collision
|
# handle collision
|
||||||
hits = pygame.sprite.spritecollide(self.ball, self.foods, True)
|
|
||||||
if hits:
|
|
||||||
|
|
||||||
for food in hits:
|
self._check_foods_collision()
|
||||||
# TODO add food score to function
|
|
||||||
if food.type == FoodTypeEnum.GREEN:
|
|
||||||
self.sound_controller.play_eating_good()
|
|
||||||
self.score += 1
|
|
||||||
self._create_foods(1, FoodTypeEnum.GREEN)
|
|
||||||
|
|
||||||
elif food.type == FoodTypeEnum.RED:
|
|
||||||
self.sound_controller.play_eating_bad()
|
|
||||||
self._create_foods(1, FoodTypeEnum.RED)
|
|
||||||
self.score -= 1
|
|
||||||
# self._timer = round(time.time() - self._begin_time, 3)
|
# self._timer = round(time.time() - self._begin_time, 3)
|
||||||
|
|
||||||
self.frame_count += 1
|
self.frame_count += 1
|
||||||
|
@ -126,6 +115,21 @@ class EasyGame(PaiaGame):
|
||||||
if not self.is_running:
|
if not self.is_running:
|
||||||
return "RESET"
|
return "RESET"
|
||||||
|
|
||||||
|
def _check_foods_collision(self):
|
||||||
|
hits = pygame.sprite.spritecollide(self.ball, self.foods, True)
|
||||||
|
if hits:
|
||||||
|
for food in hits:
|
||||||
|
self.score+= food.score
|
||||||
|
# TODO add food score to function
|
||||||
|
if food.type == FoodTypeEnum.GREEN:
|
||||||
|
self.sound_controller.play_eating_good()
|
||||||
|
self._create_foods(1, FoodTypeEnum.GREEN)
|
||||||
|
|
||||||
|
elif food.type == FoodTypeEnum.RED:
|
||||||
|
self.sound_controller.play_eating_bad()
|
||||||
|
self._create_foods(1, FoodTypeEnum.RED)
|
||||||
|
|
||||||
|
|
||||||
def get_data_from_game_to_player(self):
|
def get_data_from_game_to_player(self):
|
||||||
"""
|
"""
|
||||||
send something to game AI
|
send something to game AI
|
||||||
|
@ -136,7 +140,7 @@ class EasyGame(PaiaGame):
|
||||||
for food in self.foods:
|
for food in self.foods:
|
||||||
# TODO add good food and bad food
|
# TODO add good food and bad food
|
||||||
|
|
||||||
foods_data.append({"x": food.rect.x, "y": food.rect.y,"type":food.type})
|
foods_data.append({"x": food.rect.x, "y": food.rect.y,"type":food.type,"score":food.score})
|
||||||
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,
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import pygame.sprite
|
import pygame.sprite
|
||||||
|
|
||||||
from games.easy_game.src.env import FoodTypeEnum, FOOD_COLOR_MAP, BALL_COLOR, BALL_VEL, BALL_H, BALL_W
|
from games.easy_game.src.env import BALL_COLOR, BALL_VEL, BALL_H, BALL_W
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,26 +45,3 @@ class Ball(pygame.sprite.Sprite):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Food(pygame.sprite.Sprite):
|
|
||||||
def __init__(self, group, type: FoodTypeEnum):
|
|
||||||
pygame.sprite.Sprite.__init__(self, group)
|
|
||||||
self.image = pygame.Surface([8, 8])
|
|
||||||
self.type = type
|
|
||||||
self.color = FOOD_COLOR_MAP[type]
|
|
||||||
|
|
||||||
self.rect = self.image.get_rect()
|
|
||||||
self.angle = 0
|
|
||||||
|
|
||||||
def update(self) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
@property
|
|
||||||
def game_object_data(self):
|
|
||||||
return create_rect_view_data(
|
|
||||||
"food",
|
|
||||||
self.rect.x,
|
|
||||||
self.rect.y,
|
|
||||||
self.rect.width,
|
|
||||||
self.rect.height,
|
|
||||||
self.color
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in New Issue