在pygame中闪烁矩形

时间:2015-01-01 20:06:28

标签: python

我正在尝试学习pyGame。我遇到了一个问题。我有一个矩形,可以用箭头按钮移动。然后我创建了另一个生成较小矩形的线程,可以拾取。但是当我运行游戏时,生成的小矩形会闪烁太多。我怎样才能使它们稳定?我想我不太了解时间概念。有人可以解释我等吗

我的代码:

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)

2 个答案:

答案 0 :(得分:1)

clock.tick()实际上意味着FPS(每秒帧数);而不是改变它,改变另一个变量。 观看this tutorial,它可能有助于您了解FPS。

关于平滑度的问题有时与对象的大小有关,以及当关键事件为True时您移动它的像素数。小心一点;想象你有一个小盒子,你正试图移动它。如果你用大步移动它,它似乎“滞后”,但如果你用很少的步骤移动它,那么它可能看起来更平滑。希望你明白我的意思。

关于第二个问题,请查看this documentation

答案 1 :(得分:0)

好的,我现在明白了。天哪,我觉得这样的白痴。但我原谅自己,我也在做Python的并行学习。我以前用Java编写代码。

无论如何:问题是我的drawStuff函数在函数中引入了东西,但是当它离开这个函数时,它什么也没做。变更是在当地进行的。我通过使用global关键字解决了这个问题。我在代码中做了更改,线程计算实体坐标,然后主循环更新。这引出了另一个想法,也许应该有一些游戏信息对象一直在更新,所有矩形信息都会转到那个游戏信息对象。

无论如何,我很快就会编辑这个邮政编码,看看我做了什么

相关问题