Pygame中实现图片的移动

编程入门 行业动态 更新时间:2024-10-11 07:33:32

Pygame中实现<a href=https://www.elefans.com/category/jswz/34/1770705.html style=图片的移动"/>

Pygame中实现图片的移动

在《Pygame中将鼠标形状设置为图片2-1》和《Pygame中将鼠标形状设置为图片2-2》中提到将鼠标设置为指定图片。接下来在该文章涉及到的代码基础之上,实现图片的移动,效果如图1所示。

图1 图片移动效果

从图1中可以看出,导入一个大猩猩的图片,让大猩猩图片可以在创建的屏幕上左右移动。

1 猩猩类的创建

创建一个表示猩猩图片的类Chimp,该类也是pygame.sprite.Sprite类的派生类。

1.1 Chimp类的__init__()方法

Chimp类的__init__()方法的代码如图2所示。

图2 Chimp类__init__()代码

在Chimp类的__init__()方法中,首先调用其父类的__init__();之后调用自定义函数load_image()方法导入大猩猩图片,并将图片保存到属性image中,而rect属性表示图片的位置;第22-23行代码获取程序窗口的大小,并将其保存到area属性中;第24行设置图片的位置,rect.topleft表示图片左上角所在的图标;第25行move属性表示图片移动的速度。

相关链接1 load_image()方法的详细说明,请参考Pygame中Sprite类的使用6-1-CSDN博客

1.2 Chimp类的_walk()方法

Chimp类的_walk()方法用于控制图片的移动,其代码如图3所示。

图3 Chimp类_walk()代码

其中,第31行代码通过pygame.Rect类的实例rect调用move()方法开始移动,该方法的参数是一个元组,元组中的第一个元素表示在横坐标移动的速度,第二个元素表示纵坐标移动的速度,newpos保存了图片移动后的位置;当图片向左或者向右移动到了窗口边缘后,需要改变移动方向。因此第32行中的代码通过pygame.Rect类的contains()方法判断图片移动后的位置newpos是否在窗口area中,如果不在窗口中,则contains()返回值是False;此时第34-35行代码,再次判断图片是否左移或者右移出了窗口,如果是以上情况,则通过36行所示代码,将速度改为负值,也就是更改了移动方向;37行代码表示按照更改后的新方向继续移动图片,因为图片移动的方向发生了逆转,所以也需要对图片进行逆转,第38-39行代码调用pygame.transform类的flip()方法,将图片进行逆转;flip()方法表示第一个参数要逆转的图片,第二个表示是否在水平方向逆转,1表示逆转,0表示不逆转,第三个参数表示是否在垂直方向逆转;最后,用图片的新位置newpos来更新图片的位置属性rect。

1.3 Chimp类的update()方法

Chimp类的update()方法用于更新图片,该方法的代码如图4所示。

图4 Chimp类update()代码

从图4可以看出,在update()方法调用了Chimp类的_walk()方法,改变了图片的位置,也就更新了图片。

2 猩猩类的使用

在主程序中首先创建Chimp()类的实例,如图5中49行代码所示;之后将chimp加入到sprite的Group中;最后定义pygame.time.Clock()类的实例clock,用于控制图片移动速度。

图5 使用猩猩类的代码1

相关链接2 RenderPlain()方法的使用,请参考Pygame中Sprite类的使用6-3_棉猴的博客-CSDN博客

为了控制图片移动速度不能过快,在while going循环中,加入clock.tick()代码控制移动速度,代码如图6中54行所示。

图6 使用猩猩类的代码2

相关链接3 pygame.time.Clock类的tick()方法的使用,请参考

3 完整代码

import pygamedef load_image(name):image = pygame.image.load(name)image = image.convert()colorkey = image.get_at((0,0))image.set_colorkey(colorkey, pygame.RLEACCEL)return image, image.get_rect()class Mouse(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.image, self.rect = load_image('qiu.png')def update(self):self.rect.topleft = pygame.mouse.get_pos()self.rect.move_ip((0,0))class Chimp(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.image, self.rect = load_image('chimp.bmp')screen = pygame.display.get_surface()self.area = screen.get_rect()self.rect.topleft = 10, 10self.move = 9self.dizzy = 0def update(self):self._walk()def _walk(self):newpos = self.rect.move((self.move, 0))if not self.area.contains(newpos):if self.rect.left<self.area.left  \or self.rect.right>self.area.right:self.move = -self.movenewpos = self.rect.move((self.move, 0))self.image =  \pygame.transform.flip(self.image, 1, 0)self.rect = newpospygame.init()
screen = pygame.display.set_mode((1280, 480))
pygame.mouse.set_visible(False)
screen.fill((170, 238, 187))
going = True
mouse = Mouse()
chimp = Chimp()
allsprites = pygame.sprite.RenderPlain(mouse, chimp)
clock = pygame.time.Clock()while going:clock.tick(60)screen.fill((170, 238, 187))for event in pygame.event.get():if event.type == pygame.QUIT:going = Falseallsprites.update()allsprites.draw(screen)pygame.display.flip()
pygame.quit()

更多推荐

Pygame中实现图片的移动

本文发布于:2023-12-07 09:14:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1670796.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图片   Pygame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!