2016-05-01 18 views
0

私は現在、ユーザー/プレーヤーが、プレーヤーの四角形よりも小さい四角形だけを集めることができる青色の四角形である正方形の収集ゲームに取り組んでいます。これらは色で表され、四角形が緑色よりも小さい場合は、緑色よりも大きい場合は赤色になります。Playerクラスのサイズを増やす方法衝突時に?

私は、プレーヤーが緑色の四角形と衝突したときにプレーヤーのサイズを増やす方法がわからないという問題に遭遇しました。私は動作していないと思ういくつかの異なるオプションを試してこのタスクを達成するために感謝するいくつかの異なるものを試してみました。ここで

は、私が現在使用していますコードです:

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としてそれらを使用しています。

関連する問題