• 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()方法的使用,请参考Pygame中Sprite类的使用6-2_棉猴的博客-CSDN博客

    3 完整代码

    1. import pygame
    2. def load_image(name):
    3. image = pygame.image.load(name)
    4. image = image.convert()
    5. colorkey = image.get_at((0,0))
    6. image.set_colorkey(colorkey, pygame.RLEACCEL)
    7. return image, image.get_rect()
    8. class Mouse(pygame.sprite.Sprite):
    9. def __init__(self):
    10. pygame.sprite.Sprite.__init__(self)
    11. self.image, self.rect = load_image('qiu.png')
    12. def update(self):
    13. self.rect.topleft = pygame.mouse.get_pos()
    14. self.rect.move_ip((0,0))
    15. class Chimp(pygame.sprite.Sprite):
    16. def __init__(self):
    17. pygame.sprite.Sprite.__init__(self)
    18. self.image, self.rect = load_image('chimp.bmp')
    19. screen = pygame.display.get_surface()
    20. self.area = screen.get_rect()
    21. self.rect.topleft = 10, 10
    22. self.move = 9
    23. self.dizzy = 0
    24. def update(self):
    25. self._walk()
    26. def _walk(self):
    27. newpos = self.rect.move((self.move, 0))
    28. if not self.area.contains(newpos):
    29. if self.rect.left
    30. or self.rect.right>self.area.right:
    31. self.move = -self.move
    32. newpos = self.rect.move((self.move, 0))
    33. self.image = \
    34. pygame.transform.flip(self.image, 1, 0)
    35. self.rect = newpos
    36. pygame.init()
    37. screen = pygame.display.set_mode((1280, 480))
    38. pygame.mouse.set_visible(False)
    39. screen.fill((170, 238, 187))
    40. going = True
    41. mouse = Mouse()
    42. chimp = Chimp()
    43. allsprites = pygame.sprite.RenderPlain(mouse, chimp)
    44. clock = pygame.time.Clock()
    45. while going:
    46. clock.tick(60)
    47. screen.fill((170, 238, 187))
    48. for event in pygame.event.get():
    49. if event.type == pygame.QUIT:
    50. going = False
    51. allsprites.update()
    52. allsprites.draw(screen)
    53. pygame.display.flip()
    54. pygame.quit()

  • 相关阅读:
    深圳高级中学国际课程体系IBDP成绩
    协议僵化 or 协议僵化
    怎样将文字转换成语音?这两种转换方法方便有效
    SQLite的知名用户(二十九)
    基于晶体结构算法优化概率神经网络PNN的分类预测 - 附代码
    使用OpenCV计算两幅图像的协方差
    Linux基于多线程和任务队列实现生产消费模型
    【数据结构】红黑树实现详解
    Android使用AudioTrack播放WAV音频文件
    【Spring】Day19
  • 原文地址:https://blog.csdn.net/hou09tian/article/details/133934697