我最近在Pygame中遇到了这个代码的麻烦,我无法找到我的错误。脚本目前只是一个可控制的精灵,(我理解它并没有显示,因为它被绘制在它上面)但是当我运行我的代码时,它认为小的白色方块(玩家)正在与较大的(,块)碰撞。树)。代码是这样的:
import pygame
from pygame.locals import*
#initialise pygame
pygame.init()
WHITE = (255,255,255)
#counts which sprite you should be on when running
#create screen
screen_width = 160
screen_height = 144
screen_multiplier = 4
screen = pygame.display.set_mode(((screen_width*screen_multiplier), (screen_height*screen_multiplier)))
pygame.display.set_caption('Pokemon Blood Red')
#Sprite stuff
sprite = pygame.image.load('player_east_still.png')
#Reform the sprite
sprite = pygame.transform.scale(sprite, (10*screen_multiplier, 14*screen_multiplier))
sprite.set_colorkey(WHITE)
#############################################################################
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.player_sprite = pygame.Surface((10*screen_multiplier, 14*screen_multiplier))
self.player_sprite.fill(WHITE)
self.rect = self.player_sprite.get_rect()
class Blocks(pygame.sprite.Sprite):
def __init__(self):
super(Blocks, self).__init__()
self.trees = pygame.Surface((20*screen_multiplier, 40*screen_multiplier))
self.trees.fill(WHITE)
self.rect = self.trees.get_rect()
player = Player()
blocks = Blocks()
#############################################################################
#Random variables for later use
amount_caught = 0
place = 1
catch1 = {'pokemon':'none',
'hp':0,
'attack':0,
'defence':0,
'sp_attack':0,
'sp_defence':0,}
background = 1
def area_load():
global background
if background == 1:
background = pygame.image.load('neuory_town.png').convert()
background = pygame.transform.scale(background, (160*screen_multiplier, 144*screen_multiplier))
area_load()
(x) = 160*0.45
(y) = 144*0.45
def caught():
if amount_caught == 0:
pass
#Mainloop
crashed = False
while not crashed:
pressed_keys = pygame.key.get_pressed()
player.update(pressed_keys)
x_change = 0
y_change = 0
#Different buttons
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = 5*screen_multiplier
sprite = pygame.image.load('player_west_still.png')
elif event.key == pygame.K_RIGHT:
x_change = -5*screen_multiplier
sprite = pygame.image.load('player_east_still.png')
elif event.key == pygame.K_UP:
y_change = -5*screen_multiplier
sprite = pygame.image.load('player_north_still.png')
elif event.key == pygame.K_DOWN:
y_change = 5*screen_multiplier
sprite = pygame.image.load('player_south_still.png')
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_change = 0
elif event.key == pygame.K_RIGHT:
x_change = 0
elif event.key == pygame.K_UP:
y_change = 0
elif event.key == pygame.K_DOWN:
y_change = 0
x += x_change
y += y_change
blockades = pygame.sprite.Group()
blockades.add(blocks)
#Check for collisions
hits = pygame.sprite.spritecollide(player, blockades, True)
if hits:
print ('Collision!')
#Draw everything
sprite = pygame.transform.scale(sprite, (10*screen_multiplier, 14*screen_multiplier))
sprite.set_colorkey(WHITE)
screen.blit(background,(0,0))
screen.blit(sprite,(x,y))
screen.blit(player.player_sprite, (x, y))
screen.blit(blocks.trees, (200, 200))
pygame.display.flip()
pygame.quit()
感谢您的帮助!
答案 0 :(得分:0)
pygame.sprite.spritecollide
检查精灵的rect
是否重叠。在实例化之后,您永远不会将rects移动到新位置,因此player
和blocks
仍然位于(0,0)(尝试print(blocks.rect, player.rect)
)。要移动它们,您可以更改x和y坐标,topleft,center或rect的other attributes:
blocks.rect.center = (400, 500)
player.rect.topleft = (20, 40)
player.rect.x += 50
player.rect.y += 100
在原始示例中,您不会移动精灵,而只会更改用作图像的blit位置的变量x和y。
# This doesn't change the position of the rect, so there won't be collisions.
x += x_change
y += y_change
您应该移动矩形,然后使用rect作为blit位置:
player.rect.x += x_change
player.rect.y += y_change
screen.blit(player.player_sprite, player.rect) # Blits the image at rect's topleft coords.
此外,pygame.sprite.Sprite
的图片属性应称为image
而不是player_sprite
。然后你可以将所有精灵放到一个pygame.sprite.Group
中,并更新和绘制所有精灵,你只需要两行代码:
# Before the while loop define the sprite group.
all_sprites = pygame.sprite.Group()
# Add all sprites.
all_sprites.add(player) # And other sprites.
# In the while loop update the sprites.
all_sprites.update()
# And in the draw phase.
all_sprites.draw(screen)