0
我是Python和Pygame的新手。我想在pygame中有一个屏幕,并且独立地移动相同图像的多个副本。我试图把它写成一个类,然后在while
循环内调用它的实例,但它不起作用。有人可以展示我如何使用class
基本上做这样的事情?Pygame独立移动图像在屏幕上
我是Python和Pygame的新手。我想在pygame中有一个屏幕,并且独立地移动相同图像的多个副本。我试图把它写成一个类,然后在while
循环内调用它的实例,但它不起作用。有人可以展示我如何使用class
基本上做这样的事情?Pygame独立移动图像在屏幕上
我试图把一切都简单
例子:
import pygame
pygame.init()
WHITE = (255,255,255)
BLUE = (0,0,255)
window_size = (400,400)
screen = pygame.display.set_mode(window_size)
clock = pygame.time.Clock()
class Image():
def __init__(self,x,y,xd,yd):
self.image = pygame.Surface((40,40))
self.image.fill(BLUE)
self.x = x
self.y = y
self.x_delta = xd
self.y_delta = yd
def update(self):
if 0 <= self.x + self.x_delta <= 360:
self.x += self.x_delta
else:
self.x_delta *= -1
if 0 <= self.y + self.y_delta <= 360:
self.y += self.y_delta
else:
self.y_delta *= -1
screen.blit(self.image,(self.x,self.y))
list_of_images = []
list_of_images.append(Image(40,80,2,0))
list_of_images.append(Image(160,240,0,-2))
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(WHITE)
for image in list_of_images:
image.update()
pygame.display.update()
clock.tick(30)
pygame.quit()
每个图像都可以单独从列表中通过简单地改变Image.x/y以什么叫搬到你想
好!谢谢。 – amirteymuri
示例 - 使用蝴蝶班的许多蝴蝶:http://pastebin.com/p2KAfsHH。它使用pygame.Surface,但它可以是图像。 – furas
谢谢你的例子。 '(event.pos)'在你的'event_handle'定义中做了什么?我没有在名为'pos'的pygame中找到任何东西,这是从哪里来的? – amirteymuri
'事件'来自'事件'循环。不同的事件有不同的领域。鼠标事件有'event.pos' - 它是鼠标的位置。查看http://www.pygame.org/docs/ref/event.html上的所有字段(请参阅带黄色背景的列表) – furas