Why does the it not update the circle should move one to the right?

50 Views Asked by At
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))

Tiny bit extra with age = 15 and so on but i was making easier to look at but the problem isn't coming from the instance being initialised

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()

I here I print x and y to check they are increasing and it is acting as normal

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

I use a delay to slow down the update function

If i have made any errors when it comes to standardised form please tell me !!

1

There are 1 best solutions below

2
On

Th problem is in your draw() function:

    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)

As you can see, the x in x = self.y * 20 - 10 needs to be replaced with a y to be x = self.x * 20 - 10.