1

Имеется следующий код. И проблема у меня возникла с методом eating и think класса smallFish. При выполнения моего кода, мало того что рыбки поедат водоросли они еще и умудряються размножаться, не могу понять с чем это связанно. Логика поедание водросли: если вокруг есть водросль рыбка ее ест ( становиться на место водросли и старое место мы забиваем обьектом place а в новом месте уже эта рыбка - smallFish)

import pygame
from pygame import *
import random

width,height = 100, 50;
directions = [
    [-1,0],
    [1,0],
    [0,1],
    [0,-1],
    [-1,1],
    [-1,-1],
    [1,-1],
    [1,1],
    [0,0],
    ]

def derictionRandomazer(arr):
    random.shuffle(arr);
    return arr;

def addObjects(obj,objCount,count):
    while count <= objCount:
        x = random.randint(1,width-2);
        y = random.randint(1,height-2);
        if type(pond[x][y]) == Place:
            pond[x][y] = obj(x,y);
            pond[x][y].spawn();
            count += 1
    pygame.display.flip();

pygame.init();

screen = pygame.display.set_mode( (width * 10, height * 10) )
pygame.display.set_caption("Life in Pond"); 

class Place:
    color = (52,152,219)
    def __init__(self, x,y):
        self.x = x;
        self.y = y ;

    def spawn(self):
        square = pygame.Rect(self.x*10, self.y*10, 10, 10);
        pygame.draw.rect(screen, self.color, square);

class Stone(Place):
    color = (0,0,0);

class Seaweed(Place):
    lifePeriod = 40;
    color = (2,124,2);
    reproductivePeriod = 10;
    def __init__(self,x,y):
        super().__init__(x,y);
        self.age = 0;

    def aging(self):
        self.age += 1;
        if self.age == self.lifePeriod:
            self.dying();


    def dying(self):
        pond[self.x][self.y] = Place(self.x, self.y);
        pond[self.x][self.y].spawn();
 

pond = [[ 0 for x in range(height)] for y in range(width)]

for y in range(height):
    for x in range(width):
        pond[x][y] = Place(x,y);
        pond[x][y].spawn();

pygame.display.flip();
num = 0;

class smallFish(Seaweed):
    lifePeriod = 30;
    color = (0, 77, 255);
    reproductivePeriod =  5;

    def __init__(self,x,y):
        super().__init__(x,y)
        self.age = 0;
        self.hunger =  8;


    def reproduction(self,obj):
        if self.age % self.reproductivePeriod == 0:
            for direction in derictionRandomazer(directions):
                new_x = self.x + direction[0];
                new_y = self.y + direction[1];
            if 0 <= new_x < width and 0 <= new_y < height:
                if type(pond[new_x][new_y]) == Place:
                    pond[new_x][new_y] = obj(new_x,new_y);
                    pond[new_x][new_y].spawn();

    def aging(self):
        self.age += 1;
        if self.age == self.lifePeriod:
            self.dying();

    def hungering(self):
        self.hunger -= 1;
        if self.hunger == 0:
            self.dying();

    def think(self,obj):
        for direction in directions:
            new_x = self.x + direction[0];
            new_y = self.y + direction[1];
            if 0 <= new_x < width and 0 <= new_y < height:
                if type(pond[new_x][new_y]) == Seaweed:
                    self.eating(self.x,self.y,new_x,new_y,obj);


        for direction in derictionRandomazer(directions):   
                new_x = self.x + direction[0];
                new_y = self.y + direction[1];
        if 0 <= new_x < width and 0 <= new_y < height:
            if type(pond[new_x][new_y]) == Place:
                self.swim(self.x,self.y,new_x,new_y,obj);

    def reproduction(self,obj):
        if self.age % self.reproductivePeriod == 0:
            for direction in derictionRandomazer(directions):
                new_x = self.x + direction[0];
                new_y = self.y + direction[1];
            if 0 <= new_x < width and 0 <= new_y < height:
                if type(pond[new_x][new_y]) == Place:
                    pond[new_x][new_y] = obj(new_x,new_y);
                    pond[new_x][new_y].spawn();

    def eating(self,x,y,new_x,new_y,obj):
            pond[x][y] = Place(x,y);
            pond[x][y].spawn();
            
            pond[new_x][new_y] = smallFish(new_x,new_y);
            pond[new_x][new_y].spawn();
            self.hunger =+ 1;
        
    def swim(self,x,y,new_x,new_y,obj):
        pond[x][y] = Place(x,y);
        pond[x][y].spawn();
        pond[new_x][new_y] = obj(new_x,new_y);
        pond[new_x][new_y].spawn(); 



addObjects(smallFish,1,0);
addObjects(Stone,50,0);
addObjects(Seaweed,1,0);


while True:
    for y in range(height):
        for x in range(width):
            #  or type(pond[x][y]) == predatoryFish

            if type(pond[x][y]) == Seaweed:
                pond[x][y].aging();
                if type(pond[x][y]) == Seaweed :
                    pond[x][y].reproduction(Seaweed);
    


            if type(pond[x][y]) == smallFish:
                    pond[x][y].aging();
                    # pond[x][y].reproduction(smallFish);
                    pond[x][y].hungering();
                    if type(pond[x][y]) == smallFish:
                        pond[x][y].think(smallFish);


    pygame.display.flip();
0

1 ответ 1

1

Проблема всетаки была в том, что я создаю новые обьекты, а не работаю с имеющимся, поэтому я просто добавил новую координату к "себе".

pond[new_x][new_y] = self;
pond[new_x][new_y].spawn();

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.