这是我第一次使用堆栈溢出,我希望你们中的一些人能帮助我解决这个问题。
我正在设计一个心理物理学实验,它需要随机显示模式,如下所示:
https://www.dropbox.com/s/n09vkvbqxagt8hd/dot%20pairs.png?dl=0
但是,我仍然需要指定几个剩余的参数,但我不知道如何实现:
我希望有人能尽快帮助我在1和2下添加这两个参数!
谢谢你!
我找到、修改并用于生成这里给出的插图的代码是(有9幅图像,每个图像由点对组成,命名为"stim"*.png):
import random
import pygame
WHITE = (255,255,255)
BLACK = (0 ,0 ,0 )
class Player():
def __init__(self, image, x=0, y=0):
self.image = pygame.image.load(image) #.convert()
#self.image.set_colorkey(WHITE)
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.centery = y
#------------
def draw(self, screen):
screen.blit(self.image, self.rect)
#------------
def update(self):
# here change randomly positon
self.rect.topleft = random.randint(60,220+1), random.randint( 0, 475+1)
class Game():
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((800,600))
self.background = pygame.image.load("background.jpg").convert()
self.multi_players = []
# create stimuli
for i in range(1,10):
player = Player("stim"+str(i)+".png")
player.update() # set random position on start
self.multi_players.append(player)
#------------
def run(self):
clock = pygame.time.Clock()
RUNNING = True
while RUNNING:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUNNING = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
RUNNING = False
# changes position when key is pressed
for player in self.multi_players:
player.update()
# --- updates ----
# place for updates
# --- draws ---
self.screen.fill(BLACK)
self.screen.blit(self.background, self.background.get_rect())
for player in self.multi_players:
player.draw(self.screen)
pygame.display.flip()
# --- FPS ---
clock.tick(20)
# --- quit ---
pygame.quit()
Game().run()发布于 2014-09-17 13:04:35
由于这是我第一次接触到游戏库,这可能是有一个更好的解决方案,但经过一项小的研究后,我认为最好的方法是使用检测像素完美碰撞在.png masks.That is,吡咯烷酮允许检查按颜色重叠的玩家通过定义面具。我修改了您的代码,现在它似乎运行得很好:
import random
import pygame
WHITE = (255,255,255)
BLACK = (0 ,0 ,0 )
RED = (255,0,0)
PLAYERS = []
class Player():
def __init__(self, image, x=0, y=0):
self.image = pygame.image.load(image).convert_alpha() #.convert()
#self.image.set_colorkey(WHITE)
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.centery = y
#self.image.set_colorkey(BLACK)
#------------
def draw(self, screen):
screen.blit(self.image, self.rect)
#------------
def update(self):
#randomly rotate
angle = random.randint(0,360)
#self.image = pygame.transform.rotate(self.image, angle)
self.image = pygame.transform.rotozoom(self.image, angle, 1)
# here change randomly positon
self.rect.topleft = random.randint(60,220+1), random.randint( 0, 475+1)
searchAgain = 0
while (searchAgain < 1):
self.rect.topleft = random.randint(60,220+1), random.randint( 0, 475+1)
searchAgain = 1
for p in PLAYERS:
# creating masks for the images
myImage_mask = pygame.mask.from_surface(self.image)
myOtherImage_mask = pygame.mask.from_surface(p.image)
# this is where the images are
myImage_rect = self.rect
myOtherImage_rect = p.rect
# this is where we check for pixel perfect collision
# observe the order mask variables are used in calculating offset and in overlap method
offset_x, offset_y = (myOtherImage_rect.left - myImage_rect.left), (myOtherImage_rect.top - myImage_rect.top)
print (offset_x, offset_y )
if (myImage_mask.overlap(myOtherImage_mask, (offset_x, offset_y)) != None):
print 'Collision Detected!'
#if we detect collision of players stay in the loop so that we get another pair of coordinates for the new player
searchAgain = 0
else:
print 'None'
class Game():
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((800,600))
self.background = pygame.image.load("background.jpg").convert()
self.multi_players = []
# create stimuli
for i in range(1,10):
player = Player("stim"+str(i)+".png")
#player = Player("stim6.gif")
player.update() # set random position on start
PLAYERS.append(player)
self.multi_players.append(player)
#------------
def run(self):
clock = pygame.time.Clock()
RUNNING = True
while RUNNING:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUNNING = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
RUNNING = False
# changes position when key is pressed
for player in self.multi_players:
player.update()
# --- updates ----
# place for updates
# --- draws ---
self.screen.fill(WHITE)
self.screen.blit(self.background, self.background.get_rect())
for player in self.multi_players:
player.draw(self.screen)
pygame.display.flip()
# --- FPS ---
clock.tick(20)
# --- quit ---
pygame.quit()
Game().run()大多数更改都是在Player类中的update()函数中完成的,但总的来说,我所做的是:
至于对,我不太明白你是否需要创建对相反的点,但无论如何,我希望你将有足够的继续我在这里张贴的东西。您可以在这里获得完整的示例:关于我的dropbox链接
希望它能帮上忙,向你问好,加森。
另外,当使用透明的PNG文件以避免在旋转非平方png图像后出现黑色背景的问题时,只需对代码中的self.image.set_colorkey(黑色)行进行注释。我是这样说的,出于个人的理由,在使用PNG时,您似乎不需要通过定义颜色键来注意透明度。
发布于 2014-09-17 07:36:31
我现在不能用pygame测试这段代码,因为它不是安装在这台计算机上,而是为了提供理论上的建议:
1)避免对象重叠。您应该维护已经放置的项的列表,并在生成随机位置时验证它们的边界框是否重叠。
一个例子就是
在__init__方法中添加一个self.surfaces = []
然后在绘制对象时,将它们添加到self.surfaces中。每次生成新的随机对象时,请如下所示:
def test_overlapping_with_other_surfaces(self, myRandomlyPlacedImage):
""" Tests if the randomly placed image overlap with an already existing image. """
for surface in self.surface:
if surface.get_rect.colliderect(myRandomlyPlacedImage.get_rect):
return False
return True如果对此函数的调用返回false,则将图像随机移动到另一个位置并再次测试。
2)方向的随机性似乎很简单。事实上,请参阅正式文档http://www.pygame.org/docs/ref/transform.html#pygame.transform.rotate
def randomly_rotate_image(self, image):
""" Returns the image randomly rotated. """
angle = randint(0,360)
return pygame.transform.rotate(image, angle)利用self.randomly_rotate_image(image).clip()而不是image.clip()
希望能帮上忙。
亚瑟。
https://stackoverflow.com/questions/25883818
复制相似问题