所以我对 pygame 有点陌生,刚刚开始。 我正在尝试制作一个简单的装扮游戏,其中有三个按钮。根据您按下的按钮,您可以将粉色、蓝色或绿色衬衫放在人体模型上。 然而,我的问题是,当您单击另一件衬衫的按钮时,游戏无法正确地将一件衬衫替换为另一件衬衫。我想要做到这一点,以便您可以在按下相应按钮时更改您希望人体模型穿的衬衫。
这是我当前的代码:
import pygame
class Button(pygame.sprite.Sprite):
def __init__(self,x,y,image):
pygame.sprite.Sprite.__init__(self)
#creates a rectangle out of an image and gives it a x and y
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.clicked = False #will let function know if button clicked
def update(self):
action = False #whatever action that will occur in the main code
#get the mouse's position
mousePosition = pygame.mouse.get_pos()
if self.rect.collidepoint(mousePosition):
if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
self.clicked = True
if self.clicked == True:
action = True
win.blit(self.image,self.rect)
return action
pygame.init()
#screen window's hight and width
winW = 600
winH = 600
#game window
win = pygame.display.set_mode((winW,winH))
#game title
pygame.display.set_caption("Dress Up Game")
#image of mannequin to put cloths on
lady = pygame.image.load("images/mannequin.webp")
lady = pygame.transform.scale(lady,(460,560))
#images of the cloths
blueShirt = pygame.image.load("images/blue shirt.jpg")
blueShirt = pygame.transform.scale(blueShirt,(60,60))
greenShirt = pygame.image.load("images/greenShirt.jpg")
greenShirt = pygame.transform.scale(greenShirt,(60,60))
pinkShirt = pygame.image.load("images/pinkShirt.jpg")
pinkShirt = pygame.transform.scale(pinkShirt,(60,60))
#the buttons
pinkShirtButton = Button(20,100,pinkShirt)
blueShirtButton = Button(80,100,blueShirt)
greenShirtButton = Button(132,100,greenShirt)
#game loop
run = True
while run == True:
win.fill((255,0,0))
win.blit(lady,(280,90)) #draws the mannequin on screen
if pinkShirtButton.update(): #if you press the button
win.blit(pinkShirt,(500,500)) #the pink shirt goes on the mannequin
if blueShirtButton.update(): #if you press the button
win.blit(blueShirt,(500,300)) #the blue shirt goes on the mannequin
if greenShirtButton.update(): #if you press the button
win.blit(greenShirt,(500,100)) #the green shirt goes on the mannequin
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()
我尝试通过让当前未挑选到模特身上的衬衫从屏幕上消失来使其消失,但这似乎没有影响任何事情。
我现在不太确定该怎么做
您应该使用Sprite Groups来管理您的精灵。有关碰撞检测,请参阅此答案。
要切换可见性,您可以使用特殊的精灵组,该组只能包含一个精灵并分配关联的精灵。
这是基于您的代码的未经测试的代码:
import pygame
class Button(pygame.sprite.Sprite):
def __init__(self,x,y,image):
pygame.sprite.Sprite.__init__(self)
#creates a rectangle out of an image and gives it a x and y
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def update(self):
pass
pygame.init()
#screen window's hight and width
winW = 600
winH = 600
#game window
win = pygame.display.set_mode((winW,winH))
clock = pygame.time.Clock()
#game title
pygame.display.set_caption("Dress Up Game")
#image of mannequin to put cloths on
lady = pygame.image.load("images/mannequin.webp")
lady = pygame.transform.scale(lady,(460,560))
#images of the cloths
blueShirt = pygame.image.load("images/blue shirt.jpg")
blueShirt = pygame.transform.scale(blueShirt,(60,60))
greenShirt = pygame.image.load("images/greenShirt.jpg")
greenShirt = pygame.transform.scale(greenShirt,(60,60))
pinkShirt = pygame.image.load("images/pinkShirt.jpg")
pinkShirt = pygame.transform.scale(pinkShirt,(60,60))
# coordinates
shirt_position = (500, 500)
mannequin_position = (280, 90)
#the buttons
pinkShirtButton = Button(20,100,pinkShirt)
blueShirtButton = Button(80,100,blueShirt)
greenShirtButton = Button(132,100,greenShirt)
# groups
buttons = pygame.sprite.Group()
for button in [pinkShirtButton, blueShirtButton, greenShirtButton]
buttons.add(button)
selected_shirt = pygame.sprite.GroupSingle()
#game loop
run = True
while run == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.MOUSEBUTTONUP:
for button in buttons:
if button.rect.collide_point(event.pos):
# create a sprite to draw based on the button image
shirt = Button(*shirt_position, button.image)
selected_shirt.add(shirt)
win.fill("red")
win.blit(lady, mannequin_position) #draws the mannequin on screen
buttons.draw(win)
selected_shirt.draw(win)
pygame.display.update()
clock.tick(60) # limit to 60 FPS
pygame.quit()
将来,请构建一个最小可重现示例,以便更轻松地为您提供帮助。例如,对于这样的问题,以编程方式创建精灵图像,例如为衬衫绘制三角形,这样就不需要外部资源。