Code slows down


3 posts
by kdamav7949 » Sun May 14, 2017 7:22 pm
Hi. I am making a game. For the fighting mechanics, the target is supposed to move around a certain area on the screen. When the target is clicked, it loses one health. When I run this code, it is extremely slow. When I comment out the code that registers the click and takes away health, the program runs smoothly. The program uses multiple files, so I will show the code that is relevant.

The Main File:
Code: Select all
Have the player fight the enemy
Fighting.melee(Enemies.TutorialEnemyMelee)



A file with a list of enemies (Enemies.py):
Code: Select all
TutorialEnemyMelee = (pygame.image.load(os.path.join(sys.path[0], 'puzque/Fighting/TutorialEnemy.png')).convert(), 10, 2, 1, 'puzque/Fighting/TutorialEnemy.png')


The file that has the fighting commands (Fighting.py):
Code: Select all
#Fighting Mechanics

#import libraries
import pygame
from Enemies import Enemies
import random
import time
import sys, os
from threading import Thread
Enemies = Enemies()

pygame.init
screenSize = (700, 500)
screen = pygame.display.set_mode(screenSize)

'''NOTES:
Veiw screen size: 1
'''


           
class Fighting:

    def __init__(self):
        self.oldEnemyCoordX = random.randint(10.0, 322.0)
        self.oldEnemyCoordY = random.randint(0.0, 122.0)
   
    def imgEnemy(self, xcoor, ycoor, bgkey, enemyIMG):
        imgLocation = os.path.join(sys.path[0], enemyIMG)
        TemplateIMG = pygame.image.load(imgLocation).convert()
        if bgkey != 0:           
            TemplateIMG.set_colorkey(bgkey)
        screen.blit(TemplateIMG, [xcoor, ycoor])
        pygame.display.flip()
   
    def clearVeiwScreen(self):
        pygame.draw.rect(screen, (255, 255, 255), [0, 0, 400, 200], 0)
        pygame.display.flip()
   
    def melee(self, enemy):
        self.clearVeiwScreen()
        fEnemy = list(enemy)
        def meleeMove():
            xChange = fEnemy[2]
            yChange = fEnemy[2]
            while fEnemy[1] > 0:
                pygame.draw.rect(screen, (255, 255, 255), [self.oldEnemyCoordX, self.oldEnemyCoordY, 70, 70], 0)
                if self.oldEnemyCoordX < 10 or self.oldEnemyCoordX > 320:
                    xChange = xChange * (-1)
                if self.oldEnemyCoordY < 10 or self.oldEnemyCoordY > 120:
                    yChange = yChange * (-1)               
                self.oldEnemyCoordX += xChange
                self.oldEnemyCoordY += yChange
                self.imgEnemy(self.oldEnemyCoordX, self.oldEnemyCoordY, 0, fEnemy[4])
                time.sleep(0.001)
        def loseHealth():
            while fEnemy[1] > 0:
                for event in pygame.event.get():
                    pos = pygame.mouse.get_pos()
                    if event.type == pygame.MOUSEBUTTONUP:
                        if pos[0] > self.oldEnemyCoordX:
                            if pos[0] < self.oldEnemyCoordX+70:
                                if pos[1] > self.oldEnemyCoordY:
                                    if pos[1] < self.oldEnemyCoordY+70:
                                        print('You got me!')
                                        fEnemy[1] -= 1
                    elif event.type == pygame.QUIT:
                        pygame.quit()
                       
        Thread(target=meleeMove, args=()).start()
        Thread(target=loseHealth, args=()).start()



The code does work, it is just super slow...
If I forgot to post something or something doesnt make sense, please tell me and I will fix it.

Thanks :)
kdamav7949
Program a game to find that your chosen name is already taken.
Code: Select all
if gameName is already being used:
    print('You are very angry!')
    self.BlowUp

Do some research before choosing a name.
Posts: 39
Joined: Tue Sep 01, 2015 5:22 pm
by topguy » Mon May 15, 2017 12:29 pm
I'm guessing your looseHealth() function is using 100% CPU. You should probably use pygame.event.wait() instead of "get()". ( note! wait() return single even not a list of events. )
You should probably also use pygame.event.set_allowed()/set_blocked() to only receive the type of events you need.
User avatar
Posts: 3221
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway
by kdamav7949 » Fri May 19, 2017 1:56 pm
topguy wrote:I'm guessing your looseHealth() function is using 100% CPU. You should probably use pygame.event.wait() instead of "get()". ( note! wait() return single even not a list of events. )
You should probably also use pygame.event.set_allowed()/set_blocked() to only receive the type of events you need.


I think it is a CPU problem like you said. Thanks for the help, but how do I use pygame.wait/pygame.event.set_allowed()/set_blocked() ? :geek: I don't know much more than the very basics of pygame, so...



Thanks
kdamav7949
Program a game to find that your chosen name is already taken.
Code: Select all
if gameName is already being used:
    print('You are very angry!')
    self.BlowUp

Do some research before choosing a name.
Posts: 39
Joined: Tue Sep 01, 2015 5:22 pm