要求:
http://programarcadegames.com/index.php?chapter=lab_sprite_collecting
# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://programarcadegames.com/
# http://simpson.edu/computer-science/
# Explanation video: http://youtu.be/4W2AqUetBi4
import pygame
import random
# Define some colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
red = ( 255, 0, 0)
green = ( 0, 255, 0)
blue = (0,0,255)
# This class represents the ball
# It derives from the "Sprite" class in Pygame
class Block(pygame.sprite.Sprite):
# Constructor. Pass in the color of the block,
# and its x and y position
def __init__(self, color, width, height):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# image.
# Update the position of this object by setting the values
# of rect.x and rect.y
self.rect = self.image.get_rect()
class Player(pygame.sprite.Sprite):
change_x = 0
change_y = 0
def __init__(self,x,y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([15,15])
self.image.fill(blue)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def changespeed(self,x,y):
self.change_x += x
self.change_y += y
def update(self):
self.rect.x += self.change_x
self.rect.y += self.change_y
if self.rect.x>700:
self.rect.x = 0
if self.rect.x<0:
self.rect.x = 700
if self.rect.y>500:
self.rect.y = 0
if self.rect.y<0:
self.rect.y = 500
# Initialize Pygame
pygame.init()
# Set the height and width of the screen
screen_width=700
screen_height=400
screen=pygame.display.set_mode([screen_width,screen_height])
# This is a list of 'sprites.' Each block in the program is
# added to this list. The list is managed by a class called 'Group.'
good_list = pygame.sprite.Group()
bad_list = pygame.sprite.Group()
# This is a list of every sprite. All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()
for i in range(50):
# This represents a block
block = Block(green, 20, 15)
# Set a random location for the block
block.rect.x = random.randrange(screen_width)
block.rect.y = random.randrange(screen_height)
# Add the block to the list of objects
good_list.add(block)
all_sprites_list.add(block)
for i in range(50):
# This represents a block
block = Block(red, 20, 15)
# Set a random location for the block
block.rect.x = random.randrange(screen_width)
block.rect.y = random.randrange(screen_height)
# Add the block to the list of objects
bad_list.add(block)
all_sprites_list.add(block)
# Create a red player block
player = Player(20, 15)
all_sprites_list.add(player)
#Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
score = 0
# -------- Main Program Loop -----------
while done==False:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
# Set the speed based on the key pressed
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-3,0)
elif event.key == pygame.K_RIGHT:
player.changespeed(3,0)
elif event.key == pygame.K_UP:
player.changespeed(0,-3)
elif event.key == pygame.K_DOWN:
player.changespeed(0,3)
# Reset speed when key goes up
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(3,0)
elif event.key == pygame.K_RIGHT:
player.changespeed(-3,0)
elif event.key == pygame.K_UP:
player.changespeed(0,3)
elif event.key == pygame.K_DOWN:
player.changespeed(0,-3)
player.update()
# Clear the screen
screen.fill(white)
# See if the player block has collided with anything.
blocks_good_list = pygame.sprite.spritecollide(player, good_list, True)
blocks_bad_list = pygame.sprite.spritecollide(player, bad_list, True)
# Check the list of collisions.
for block in blocks_good_list:
score += 1
for block in blocks_bad_list:
score += -1
font = pygame.font.Font(None,25)
text = font.render("The score is "+str(score),True,black)
screen.blit(text,[5,5])
# Draw all the spites
all_sprites_list.draw(screen)
# Limit to 20 frames per second
clock.tick(20)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
pygame.quit()