Why is this thing lagging so much?

Why is this thing lagging so much?

Postby Hissing Cockroach » Thu Oct 03, 2013 9:28 pm

Hi, I am a beginner at python.
I have a code that whenever you shoot the target
with the crosshairs, it saves the screen onto Highscores.png:

Code: Select all
import pygame as pg
import math
import sys

WHITE    = (255, 255, 255)
BLACK    = (  0,   0,   0)
RED = (255, 0, 0)

BGCOLOR = WHITE

WINDOWWIDTH = 640
WINDOWHEIGHT = 480



       
class Control(object):

    def __init__(self):
        self.bullet_holes = []
        self.screen = pg.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
        self.done = False
        self.clock = pg.time.Clock()

    def update(self):
        self.mousex,self.mousey = pg.mouse.get_pos()
        self.screen.fill(BGCOLOR)
        pg.draw.circle(self.screen, RED, (320,240),50,10)
        pg.draw.circle(self.screen, WHITE, (320,240),40,10)
        pg.draw.circle(self.screen, RED, (320,240),30,10)
        pg.draw.circle(self.screen, WHITE, (320,240),20,10)
        pg.draw.circle(self.screen, RED, (320,240),10,10)
        pg.draw.line(self.screen, BLACK, (self.mousex - 2000, self.mousey), (self.mousex + 2000, self.mousey))
        pg.draw.line(self.screen, BLACK, (self.mousex, self.mousey - 2000), (self.mousex, self.mousey + 2000))
        for bullet_pos in self.bullet_holes:
            pg.draw.circle(self.screen,BLACK,bullet_pos,5)
            pg.image.save(self.screen,"Highscores.png")

    def event_loop(self):
        for event in pg.event.get():
            if event.type ==  pg.QUIT or (event.type == pg.KEYUP and event.key == pg.K_ESCAPE):
                self.done = True
            if pg.mouse.get_pressed()[0]:
                self.bullet_holes.append((self.mousex,self.mousey))   
               
    def main_loop(self):
        while not self.done:
            self.update()
            self.event_loop()
            pg.display.flip()
            self.clock.tick(60)

   

if __name__ == '__main__':
    game = Control()
    game.main_loop()
    pg.quit()
    sys.exit()


But, it starts to lag after the first two
shots are fired - then gets so laggy I can't
barely fire. Is this just my computor?
Can I fix this? If I can, how?
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Why is this thing lagging so much?

Postby dboxall123 » Thu Oct 03, 2013 10:05 pm

The reason this is lagging is because the image is being copied to the disk many more times than it needs to. The screen is updating at 60 fps. That means that even when you have fired one shot, it's saving the image to your hard disk 60 times a second. Once it has two shots it's saving the image 120 per second, and so on. You could move the save method to your event loop, so it only overwrites the image every time the mouse is clicked.
Code: Select all
def event_loop(self):
        for event in pg.event.get():
            if event.type ==  pg.QUIT or (event.type == pg.KEYUP and event.key == pg.K_ESCAPE):
                self.done = True
            if pg.mouse.get_pressed()[0]:
                self.bullet_holes.append((self.mousex,self.mousey))   
                pg.image.save(self.screen,'highscore.png')

Or, you could set up a loop so that it only allows you to take like 5 shots, and then save the image. That would probably be better.
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Why is this thing lagging so much?

Postby Hissing Cockroach » Thu Oct 03, 2013 10:19 pm

Wow! This really helped alot!
Thanks!
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Why is this thing lagging so much?

Postby DrakeMagi » Thu Oct 03, 2013 11:23 pm

this is wrong
Code: Select all
for event in pygame.event.get():
    # wrong way ! can run more then once
    if pygame.mouse.get_pressed()[0]


here an option
Code: Select all
for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN:
        if event.button == 1: # left mouse button down
            # do something


here an example why it wrong
Code: Select all
import pygame
pygame.init()

# if you see yellow or red error
# hold left mouse button down and move mouse.

def main():
    screen_size = pygame.Rect(0,0,800,600)
    pygame.display.set_caption('Pygame Example')
    screen = pygame.display.set_mode(screen_size.size)
    clock = pygame.time.Clock()
   
    running = True
    while running:
        count = 0
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
           
            # wrong way
            if pygame.mouse.get_pressed()[0]:
                count += 1
               
        if count == 0:
            color = (0,0,0)
        elif count == 1:
            color = (0,200,0)
        elif count == 2:
            color = (220,220,0)
        else:
            color = (200,0,0)
            print(count)
       
        screen.fill(color)
        pygame.display.flip()
        clock.tick(20)
       
    pygame.quit()
   
