playtheball问题
#导入模块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 main():
pygame.init()
ball_image = '星星.png'
bg_image = 'th.png'
running = True
bg_size = width,height = 236,360
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption('playtheball')
background = pygame.image.load(bg_image).convert_alpha()
balls = []
for i in range(5):
position = randint(0,width-100),randint(0,height-100)
speed =
ball = Ball(ball_image,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(background,(0, 0))
for each in balls:
screen.blit(each.image, each.rect)
pygame.display.flip()
clock.tick(30)
if __name__ =='__main__':
main()
(未打完)
为什么报错'blit'违背定义? 问题出在这一行代码: screen,blit(background,(0, 0)) ,应该改为 screen.blit(background,(0, 0)) 。
错误信息的意思是说blit不是screen对象的一个属性或方法。 blit是pygame.Surface对象的一个方法,用于将一个图像绘制到另一个图像上。在这个代码中,我们要将背景图像绘制到屏幕上,因此应该使用screen.blit()方法。
页:
[1]