- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个垂直线的图像,想创建两个。我希望它们在沿 x 轴移动时随机分开并缩小间隙(同时保持最小和最大距离 - 我正在等待先解决这个问题)。 Y 坐标不会移动。
有很多使用 pygame 创建更复杂的 Sprite 和游戏的例子,但我找不到一个足够简单的例子来确定我应该做什么,所以任何帮助都将不胜感激。此代码基于找到的一个简单示例 here.
这是我目前拥有的最基本的代码。
class Squeeze(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.img_load()
def update(self):
self.rect.x += random.randint(-1, 1)
if self.rect.x >= 1920:
self.rect.x += -1
if self.rect.x <= 0:
self.rect.x += 1
def img_load(self):
self.image = pygame.Surface([SCREEN_WIDTH, SCREEN_HEIGHT])
self.image.fill(BLACK)
self.rect = self.image.get_rect()
pygame.init()
SCREEN_WIDTH = 1920
SCREEN_HEIGHT = 1080
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
FPS = 60
wall = pygame.image.load('wall.png')
wall_list = pygame.sprite.Group()
BLACK = (0, 0, 0)
for i in range(2):
wall = Squeeze()
wall.rect.x = random.randrange(SCREEN_WIDTH)
wall.rect.y = random.randrange(SCREEN_HEIGHT)
wall_list.add(wall)
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(BLACK)
wall_list.update()
wall_list.draw(screen)
clock.tick(60)
pygame.display.flip()
pygame.quit()
我对 pygame.sprite.Group() 交易感到困惑。如何让两个图像分别加载和操作?也欢迎任何其他建议。
最佳答案
基本上“ Sprite ”是游戏中可见的游戏对象。游戏 pygame.sprite.Sprite
类被称为所有这些对象的“基类”。
每次创建 Squeeze
类时,您都派生形成 Pygames 内置 Sprite 基类:
class Squeeze(pygame.sprite.Sprite):
def __init__(self):
...
pygame.sprite.Sprite
类的每个对象(或实例)都包含
.update()
方法与.image
和.rect
属性。作为documentation状态,“* ...派生类将要覆盖 Sprite.update()
并分配一个 Sprite.image
和 Sprite.rect
属性.*"
您通过在 Squeeze
类的 __init__()
方法中调用您的 img_load()
方法来做到这一点。
现在的问题是您的 .rect
被指定为与您的屏幕相同的尺寸 (self.image = pygame.Surface([SCREEN_WIDTH, SCREEN_HEIGHT])
) .image
属性与您的显示屏颜色相同,因此您看不到它。
尝试将您的img_load()
方法修改为
def img_load(self, img):
self.image = img
self.rect = self.image.get_rect()
如您所见,修改后的 img_load()
方法需要一个图像对象——它被传递给 img
参数。所以你还需要改变你的 __init__()
方法:
def __init__(self, img):
pygame.sprite.Sprite.__init__(self)
self.img_load(img)
现在实例化(例如创建类的对象)Squeeze
对象时,您需要将图像(在我们的例子中为 wall_img
)传递给构造函数:
#... main programm
wall_img = pygame.image.load('wall.png') #load an image
wall_list = pygame.sprite.Group() #create a sprite group
BLACK = (0, 0, 0)
for i in range(2):
wall = Squeeze(wall_img) #create a sprite
wall.rect.x = random.randrange(SCREEN_WIDTH)
wall.rect.y = random.randrange(SCREEN_HEIGHT)
wall_list.add(wall) #add the sprite to the sprite group
您使用的第二个内置类是 pygame.sprite.Group
类 -- 一个容器,可以容纳和管理(修改)它包含的 Sprite 。
调用 Sprite 组实例的.update()
方法,Pygame 自动调用该 Sprite 组中所有 Sprite 的update()
方法团体。这意味着您可以通过实现其 update()
方法来控制 Sprite 的行为,就像您所做的那样:
#update method of the Squeeze class
def update(self):
#any logic which changes sprite
self.rect.x += random.randint(-1, 1)
if self.rect.x >= 1920:
self.rect.x += -1
if self.rect.x <= 0:
self.rect.x += 1
通过调用
wall_list.draw(screen)
你的恶意将被 blitted 到 screen
表面上,使用源表面的 .image
属性和位置的 .rect
。
如果您现在不想拥有不同的图像,您只需将其他图像(或表面)传递给 Squeeze
构造函数:
#... main programm
wall_imgs = [pygame.image.load('wall.png'),
pygame.image.load('other_wall.png')] #load images in list
wall_list = pygame.sprite.Group() #create a sprite group
BLACK = (0, 0, 0)
for i in range(2):
wall = Squeeze(wall_imgs[i]) #create a sprite
wall.rect.x = random.randrange(SCREEN_WIDTH)
wall.rect.y = random.randrange(SCREEN_HEIGHT)
wall_list.add(wall) #add the sprite to the sprite group
希望对您有所帮助:)
关于python - 在pygame中创建两个运动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33007721/
这是否可以检测到手机何时像图片上那样移动?这个在android中叫什么名字? 是否有处理此类事情的事件?一年前,我看到一个带指南针的应用程序,它可以实时运行。 谢谢! 最佳答案 我希望这段代码能有所帮
我正在为这个而撕扯我的头发。出于某种奇怪的原因,我找不到/想不出如何在 SFML 和/或 SDL 中移动 Sprite 。我看过的这两个库的教程对此一无所知;所以我认为它更像是 C++ 的东西而不是库
所以我最近一直在研究 DirectX11,但我对它还是很陌生。我现在正在尝试通过翻译来移动一些东西,这就是我所拥有的。我一直在阅读 Frank D Luna 关于 DirectX11 的书,他提供了一
我一直在尝试为绘图元素制作动画,但没有成功。我可以对导入的图像进行动画处理,但是当我尝试对 pygame 生成的绘图进行动画处理时,它们仍然是静态的。 编辑:“动画”是指“移动”。就像使圆在 x 和
好吧,我已经尝试 Java 几个星期了,遵循类和在线教程。我做了一个简单的游戏,其中方 block 落向屏幕底部,而玩家控制一个小球,仅在 x 轴上移动并尝试避开它们。 我遇到的问题是方 block
我的 python 代码遇到一些问题,我正在制作蛇的一个版本,我的问题涉及蛇本身的运动。我已经得到了工作正常的方向,我只需要做到这一点,以便蛇继续沿着通过按键告诉它的方向移动,我还需要使它成为一个 b
那是我的代码。 -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { [[NSNotificationC
我正在构建这个用于慢跑的 Android 应用程序,它应该测量用户从某个时间点开始的距离。 我对使用 GPS 选项(经度、纬度)不感兴趣,所以如果没有 GPS 选项并且只使用 android 传感器,
什么是自律?网上有这样一个回答:“最健康的自律是早睡,最实用的自律是运动,最丰盈内心的自律是读书。”早睡养神,运动养身,读书养脑。坚持把这三件事做好,你就已经超越了很多人。 1 每天早点睡 你有没有过
考虑下面这行 Lisp 代码: (some-function 7 8 | 9) ;; some comment. note the extra indentation 该点位于“8”和
在 Vim 中,如何移动到 xml 文件中的父/表亲标签?我正在寻找类似的东西: vatat " create a selection for second parent tag with all c
用 Dart 做这样的 Canvas 运动的最佳方法是什么? http://jsfiddle.net/loktar/dMYvG/ 我正在尝试使 Canvas 运动平稳,并想看看Dart可以做什么。 还
我试图让一个物体在固定的时间段内沿着圆形路径移动。 这个应用程序是一个“平滑运动”时钟。 因此,我不想每次 .getSeconds() 更新时将位置移动到固定坐标,而是想使用 ( .getSecond
我正在尝试创建一个简单的动画,其中一系列气泡围绕中心点旋转。我有一个动画版本,其中气泡在开始旋转之前从中心点扩散,效果很好,但是一旦我单击其中一个图像(引发动画),屏幕就会卡住一会儿,然后气泡出现在他
不久前我开始学习java作为一种爱好,因为我想制作一个小游戏。我学习了 Java 基础知识,并决定尝试解决游戏开发问题。我的 JFrame 和一切都很好,从技术上讲我没有错误,但我的小矩形家伙不会在屏
我在制作台球游戏时遇到问题,当我模拟击球时,我需要球使用react,程序是这样工作的,您单击击球的方向和力量,然后单击开始, go按钮位于创建标签的GUI类中,该按钮调用我的主类中的一个方法来接收参数
我以前在 2d 项目中使用过类似的东西来移动并且它总是有效。我现在正在使用它,它为我提供了一些输出的正确角度和错误的角度。我认为我的触发器中有一些错误,我弄错了。不过,我已经检查了大约一百万次。 Pr
我的 OpenGL 应用程序有问题,您可以在 this gif 中清楚地看到.基本上我想朝光标指向的方向移动,但这并没有发生,而是“前进”方向保持不变。 例如,如果我转身 180° 并按“w”向前走,
因此,我再次开始使用 C++ 编程并尝试使用 OpenGL。目前我有一个基本的应用程序,我只想用键移动相机。我读了很多关于这个的文章,但我仍然对运动有问题,我猜是因为即使只有一点点,但它们与我的程序不
我在 Android OpenGL 中有一个 Sprite 。这个 Sprite (一只小甲虫)总是向前移动,我使用: sprite.setPosition(posX, posY); 现在我有一个旋转
我是一名优秀的程序员,十分优秀!