2016-05-01 49 views
0

我目前正在进行一个广场收集游戏,其中用户/玩家将是一个蓝色正方形,只能收集比玩家的正方形小的正方形。如果正方形比玩家小于绿色,则它们用颜色表示,如果它更大,则会变成红色。如何增加玩家类大小碰撞后?

我遇到了一个问题,我无法弄清楚如何在玩家与绿色方块碰撞时增加玩家的尺寸。我尝试了许多不同的东西,但似乎并不奏效,并希望尝试完成此任务的一些不同选项。

这里是我目前使用的代码:

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) 

class Block(pygame.sprite.Sprite): 
    """ 
    This class represents the ball. 
    It derives from the "Sprite" class in Pygame. 
    """ 

    def __init__(self, color, width, height): 
     """ Constructor. Pass in the color of the block, 
     and its x and y position. """ 

     # Call the parent class (Sprite) constructor 
     super().__init__() 

     # 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): 
    """ The class is the player-controlled sprite. """ 

    # -- Methods 
    def __init__(self, x, y): 
     """Constructor function""" 
     # Call the parent's constructor 
     super().__init__() 

     # Set height, width 
     self.image = pygame.Surface([15, 15]) 
     self.image.fill(BLUE) 

     # Make our top-left corner the passed-in location. 
     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 

     # -- Attributes 
     # Set speed vector 
     self.change_x = 0 
     self.change_y = 0 

    def changespeed(self, x, y): 
     """ Change the speed of the player""" 
     self.change_x += x 
     self.change_y += y 

    def update(self): 
     """ Find a new position for the player""" 
     # move left or right 
     self.rect.x += self.change_x 
     if self.rect.x <= 0: 
      self.rect.x = 1 
      wall_sound.play() 
     if self.rect.x >= 680: 
      self.rect.x = 679 
      wall_sound.play() 
     # move up or down 
     self.rect.y += self.change_y 
     if self.rect.y <= 0: 
      self.rect.y = 1 
      wall_sound.play() 
     if self.rect.y >= 385: 
      self.rect.y = 384 
      wall_sound.play() 


# 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.' 
block_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() 

# Create a BLUE player block 
p_width = 30 
p_height = 25 
p_size = (p_width, p_height) 
player = Player(p_width, p_height) 
all_sprites_list.add(player) 

# BLOCK LIST 
for i in range(50): 
    width = random.randrange(20, 50) 
    height = width - 5 
    b_size = (width, height) 
    if b_size <= p_size: 
     color = GREEN 
    if b_size > p_size: 
     color = RED 
    # This represents a block 
    block = Block(color, width, height) 

    # Set a random location for the block 
    block.rect.x = random.randrange(screen_width - width) 
    block.rect.y = random.randrange(screen_height - height) 

    # Add the block to the list of objects 
    block_list.add(block) 
    all_sprites_list.add(block) 

# Loop until the user clicks the close button. 
done = False 

# Pygame sound effects 

collect_good = pygame.mixer.Sound("SnowWalk.ogg") 
collect_bad = pygame.mixer.Sound("icebreaks.ogg") 
wall_sound = pygame.mixer.Sound("evillaugh.ogg") 

# Used to manage how fast the screen updates 
clock = pygame.time.Clock() 

font = pygame.font.Font(None, 25) 

score = 0 

# -------- Main Program Loop ----------- 
while not done: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 

    # 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) 
      eli 
    # See if the player block has collided with anything. 
     #good 
    blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True) 

    # Check the list of collisions. 
     #good 
    for block in blocks_hit_list: 
     score += 1 
     collect_good.play() 

    if score <= -1: 
     done = True 
    # Draw all the spites 
    all_sprites_list.draw(screen) 

    text = font.render("Score: " + str(score), True, BLACK) 
    screen.blit(text, [20, 300]) 

    # Go ahead and update the screen with what we've drawn. 
    pygame.display.flip() 

    # Limit to 60 frames per second 
    clock.tick(60) 

pygame.quit()f event.key == pygame.K_UP: 
       player.changespeed(0, 3) 
      elif event.key == pygame.K_DOWN: 
       player.changespeed(0, -3) 

    # This calls update on all the sprites 
    all_sprites_list.update() 

    # Clear the screen 
    screen.fill(WHITE) 

    # See if the player block has collided with anything. 
     #good 
    blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True) 

    # Check the list of collisions. 
     #good 
    for block in blocks_hit_list: 
     score += 1 
     collect_good.play() 

    if score <= -1: 
     done = True 
    # Draw all the spites 
    all_sprites_list.draw(screen) 

    text = font.render("Score: " + str(score), True, BLACK) 
    screen.blit(text, [20, 300]) 

    # Go ahead and update the screen with what we've drawn. 
    pygame.display.flip() 

    # Limit to 60 frames per second 
    clock.tick(60) 

pygame.quit() 
+1

为什么你不能只是增加高度和宽度? – Natecat

+0

@Natecat我确实有玩家的高度和宽度(p_width和p_height),但我需要一个条件,当玩家块与p_height和p_width增加的绿色块相撞时。 –

回答

0

使玩家构造带的宽度和高度以及x和y。在碰撞检查中,增加这些参数并用新增的x和y值为玩家创建新图像。不要忘记同时重置播放器矩形。

此外,您传递的宽度和高度,并使用它们作为x和y不是宽度和高度。