user4080732
user4080732

Reputation: 157

Blinking rectangle in pygame

I am trying to learn pyGame. And I ran into a problem. I have a rectangle which can be moved with arrow buttons. Then I created another thread which generates smaller rectangles which can be picked up. But when I run my game, the small generated rectangles blink too much. How can I make them be stable? I think I dont understand time concept here well. Could someone explain me etc

My code:

import pygame
import random
import threading
import thread
import sys
import time

pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
is_blue = True
entityList = []
x = 30
y = 30

clock = pygame.time.Clock()
class Entity():

     def __init__(self, x, y):
         self.x = x
         self.y = y

     def getX(self):
         return self.x

     def getY(self):
         return self.y

     def drawStuff(entityList):
     #   pygame.draw.rect(screen, (255, 100, 0), pygame.Rect(55, 45, 10, 10))
         for x in range (0, entityList.__len__()):
             pygame.draw.rect(screen, (255, 100, 0), pygame.Rect(entityList[x].getX(),     entityList[x].getY(), 10, 10))
         pygame.display.flip()
         clock.tick(60)


class EntityManager(threading.Thread):

     def __init__(self):
         threading.Thread.__init__(self)

     def run(self):
         while True:
             entityList = generateEntities()
             drawStuff(entityList)

     def endJob(self):
         thread.exit()
         time.sleep(2)


def detect_collision(x,y):
    if x > 340:
       x -= 1
    if y > 240:
       y -= 1
    if y < 0:
       y += 1
    if x < 0:
       x += 1
    return x,y

def generateEntities():
    itemlist = []
    for x in range (0,4):
        x = random.randint(1,339)
        y = random.randint(1,239)
        entity = Entity(x,y)
        itemlist.append(entity)
    return itemlist

entityList = generateEntities()
a = EntityManager()
a.setDaemon(True)
a.start()

while not done:

    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                    done = True
                    pygame.quit()
                    sys.exit()

            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                    is_blue = not is_blue

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP]:
        y -= 1
        x,y = detect_collision(x, y)
    if pressed[pygame.K_DOWN]:
        y += 1
        x,y = detect_collision(x, y)
    if pressed[pygame.K_LEFT]:
        x -= 1
        x,y = detect_collision(x, y)
    if pressed[pygame.K_RIGHT]:
        x += 1
        x,y = detect_collision(x, y)

    screen.fill((0, 0, 0))
    if is_blue: color = (0, 128, 255)
    else: color = (255, 100, 0)
    pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60))
    pygame.display.flip()
    clock.tick(60)

Upvotes: 0

Views: 2775

Answers (2)

GLHF
GLHF

Reputation: 4054

clock.tick() actually means FPS (frames per second); instead of changing it, change another variable. Watch this tutorial, it might help you understand FPS.

Issues about smoothness are sometimes about the size of your object and how many pixels you are moving it when key events are True. Be careful about that; imagine you have a little box and you are trying to move it. If you move it with big steps, it seems 'laggy', but if you move it with little steps then it may look smoother. Hope you understand what I mean.

For your second question please check this documentation.

Upvotes: 1

user4080732
user4080732

Reputation: 157

Okay, I figured it out now. OMG, I feel such an idiot after this. But I forgive myself that I am also doing parallell learning of Python. I used to code in Java more.

Anyway: problem was that my drawStuff function drew things in function but when it left this function, it did nothing. Changes were made locally. I solved this problem by using global keyword. I made in my code changes that thread computes entity coordinates and then main loop does update. This leads me to another idea that maybe there should be some game info object which gets updated all the time and all rectangles info go to that game info object.

Anyway, soon I will edit this post code too to see what i did

Upvotes: 0

Related Questions