鱼C论坛

 找回密码
 立即注册
查看: 1483|回复: 1

[已解决]今天的第二贴,pygame写游戏,图片在背景上移动会留下拖痕是什么原因呢

[复制链接]
发表于 2022-5-11 17:08:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
微信截图_20220511170635.png

  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *

  5. class Ball(pygame.sprite.Sprite):
  6.     def __init__(self,image,position,speed):
  7.         pygame.sprite.Sprite.__init__(self)
  8.         self.image=pygame.image.load(image).convert_alpha()
  9.         self.rect=self.image.get_rect()
  10.         self.rect.left, self.rect.top=position
  11.         self.speed=speed

  12.     def move(self):
  13.         self.rect=self.rect.move(self.speed)

  14. def main():
  15.     pygame.init()
  16.     ball_img='ball.png'
  17.     bg_img='bg.png'

  18.     running=True

  19.     bg_size=width,height=1357,637
  20.     screen=pygame.display.set_mode(bg_size)
  21.     pygame.display.set_caption('play the ball')

  22.     bg=pygame.image.load(bg_img).convert_alpha()

  23.     balls=[]

  24.     for i in range(3):
  25.         position=randint(0, width-100),randint(0,height-100)
  26.         speed=randint(-10, 10),randint(-10, 10)
  27.         ball=Ball(ball_img, position, speed)
  28.         balls.append(ball)

  29.     clock=pygame.time.Clock()
  30.     while running:
  31.         for event in pygame.event.get():
  32.             if event.type ==QUIT:
  33.                 sys.exit()
  34.         
  35.         screen.blit(bg, (0,0))

  36.         for each in balls:
  37.             each.image=pygame.transform.smoothscale(each.image, (each.rect.width*0.5,each.rect.height*0.5))
  38.             each.move()
  39.             screen.blit(each.image, each.rect)

  40.         pygame.display.flip()
  41.         clock.tick(30)

  42. if __name__ =="__main__":
  43.     main()
复制代码
最佳答案
2022-5-11 17:31:19

将你的代码 28 行改成     bg = pygame.image.load(bg_img).convert(),参考代码:

  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *


  5. class Ball(pygame.sprite.Sprite):
  6.     def __init__(self, image, position, speed):
  7.         pygame.sprite.Sprite.__init__(self)
  8.         self.image = pygame.image.load(image).convert_alpha()
  9.         self.rect = self.image.get_rect()
  10.         self.rect.left, self.rect.top = position
  11.         self.speed = speed

  12.     def move(self):
  13.         self.rect = self.rect.move(self.speed)


  14. def main():
  15.     pygame.init()
  16.     ball_img = 'ball.png'
  17.     bg_img = 'bg.png'

  18.     running = True

  19.     bg_size = width, height = 1357, 637
  20.     screen = pygame.display.set_mode(bg_size)
  21.     pygame.display.set_caption('play the ball')

  22.     bg = pygame.image.load(bg_img).convert()

  23.     balls = []

  24.     for i in range(3):
  25.         position = randint(0, width - 100), randint(0, height - 100)
  26.         speed = randint(-10, 10), randint(-10, 10)
  27.         ball = Ball(ball_img, position, speed)
  28.         balls.append(ball)

  29.     clock = pygame.time.Clock()
  30.     while running:
  31.         for event in pygame.event.get():
  32.             if event.type == QUIT:
  33.                 sys.exit()

  34.         screen.blit(bg, (0, 0))

  35.         for each in balls:
  36.             each.image = pygame.transform.smoothscale(each.image, (each.rect.width * 0.5, each.rect.height * 0.5))
  37.             each.move()
  38.             screen.blit(each.image, each.rect)

  39.         pygame.display.flip()
  40.         clock.tick(30)


  41. if __name__ == "__main__":
  42.     main()
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-11 17:31:19 | 显示全部楼层    本楼为最佳答案   

将你的代码 28 行改成     bg = pygame.image.load(bg_img).convert(),参考代码:

  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *


  5. class Ball(pygame.sprite.Sprite):
  6.     def __init__(self, image, position, speed):
  7.         pygame.sprite.Sprite.__init__(self)
  8.         self.image = pygame.image.load(image).convert_alpha()
  9.         self.rect = self.image.get_rect()
  10.         self.rect.left, self.rect.top = position
  11.         self.speed = speed

  12.     def move(self):
  13.         self.rect = self.rect.move(self.speed)


  14. def main():
  15.     pygame.init()
  16.     ball_img = 'ball.png'
  17.     bg_img = 'bg.png'

  18.     running = True

  19.     bg_size = width, height = 1357, 637
  20.     screen = pygame.display.set_mode(bg_size)
  21.     pygame.display.set_caption('play the ball')

  22.     bg = pygame.image.load(bg_img).convert()

  23.     balls = []

  24.     for i in range(3):
  25.         position = randint(0, width - 100), randint(0, height - 100)
  26.         speed = randint(-10, 10), randint(-10, 10)
  27.         ball = Ball(ball_img, position, speed)
  28.         balls.append(ball)

  29.     clock = pygame.time.Clock()
  30.     while running:
  31.         for event in pygame.event.get():
  32.             if event.type == QUIT:
  33.                 sys.exit()

  34.         screen.blit(bg, (0, 0))

  35.         for each in balls:
  36.             each.image = pygame.transform.smoothscale(each.image, (each.rect.width * 0.5, each.rect.height * 0.5))
  37.             each.move()
  38.             screen.blit(each.image, each.rect)

  39.         pygame.display.flip()
  40.         clock.tick(30)


  41. if __name__ == "__main__":
  42.     main()
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-29 05:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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