鱼C论坛

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

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

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

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

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

x
微信截图_20220511170635.png
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()
最佳答案
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()

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

使用道具 举报

发表于 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()

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-18 10:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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