Issue
I am creating an Asteroids Game. the game is intended for use over long periods of time.
on startup (using pycharms debugg tool) i am using roughly 30.1 MB of system memory. however i have noticed that while running, it increases by roughly 0.1 MB every second (with a fixed fps of 60). while testing and setting fps to unlimited, i have noticed that memory usage increases respectively to fps increase.
import pygame
import math
import random
import threading
class Main:
def __init__(self):
self.image = None
self.Dimensions = 1000
self.display = pygame.display.set_mode((self.Dimensions, self.Dimensions))
self.fps = 60
self.MainPage = True
self.Game = False
self.clock = pygame.time.Clock()
pygame.init()
def start(self):
self.running = True
self.character = Player()
self.handler = EventHandler()
self.display = pygame.display.set_mode((self.Dimensions, self.Dimensions))
while self.running:
self.event_list = pygame.event.get()
for self.event in self.event_list:
if self.event.type == pygame.QUIT:
self.running = False
pygame.display.quit()
pygame.quit()
quit()
white = [255, 255, 255]
self.display.fill(white)
self.Dimensions = 1000
self.clock.tick(self.fps)
self.handler.spawn()
self.handler.update()
pygame.display.flip()
class Player:
def __init__(self):
self.Dimensions = Game.Dimensions
self.x = self.Dimensions / 2
self.y = self.Dimensions / 2
self.looking_directions = 0
self.velocity = 0
self.image = "Pictures/Player.png"
class Asteroids():
def __init__(self):
self.size = 100
self.x = random.choice(list(set([x for x in range(0, 9)]) - set(range(int(Game.Dimensions/5), int(Game.Dimensions*(4/5))))))
self.y = random.choice(list(set([x for x in range(0, 9)]) - set(range(int(Game.Dimensions/5), int(Game.Dimensions*(4/5))))))
self.velocity = random.uniform(1, 2)
self.looking_direction = random.randrange(0, 360)
self.picture = "Pictures/Asteroids.png"
size = 75
self.image = pygame.image.load(self.picture)
self.image = pygame.transform.scale(self.image, (size, size))
self.image = pygame.transform.rotate(self.image, self.looking_direction)
def Update(self):
Game.display.blit(self.image, (self.x, self.y))
class EventHandler():
def __init__(self):
self.asteroid_list = []
self.tick = 0
self.asteroid_cap = 15
def spawn(self):
self.tick += 1
if len(self.asteroid_list) >= self.asteroid_cap:
pass
elif self.tick >= 60:
temp_asteroid = Asteroids()
self.asteroid_list.append(temp_asteroid)
print("Asteroid created: " + str(len(self.asteroid_list)) + " currently alive")
self.tick = 0
def update(self):
for current in self.asteroid_list:
x = threading.Thread(target=current.Update)
x.start()
try:
x.join()
except:
pass
if __name__ == "__main__":
Game = Main()
Game.start()
EDIT: the memory increase is coming from this function. However now i want to know how to change this change this function so i can keep the threads without the memory increase
def update(self):
for current in self.asteroid_list:
x = threading.Thread(target=current.Update)
y = threading.Thread(target=current.Drift)
x.start()
y.start()
try:
x.join()
y.join()
except:
pass
I want to keep the threading (or something as efficient) since from my experience pygame is a bit slow in blitting multiple images at once, so using the loop, i can start the process of blitting them asap and then once they have all started, i call x.join() so no matter what, the threads do not fall behind a frame causing visual bugs or the like.
Thanks in advance
Solution
Removed threading entirely. from the issue and fixed the initial image loading of every class instance
Answered By - Kamil Leocadie-Olsen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.