feat:add new class and use game params model

This commit is contained in:
Kylin_on_Mac
2023-11-09 16:23:39 +08:00
parent c777814018
commit c4953cd57a
5 changed files with 90 additions and 26 deletions

View File

@ -1,4 +1,8 @@
import math
import random
import pygame.sprite
from pygame import Rect
from .env import FoodTypeEnum, FOOD_COLOR_MAP, FOOD_LV1_SIZE, FOOD_LV2_SIZE, FOOD_LV3_SIZE
from mlgame.view.view_model import create_rect_view_data
@ -14,8 +18,10 @@ class Food(pygame.sprite.Sprite):
self.rect = self.image.get_rect()
self.angle = 0
def update(self) -> None:
def set_center_x_and_y(self,x:int,y:int):
self.rect.centerx = x
self.rect.centery = y
def update(self,*args,**kwargs) -> None:
pass
@property
@ -38,6 +44,7 @@ class GoodFoodLv1(Food):
self.color = FOOD_COLOR_MAP[self.type]
self.score = 1
self.rect = self.image.get_rect()
class GoodFoodLv2(Food):
def __init__(self, group):
super().__init__(group)
@ -57,7 +64,48 @@ class GoodFoodLv3(Food):
self.color = FOOD_COLOR_MAP[self.type]
self.score = 4
self.rect = self.image.get_rect()
class Fish(Food):
def __init__(self, group):
super().__init__(group)
self.image = pygame.Surface([FOOD_LV1_SIZE, FOOD_LV1_SIZE])
self.type = FoodTypeEnum.GOOD_1
self.color = FOOD_COLOR_MAP[self.type]
self.score = 1
self.rect = self.image.get_rect()
self._target_x=400
self._target_y=400
self._direction = 1
self._vel=2
self._vely=1
def update(self,playground:Rect):
if self._direction==1:
if self.rect.centerx > self._target_x:
# change
self._direction= - self._direction
self._target_x = random.randint(playground.left, playground.right)
self._target_y = self.rect.centery + random.randint(-20,20)
self._vely = self._vel * ((self._target_y - self.rect.centery)/(self._target_x - self.rect.centerx) )
pass
# move to right
self.rect.centerx += self._vel
self.rect.centery = self.rect.centery +self._vely
elif self._direction==-1:
if self.rect.centerx < self._target_x:
# change
self._direction= - self._direction
self._target_x = random.randint(playground.left, playground.right)
self._target_y = self.rect.centery + random.randint(-50,50)
self.rect.centery = self.rect.centery + self._vely
pass
self.rect.centerx -= self._vel
self.rect.centery -= math.sin(self.rect.centerx)
pass
pass