我目前正在制作一个方形收集游戏,其中用户/玩家将是一个蓝色方块,只能收集小于玩家方阵的方块。这些用颜色表示,如果方块小于播放器而不是绿色,如果它更大则为红色。
我遇到了一个问题,当玩家与绿色方块发生碰撞时,我无法弄清楚如何增加玩家的尺寸。我已经尝试了许多似乎不起作用的不同的东西,并会欣赏一些不同的选项来尝试完成这项任务。
以下是我目前使用的代码:
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()
答案 0 :(得分:0)
让玩家构造函数获取宽度和高度以及x和y。在碰撞检查中,增加这些并为新增加的x和y值为玩家创建新图像。不要忘记同时重置播放器矩形。
此外,您正在传递宽度和高度,并将它们用作x和y而不是宽度和高度。