pygame simple game lagging

hi i am beginner in programming and pygame and i was trying to make game like this

http://dan-ball.jp/en/javagame/dust/

and i started by making this simple code

import pygame
from pygame.locals import*
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (0,25)
pygame.init()
screen=pygame.display.set_mode((1360,705))
clock = pygame.time.Clock()
boxs=[]
while True :
     screen.fill((0,0,0))
     for event in pygame.event.get():
                if event.type==QUIT :
                    pygame.quit()
                    quit()
                if event.type== pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                           mous=pygame.math.Vector2(event.pos[0]-4,event.pos[1]-4)
                           boxs.append(mous)
                if event.type== pygame.MOUSEBUTTONDOWN:
                     if event.button == 3:
                          print (len(boxs))
     for i in range(len(boxs)): 
          if ((boxs[i])[1])>=685:
               pass
          else:
               (boxs[i])[1]=(boxs[i])[1]+2
               for v in range (len(boxs)):
                    if v==i:
                         pass 
                    else :
                         if (int((boxs[i])[1]))+4 >= (int((boxs[v])[1])) and (int((boxs[i])[1])) <= (int((boxs[v])[1]))+4 and (int((boxs[i])[0]))+4 >= (int((boxs[v])[0])) and (int((boxs[i])[0])) <= (int((boxs[v])[0]))+4:
                               (boxs[i])[1]=(boxs[i])[1]-2
                               break 
          pygame.draw.rect(screen,(250,250,250),((boxs[i])[0],(boxs[i])[1], 4, 4))     
     pygame.display.update()
     clock.tick(60)

it works very good as start but my problem is that when the boxes became more the 350 or 400 the game will be lagging too much i dont know what iam doing wrong

and sorry if i was asking too much bad questions but thank you so much i have learned so much from you guys

Upvotes: 0

Views: 246

Answers (1)

Chris
Chris

Reputation: 898

I notice that you're doing this:

if ((boxs[i])[1])>=685:
    pass

That means that you're never deleting boxes that go off the bottom of the screen, so as time goes by, your box list is getting larger and larger, which is going to eventually lead to lag.

You're also using Pygame vectors for your list, and then not taking advantage of them. For example, instead of:

(boxs[i])[1]=(boxs[i])[1]+2

you can say:

boxs[i].y += 2

Finally, you are not looping in a Pythonic way. In Python, you almost never need to loop with an index variable. Instead, you should be doing something like this:

for box in boxs: 
      if box.y >= 685:
           pass
      else:
           box.y += 2
           for other in boxs:
                if other == box:
                     pass 

and so on...

Finally, you should be using pygame Rect() objects, because then you can use the builtin pygame collision functions to easily handle your collisions.

http://www.pygame.org/docs/ref/rect.html

Upvotes: 1

Related Questions