|

楼主 |
发表于 2022-7-23 11:29:18
|
显示全部楼层
哦哦哦,对哦,我忘了发代码
import pygame
import sys
from pygame.locals import *
from random import *
class Ball(pygame.sprite.Sprite):
def __init__(self, image, position, speed):
super(Ball, self).__init__() # 1、使用super函数继承父类,是否可直接写成super().__init__()???
# 2、下面四行代码是定义类还是类对象还是实例化对象的相关属性呢?
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 main():
pygame.init()
ball_image = 'gray_ball.png'
bg_image = 'background.png'
running = True # 3、作用???
bg_size = width, height = 1024, 681
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption('Play the ball - FishC Demo')
background = pygame.image.load(bg_image).convert_alpha()
balls = []
for i in range(5):
position = randint(0, width-100), randint(0, height-100)
speed = [randint(-10, 10), randint(-10, 10)]
ball = Ball(ball_image, position, speed)
balls.append(ball)
clock = pygame.time.Clock()
while running: # 4、为何不用while True:???
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0, 0))
for each in balls:
screen.blit(each.image, each.rect)
pygame.display.flip()
clock.tick(30)
if __name__ == '__main__':
main() |
|