库库的斯蒂芬 发表于 2022-5-11 17:08:13

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



import pygame
import sys
from pygame.locals import *
from random import *

class Ball(pygame.sprite.Sprite):
    def __init__(self,image,position,speed):
      pygame.sprite.Sprite.__init__(self)
      self.image=pygame.image.load(image).convert_alpha()
      self.rect=self.image.get_rect()
      self.rect.left, self.rect.top=position
      self.speed=speed

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

def main():
    pygame.init()
    ball_img='ball.png'
    bg_img='bg.png'

    running=True

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

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

    balls=[]

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

    clock=pygame.time.Clock()
    while running:
      for event in pygame.event.get():
            if event.type ==QUIT:
                sys.exit()
      
      screen.blit(bg, (0,0))

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

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

if __name__ =="__main__":
    main()

Twilight6 发表于 2022-5-11 17:31:19


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

import pygame
import sys
from pygame.locals import *
from random import *


class Ball(pygame.sprite.Sprite):
    def __init__(self, image, position, speed):
      pygame.sprite.Sprite.__init__(self)
      self.image = pygame.image.load(image).convert_alpha()
      self.rect = self.image.get_rect()
      self.rect.left, self.rect.top = position
      self.speed = speed

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


def main():
    pygame.init()
    ball_img = 'ball.png'
    bg_img = 'bg.png'

    running = True

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

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

    balls = []

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

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

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

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

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


if __name__ == "__main__":
    main()

页: [1]
查看完整版本: 今天的第二贴,pygame写游戏,图片在背景上移动会留下拖痕是什么原因呢