【发布时间】:2015-02-28 16:33:12
【问题描述】:
我有一个 pygame 程序的经典示例:
import pygame
import random
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
class Block(pygame.sprite.Sprite):
"""
This class represents the ball.
It derives from the "Sprite" class in Pygame.
"""
def __init__(self, color, width, height):
""" Constructor. Pass in the color of the block,
and its x and y position. """
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# image.
# Update the position of this object by setting the values
# of rect.x and rect.y
self.rect = self.image.get_rect()
# Initialize Pygame
pygame.init()
# Set the height and width of the screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
# This is a list of 'sprites.' Each block in the program is
# added to this list. The list is managed by a class called 'Group.'
block_list = pygame.sprite.Group()
# This is a list of every sprite.
# All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()
for i in range(100):
# This represents a block
block = Block(BLACK, 20, 15)
# Set a random location for the block
block.rect.x = random.randrange(screen_width)
block.rect.y = random.randrange(screen_height)
# Add the block to the list of objects
block_list.add(block)
all_sprites_list.add(block)
# Create a RED player block
player = Block(RED, 20, 15)
all_sprites_list.add(player)
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
score = 0
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Clear the screen
screen.fill(WHITE)
# Get the current mouse position. This returns the position
# as a list of two numbers.
pos = pygame.mouse.get_pos()
# Fetch the x and y out of the list,
# just like we'd fetch letters out of a string.
# Set the player object to the mouse location
player.rect.x = pos[0]
player.rect.y = pos[1]
# See if the player block has collided with anything.
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
# Check the list of collisions.
for block in blocks_hit_list:
score += 1
print(score)
# Draw all the spites
for block in all_sprites_list:
if block.rect.y + 1 > screen_height:
block.rect.y = 0
block.rect.y = block.rect.y + 1
all_sprites_list.draw(screen)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 60 frames per second
clock.tick(120)
pygame.quit()
好吧,我注意到当我移动鼠标时,其余的块移动得更慢(比如冻结了几分之一秒),但是当它在一个地方时,一切都在顺利移动。玩家精灵在所有精灵的列表中,并且没有计算玩家的位置 - 只获取鼠标的坐标。在我看来,一直获取 (100,100) 和获取其他鼠标坐标之间应该没有区别。
有人能解释一下为什么鼠标(玩家的精灵)的运动会影响其余的精灵吗? 谢谢!
【问题讨论】:
-
可能是
print语句。尝试使用注释掉的print(score)和clock.tick(200)运行您的游戏。我注意到打印到控制台而不是在游戏屏幕上显示通常会导致延迟。 -
我刚刚尝试过 - 它不是来自打印语句 :( 如果我增加时钟。tick 滞后变得更加明显。
-
我无法重现您的观察结果。事实上,随着打印被注释掉并增加
clock.tick,我得到了一个更流畅和无延迟的游戏。 ;对我来说,鼠标是静止的还是移动的都没有关系。 -
如果我注释掉
print,则完全没有。print虽然有一些滞后,但随着剩余块数的减少,它会逐渐减少。但是,如果鼠标在任何一种情况下是静止的或移动的,则延迟没有区别。除了 4GB 内存之外,我的规格是相同的。 -
好吧,原来我只需要重新启动计算机。现在一切都很顺利。谢谢! :)