0

I'm working on a basic program which should simulate the life of a species over time. The user can choose settings like the chance of reproduction or the chances of dying.

Since I have used PyGame for some basic stuff before, I chose to use PyGame (and Python 2.7).

I just started, so I'm using circles instead of animals and there aren't too many settings but I ran into a problem:

The text on the screen isn't changing even tho it should be. In the function called ManageTime there is a loop which always waits a few seconds and should then update the number on the screen.

import pygame
import random
import time
from multiprocessing import Process

pygame.init()

#Variables
amountStart = input("How many circles should there be at the start? ")
timePassing = input("How many seconds should 1 year take?")
running = True
BLACK = (0,0,0)
WHITE = (255, 255, 255, 1)
circle_amount = 0
current_year = 0

#Create and set up window
(width, height) = (900, 600)

screen = pygame.display.set_mode((width, height))
pygame.display.flip()
screen.fill(WHITE)


#Functions

def drawCircles(amount):
    screen.fill(WHITE)
    amount = int(amount)
    global circle_amount
    while amount > 0:
        amount -= 1
        circle_amount += 1
        xcord = random.randint(0,900)
        ycord = random.randint(0,600)
        print(xcord,ycord)
        pygame.draw.circle(screen, (0,255,0), (xcord,ycord), 50)
        pygame.display.update()

#Draw circles

drawCircles(amountStart)

#Font

font = pygame.font.SysFont(None, 24)
img = font.render(str(circle_amount), True, BLACK)
screen.blit(img, (20, 20))

pygame.display.update()

#Time passing
def ManageTime():
    global current_year
    global timePassing
    global year
    while True:
        time.sleep(int(timePassing))
        current_year += 1
        print("year:", current_year, "time passing:", timePassing)
        year = pygame.font.render(str(current_year), True, BLACK)
        screen.blit(year, (0, 0))

#Game loop

p = Process(target=ManageTime)
p.start()

while running:
    for event in pygame.event.get():
        #Update circle count
        img = font.render(str(circle_amount), True, BLACK)
        screen.blit(img, (20, 20))

        #Update
        pygame.display.update()
Nimantha
  • 5,793
  • 5
  • 23
  • 56
MF714
  • 133
  • 6
  • 1
    Trying to manipulate objects like `screen` across processes isn't possible like that. Multiprocessing introduces many complexities, like requiring proper inter-process communication. You'll need to use threads instead (although, they have their own problems), or communicate the data you want to display back to the main process from the child process, and have the main process display it. – Carcigenicate Jun 17 '21 at 13:15
  • Thx I'll try to use threading – MF714 Jun 17 '21 at 13:18
  • 1
    Note though that threads have their own problems. You need to limit what you do in child threads since all threads are sharing a lock, and only one thread can execute at once. – Carcigenicate Jun 17 '21 at 13:19

0 Answers0