main()
DrakeMagi
 
Posts: 72
Joined: Sun May 12, 2013 8:36 pm

Re: Why is this thing lagging so much?

Postby dboxall123 » Fri Oct 04, 2013 12:23 am

this is wrong

Code: Select all
    for event in pygame.event.get():
        # wrong way ! can run more then once
        if pygame.mouse.get_pressed()[0]

Oh yeh, forgot about that. I did actually post an updated event loop in his last thread. Guess he decided not to use it :(
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Why is this thing lagging so much?

Postby Hissing Cockroach » Fri Oct 04, 2013 9:10 pm

dboxall123 wrote:Oh yeh, forgot about that. I did actually post an updated event loop in his last thread. Guess he decided not to use it :(


I tried to use it... but unfortunately it didn't really work too well.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Why is this thing lagging so much?

Postby dboxall123 » Fri Oct 04, 2013 9:26 pm

Code: Select all
def event_loop(self):
        for event in pg.event.get():
            if event.type ==  pg.QUIT or (event.type == pg.KEYUP and event.key == pg.K_ESCAPE):
                self.done = True
            elif event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
                self.bullet_holes.append(event.pos)

Works perfectly.
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Why is this thing lagging so much?

Postby Hissing Cockroach » Sat Oct 05, 2013 12:27 am

This is what it says in Run Module:

Code: Select all
Traceback (most recent call last):
  File "C:\Users\Family\Desktop\pys\Crosshairs(2).py", line 59, in <module>
    game.main_loop()
AttributeError: 'Control' object has no attribute 'main_loop'
>>>
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Why is this thing lagging so much?

Postby dboxall123 » Sat Oct 05, 2013 1:45 am

Hmmm. Looks to me like you replaced the main loop instead of the event loop.
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Why is this thing lagging so much?

Postby Hissing Cockroach » Sat Oct 05, 2013 10:15 pm

I replaced the event loop with it.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Why is this thing lagging so much?

Postby dboxall123 » Sat Oct 05, 2013 10:26 pm

Lol, how has the main_loop() disappeared then?
Code: Select all
import pygame as pg
import math
import sys

WHITE    = (255, 255, 255)
BLACK    = (  0,   0,   0)
RED = (255, 0, 0)

BGCOLOR = WHITE

WINDOWWIDTH = 640
WINDOWHEIGHT = 480



       
class Control(object):

    def __init__(self):
        self.bullet_holes = []
        self.screen = pg.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
        self.done = False
        self.clock = pg.time.Clock()

    def update(self):
        self.mousex,self.mousey = pg.mouse.get_pos()
        self.screen.fill(BGCOLOR)
        pg.draw.circle(self.screen, RED, (320,240),50,10)
        pg.draw.circle(self.screen, WHITE, (320,240),40,10)
        pg.draw.circle(self.screen, RED, (320,240),30,10)
        pg.draw.circle(self.screen, WHITE, (320,240),20,10)
        pg.draw.circle(self.screen, RED, (320,240),10,10)
        pg.draw.line(self.screen, BLACK, (self.mousex - 2000, self.mousey), (self.mousex + 2000, self.mousey))
        pg.draw.line(self.screen, BLACK, (self.mousex, self.mousey - 2000), (self.mousex, self.mousey + 2000))
        for bullet_pos in self.bullet_holes:
            pg.draw.circle(self.screen,BLACK,bullet_pos,5)
           
    def event_loop(self):
        for event in pg.event.get():
            if event.type ==  pg.QUIT or (event.type == pg.KEYUP and event.key == pg.K_ESCAPE):
                self.done = True
            elif event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
                self.bullet_holes.append(event.pos)
                pg.image.save(self.screen,'high.png')

    def main_loop(self):
        while not self.done:
            self.update()
            self.event_loop()
            pg.display.flip()
            self.clock.tick(60)

   

if __name__ == '__main__':
    game = Control()
    game.main_loop()
    pg.quit()
    sys.exit()

Try it.
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Why is this thing lagging so much?

Postby Hissing Cockroach » Sat Oct 05, 2013 11:53 pm

That's weird. It worked!
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest