2016-05-01 5 views
0

Ich arbeite gerade an einem quadratischen Sammelspiel, bei dem der User/Player ein blaues Quadrat ist, das nur Quadrate sammeln kann, die kleiner sind als das Spielerfeld. Diese werden durch Farbe dargestellt, wenn das Quadrat kleiner als der Spieler ist, als es grün ist, wenn es größer ist, wird es rot sein.So erhöhen Sie die Player-Klasse Größe Bei einer Kollision?

Ich bin auf ein Problem gestoßen, bei dem ich nicht herausfinden kann, wie man die Größe des Players erhöht, wenn der Spieler mit den grünen Quadraten kollidiert. Ich habe eine Reihe verschiedener Dinge ausprobiert, die nicht zu funktionieren scheinen und würde mir einige verschiedene Optionen für diese Aufgabe wünschen. Hier

ist der Code, ich bin derzeit mit:

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

Warum können Sie nicht nur Höhe und Breite hinzufügen? – Natecat

+0

@Natecat Ich habe die Höhe und Breite (p_width und p_height) des Players, aber ich brauche eine Bedingung, wenn der Player Block mit einem grünen Block kollidiert, dass die p_height und p_width erhöhen. –

Antwort

0

Stellen der Spieler Konstruktor nehmen die Breite und Höhe sowie die x- und y. Vergrößern Sie diese in der Kollisionsprüfung und erstellen Sie ein neues Bild für den Player mit den neuen erhöhten x- und y-Werten. Vergessen Sie nicht, den Player gleichzeitig zurückzusetzen.

Sie übergeben auch die Breite und Höhe und verwenden sie als x und y nicht Breite und Höhe.