首页 > 解决方案 > 如何让精灵一一出现

问题描述

所以我试图让它每次玩家点击一个错误的按钮时我的精灵一个接一个地出现,当它到达最后一个精灵时,它会打印一个文本“Lose”。我不太确定该怎么做,因为我是新手。

wrong buttons are BFHIJPQRSUVXYZ

我要添加的精灵

D1 = pygame.image.load("D1.png")
D2 = pygame.image.load("D2.png")
D3 = pygame.image.load("D3.png")
D4 = pygame.image.load("D4.png")
D5 = pygame.image.load("D5.png")
D6 = pygame.image.load("D6.png")
D7 = pygame.image.load("D7.png")
D8 = pygame.image.load("D8.png")

我的完整代码


import pygame
pygame.init()

# Drawing window screen
window = pygame.display.set_mode((700,500))
# The Name of my window
pygame.display.set_caption("StickMan")
# Drawing the buttons
A = pygame.image.load("A.png")
A1 = pygame.image.load("A1.png")
A2 = pygame.image.load("A2.png")
A3 = pygame.image.load("A3.png")
A4 = pygame.image.load("A4.png")
A5 = pygame.image.load("A5.png")
A6 = pygame.image.load("A6.png")
A7 = pygame.image.load("A7.png")
A8 = pygame.image.load("A8.png")
A9 = pygame.image.load("A9.png")
A10 = pygame.image.load("A10.png")
A11 = pygame.image.load("A11.png")
A12 = pygame.image.load("A12.png")
# stickman sprites
D1 = pygame.image.load("D1.png")
D2 = pygame.image.load("D2.png")
D3 = pygame.image.load("D3.png")
D4 = pygame.image.load("D4.png")
D5 = pygame.image.load("D5.png")
D6 = pygame.image.load("D6.png")
D7 = pygame.image.load("D7.png")
D8 = pygame.image.load("D8.png")

# Button class
class button1():
    def __init__(self, color, x,y,width,height, text=''):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text
        self.clicked = False
    def draw(self,window,outline=None):
        #Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(window, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
 
        pygame.draw.rect(window, self.color, (self.x,self.y,self.width,self.height),0)
 
        if self.text != '':
            font = pygame.font.SysFont('comicsans', 60)
            text = font.render(self.text, 1, (0,0,0))
            window.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))
 
    def isOver(self, pos):
        #Pos is the mouse position or a tuple of (x,y) coordinates
        return pos[0] > self.x and pos[0] < self.x + self.width and pos[1] > self.y and pos[1] < self.y + self.height
 
    def playSoundIfMouseIsOver(self, pos, sound):
        if self.isOver(pos):            
            if not self.over:
                eat.play()
                self.over = True
        else:
            self.over = False
            

# Color
white = (255,255,255)
Abutton = button1((0,255,0),287,310,55,55, '')       
Bbutton = button1((0,255,0),358,310,55,55, '')
Cbutton = button1((0,255,0),428,310,55,55, '')
Dbutton = button1((0,255,0),504,310,55,55, '')
Ebutton = button1((0,255,0),578,310,55,55, '')
Fbutton = button1((0,255,0),645,310,55,55, '')

Gbutton = button1((0,255,0),0,377,55,55, '')
Hbutton = button1((0,255,0),73,377,55,55, '')
Ibutton = button1((0,255,0),142,377,55,55, '')
Jbutton = button1((0,255,0),212,377,55,55, '')
Kbutton = button1((0,255,0),287,377,55,55, '')
Lbutton = button1((0,255,0),358,377,55,55, '')
Mbutton = button1((0,255,0),428,377,55,55, '')
Nbutton = button1((0,255,0),504,377,55,55, '')
Obutton = button1((0,255,0),578,377,55,55, '')
Pbutton = button1((0,255,0),645,377,55,55, '')

Qbutton = button1((0,255,0),0,445,55,55, '')
Rbutton = button1((0,255,0),73,445,55,55, '')
Sbutton = button1((0,255,0),142,445,55,55, '')
Tbutton = button1((0,255,0),212,445,55,55, '')
Ubutton = button1((0,255,0),287,445,55,55, '')
Vbutton = button1((0,255,0),358,445,55,55, '')
Wbutton = button1((0,255,0),428,445,55,55, '')
Xbutton = button1((0,255,0),504,445,55,55, '')
Ybutton = button1((0,255,0),578,445,55,55, '')
Zbutton = button1((0,255,0),645,445,55,55, '')

def redrawwindow():
    window.fill((0,0,0))
    window.blit(A,(0,0))
    if Abutton.clicked:
        window.blit(A1,(0,0))
    if Cbutton.clicked:
        window.blit(A2,(0,0))
    if Kbutton.clicked:
        window.blit(A3,(0,0))
    if Nbutton.clicked:
        window.blit(A4,(0,0))
    if Obutton.clicked:
        window.blit(A5,(0,0))
    if Wbutton.clicked:
        window.blit(A6,(0,0))
    if Lbutton.clicked:
        window.blit(A7,(0,0))
    if Ebutton.clicked:
        window.blit(A8,(0,0))
    if Dbutton.clicked:
        window.blit(A9,(0,0))
    if Gbutton.clicked:
        window.blit(A10,(0,0))
    if Mbutton.clicked:
        window.blit(A11,(0,0))
    if Tbutton.clicked:
        window.blit(A12,(0,0))


run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            #CKNOWLEDGMT
        elif event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            if Abutton.isOver(pos):
                Abutton.clicked = True
            if Cbutton.isOver(pos):
                Cbutton.clicked = True
            if Kbutton.isOver(pos):
                Kbutton.clicked = True
            if Nbutton.isOver(pos):
                Nbutton.clicked = True
            if Obutton.isOver(pos):
                Obutton.clicked = True
            if Wbutton.isOver(pos):
                Wbutton.clicked = True
            if Lbutton.isOver(pos):
                Lbutton.clicked = True
            if Ebutton.isOver(pos):
                Ebutton.clicked = True
            if Dbutton.isOver(pos):
                Dbutton.clicked = True
            if Gbutton.isOver(pos):
                Gbutton.clicked = True
            if Mbutton.isOver(pos):
                Mbutton.clicked = True
            if Tbutton.isOver(pos):
                Tbutton.clicked = True
           

    redrawwindow()
    pygame.display.update()
pygame.quit()

标签: pythonpython-3.ximagepygame

解决方案


添加对象列表。单击按钮并绘制列表中的所有对象时,将对象添加到列表中:

all_objects = []

def redrawwindow():
    window.fill((0,0,0))
    window.blit(A,(0,0))
    
    if Abutton.clicked:
        all_objects.append( (A1, (0,0)) )
    if Cbutton.clicked:
        all_objects.append( (A2, (0,0)) )
    # [...] all the other buttons and objects

    for img, pos in all_objects:
        window.blit(img, pos)

推荐阅读