鱼C论坛

 找回密码
 立即注册
查看: 1075|回复: 4

[已解决]用pygame定义的精灵类移动图片为何这个结果?

[复制链接]
发表于 2020-5-19 20:59:35 | 显示全部楼层 |阅读模式
10鱼币
先看结果:
QQ图片20200519205641.png

大家应该看明白了吧,被移动的图片拖拖拖的很长了

再来看看代码。

pygame.init()

myscreen = pygame.display.set_mode((800,600))

class horse(pygame.sprite.Sprite):
        def __init__(self):
                pygame.sprite.Sprite.__init__(self)
                self.image = pygame.image.load("girl.png")#被移动的图片
                self.rect = self.image.get_rect()
                self.rect.left = 8
                self.rect.top = 10
                speed = [4,0]
                self.speed = speed
        def move(self):
                self.rect = self.rect.move(self.speed)

bgimage = pygame.image.load("girl.jpg")#背景图片

bgposition = bgimage.get_rect()

myscreen.blit(bgimage,bgposition)

horse1 = horse()

horseposition = horse1.rect

while True:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        exit()
        myscreen.blit(horse1.image,horseposition)
        horse1.move()
        horseposition = horse1.rect
        pygame.display.update()
        #pygame.time.Clock().tick(3)
最佳答案
2020-5-19 20:59:36
要循环里画个背景

最佳答案

查看完整内容

要循环里画个背景
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-19 20:59:36 | 显示全部楼层    本楼为最佳答案   
要循环里画个背景
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-19 21:10:01 | 显示全部楼层
想实现什么效果呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-19 21:15:56 | 显示全部楼层
wuqramy 发表于 2020-5-19 21:10
想实现什么效果呢

就被移动的图片正常右移啊,显然图片里的结果不是我要的,拖出来的痕迹把背景图片都覆盖了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-19 21:25:56 | 显示全部楼层
dequantianhe 发表于 2020-5-19 21:15
就被移动的图片正常右移啊,显然图片里的结果不是我要的,拖出来的痕迹把背景图片都覆盖了。


这样试试
  1. import pygame
  2. from sys import *
  3. pygame.init()

  4. myscreen = pygame.display.set_mode((800,600))

  5. class horse(pygame.sprite.Sprite):
  6.         def __init__(self):
  7.                 pygame.sprite.Sprite.__init__(self)
  8.                 self.image = pygame.image.load("girl.png")#被移动的图片
  9.                 self.rect = self.image.get_rect()
  10.                 self.rect.left = 8
  11.                 self.rect.top = 10
  12.                 speed = [4,0]
  13.                 self.speed = speed
  14.         def move(self):
  15.                 self.rect = self.rect.move(self.speed)

  16. bgimage = pygame.image.load("girl.png")#背景图片

  17. bgposition = bgimage.get_rect()

  18. myscreen.blit(bgimage,bgposition)

  19. horse1 = horse()

  20. horseposition = horse1.rect

  21. while True:
  22.         for event in pygame.event.get():
  23.                 if event.type == pygame.QUIT:
  24.                         exit()
  25.         myscreen.blit(bgimage,bgposition)
  26.         myscreen.blit(horse1.image,horseposition)
  27.         horse1.move()
  28.         horseposition = horse1.rect
  29.         pygame.display.flip()
  30.         pygame.time.Clock().tick(3)
复制代码

评分

参与人数 1鱼币 +5 收起 理由
dequantianhe + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 16:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表