Reputation: 67
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_Grid():
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)
##draw_Grid()
##pygame.display.update()
class spurgs:
def __init__(self,age,hunger,speed,gender):
# 1 for male, 2 for female
self.hunger = hunger
self.speed = speed
self.gender = gender
self.age = age
def update(self):
#growing up
Here I am setting the variable it should be working but it isnt ??!?
self.age = self.age + 1
self.hunger -= 1
if self.age > 10:
self.speed += 1
elif self.age >50:
self.speed -= 1
def eat(self):
self.hunger +=1
#remove some food to affect global food resource
def breed(self,mate):
if mate.gender != self.gender:
baby_gender = random.randint(1,2)
baby = spugs(1,100,20,baby_gender)
def draw(self,color,grid_coord_X,grid_coord_Y,size):
pygame.draw.circle(window,color,(grid_coord_X,grid_coord_Y),size)
run = True
while run:
window.fill((0,200,255))
draw_Grid()
#QUIT CONDITION
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("closing the loop")
run = False
test_spurg = spurgs(10,100,1,2)
test_spurg.draw((0,255,20),20,20,5)
test_spurg.update()
print(test_spurg.age)
Here I use the update function in a while loop it should change the age on every iteration i.e infintely
pygame.display.update()
pygame.quit()
If there are any other syntax errors or I'm just not adhering to some common rule, feel free to point it out !
Upvotes: 1
Views: 66
Reputation: 211278
Actually, the value of the attribute is changed, but since you create a new object every frame, it will continuously start with the initial value.
You need to create the instance of the object before the application loop, but you need to draw the scene in the application loop:
# create objects
test_spurg = spurgs(10,100,1,2)
run = True
while run:
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("closing the loop")
run = False
# update objects
test_spurg.update()
print(test_spurg.age)
# draw scene
window.fill((0,200,255))
draw_Grid()
test_spurg.draw((0,255,20),20,20,5)
pygame.display.update()
The objects are created before the application loop. In the application loop, the attributes of the object, e.g. B. its position is continuously changed and the scene is redrawn to reflect the changes. The typical PyGame application loop has to:
pygame.event.pump()
or pygame.event.get()
.blit
all the objects)pygame.display.update()
or pygame.display.flip()
Upvotes: 1
Reputation: 9600
It is because you are creating a new object in every iteration.
I think you need to create a new object only once and use that in the while
loop as:
window.fill((0,200,255))
draw_Grid()
test_spurg = spurgs(10,100,1,2)
test_spurg.draw((0,255,20),20,20,5)
while run:
#QUIT CONDITION
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("closing the loop")
run = False
test_spurg.update()
print(test_spurg.age)
Upvotes: 1