消除方块+不同颜色score加减分+player是否出界+鼠标控制+窗口显示得分

本文介绍了一个使用Python和Pygame库实现的小精灵收集游戏。玩家控制蓝色小精灵,在屏幕上移动来收集绿色小精灵并避开红色小精灵以获得分数。游戏通过随机位置生成小精灵,并实现了边界反弹功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要求:

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()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值