0
import random, pygame


#window setup aswell as global variable
pygame.init()
screen_size = 400
global window
window = pygame.display.set_mode((screen_size,screen_size))
pygame.display.set_caption("evolution simulator")
pygame.display.update()

def draw_Window():
    global Grid
    Grid = [[]]
    blockSize = 20
    for x in range(0,20):
        for y in range(0,20):
            Grid.append(1)
            rect = pygame.Rect(x*blockSize, y*blockSize,blockSize, blockSize)
            pygame.draw.rect(window,(0,0,0), rect, 1)




class spurgs:
    def __init__(self,age,hunger,speed,gender,x,y,race):
        # 1 for male, 2 for female
        self.hunger = hunger
        self.speed = speed
        self.gender = gender
        self.age = age
        self.race = race
        self.x = x
        self.y = y
        self.size = 8
    def update(self):
        self.age = self.age + 1
        self.draw()


#SURVIVAL ELEMENTS
    def eat(self):
        self.hunger +=1
        #remove some food  to affect global food resource
    def breed(self,mate):
        # In the generation class, make a list for the next generation and append baby to the list
        pass
# MOVEMENT
    def move(self,dir):
        if dir == "right":
            self.x += 1
        self.update()


#DRAWING
    def draw(self):
        #for grid system
        x = self.y * 20 - 10
        y = self.y * 20 - 10
        pygame.draw.circle(window,self.race,(x,y),self.size)




run = True
test_spurg = spurgs(age = 15,hunger = 100,speed = 0.001,gender = 2, x = 1,y = 1,race = (0,255,100))

年龄 = 15 等时额外增加一点点,但我更容易看,但问题不是来自正在初始化的实例

while run:
    #WINDOW
    window.fill((0,200,255))
    draw_Window()
    #QUIT CONDITION
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            print("closing the loop")
            run = False
    #TESTING
    print (test_spurg.x,test_spurg.y)
    test_spurg.move("right")
    test_spurg.update()

我在这里打印 x 和 y 以检查它们是否在增加并且它是否正常运行

    #FINAL LOOP
    pygame.display.update()
    pygame.time.delay(1000)
pygame.quit()

我使用延迟来减慢更新功能

如果我在标准化表格方面犯了任何错误,请告诉我!

4

1 回答 1

0

问题出在您的draw()功能中:

    def draw(self):
        #for grid system
        x = self.y * 20 - 10
        y = self.y * 20 - 10
        pygame.draw.circle(window,self.race,(x,y),self.size)

如您所见,xinx = self.y * 20 - 10需要替换为yto be x = self.x * 20 - 10

于 2021-02-18T13:21:41.327 回答