马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import pygame
import sys
from pygame.locals import *
from random import *
class Ball(pygame.sprite.Sprite):
def __init__(self, image, pos, speed, bg_size):
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 = pos
self.speed = speed
self.bg_width, self.bg_height = bg_size[0], bg_size[1]
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.right < 0:
self.rect.left = self.bg_width # 穿越
elif self.rect.left > self.bg_width:
self.rect.right = 0
elif self.rect.bottom < 0:
self.rect.top = self.bg_height
elif self.rect.top > self.bg_height:
self.rect.bottom = 0
def main():
pygame.init()
ball_image = "gray_ball.png"
bg_image = "background.png"
running = True
bg_size = width, height = 1024, 681
radius = 100
clock = pygame.time.Clock()
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("Play the ball")
background = pygame.image.load(bg_image).convert_alpha()
balls = []
for i in range(5):
pos = randint(0, width - radius), randint(0, height - radius)
speed = [randint(-10, 10), randint(-10, 10)]
ball = Ball(ball_image, pos, speed, bg_size)
balls.append(ball)
while running:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
screen.blit(background, (0, 0))
for ball in balls:
ball.move()
screen.blit(ball.image, ball.rect)
pygame.display.flip()
clock.tick(30)
if __name__ == "__main__":
main()
在学习小甲鱼的python入门课程,ball图片已经使用convert_alpha转换,但出来的效果仍然有黑色背景边框。
请大家帮忙看看是哪里出了问题?
|