# Listing_17-1.py
# Copyright Warren Sande, 2009
# Released under MIT license http://www.opensource.org/licenses/mit-license.php
# Version 59 ----------------------------
# Using sprites to put multiple ball images on the screen
import sys, pygame
#-----ball subclass definition -----------------------------
class MyBallClass(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pygame.image.load(image_file) #类中的类 -> pygame中这个符号是返回的类型。
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
#----- Main Program -----------------------------
pygame.init()
size = width, height = 640, 480
screen = pygame.display.set_mode(size)
screen.fill([255, 255, 255])
img_file = "beach_ball.png"
location = [180 + 10, 180 + 10]
ball = MyBallClass(img_file, location)
f_str=str(type(ball.image)) #这里肯定就能理解类了吧
f_font = pygame.font.Font(None, 26)
f_surf = f_font.render(f_str, 1, (0, 0, 255))
screen.blit(f_surf, [60, 300])
screen.blit(ball.image, ball.rect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
利用书中自带的软件运行通过。
14行和18行是理解类的关键行。