【问题标题】:Increase displayed number after clicking appropriate button [pygame]单击适当的按钮后增加显示的数字[pygame]
【发布时间】:2018-01-04 00:05:30
【问题描述】:

对于我的 pygame 'score' 板,我正在尝试制作它,以便当我单击其中一个彩色矩形时,它旁边的数字会增加。我将如何 a) 显示数字,b) 使矩形可点击,以及 c) 使数字在我左键单击相应的矩形时增加,在我右键单击时减少?

import pygame
pygame.init()

white = 255,255,255 #This block defines all the colors in (R,G,B) format
black = 0,0,0
green = 0,255,0
blue = 0,76,153
red = 255,0,0
orange = 255,128,0

height = 1366 #The height and width of our window
width = 768
window = pygame.display.set_mode((height,width)) 
pygame.display.set_caption("Score") #Edit the text between quotes to change 
window title

title_font = pygame.font.SysFont('Comic Sans MS', 70) 
button_font = pygame.font.SysFont('Times New Roman', 12)


title = title_font.render("The Activity", True, (black))

clock = pygame.time.Clock()
crashed = False

flask_img_right = pygame.image.load('flask.jpg').convert() 
flask_img_left = pygame.image.load('flask1.jpg').convert()

def flask_right(x_fr,y_fr):
    window.blit(flask_img_right,(x_fr,y_fr))
x_fr = (1000)
y_fr = (200)

def flask_left(x_fl,y_fl):
    window.blit(flask_img_left,(x_fl,y_fl))
x_fl = (100)
y_fl = (200)

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Dodged: "+str(count), True, black)
    gameDisplay.blit(text,(0,0))


while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
    #Increasing the first number moves text to the right
    #Increasing the second number moves text lower
    #Assume this for everything below unless otherwise stated

    window.fill(white)
    flask_right(x_fr,y_fr)
    flask_left (x_fl, y_fl)
    window.blit(title,(500,50))   

    mouse = pygame.mouse.get_pos()

    b1_surf, b1_rect = text_objects("", button_font)
    b1_rect.center = ((550),(220))
    window.blit(b1_surf, b1_rect)
    pygame.draw.rect(window, blue, (500,200,100,50))
    b1_text, b1t_rect = text_objects("Team 1", button_font)
    b1t_rect.center = ((550),(220))
    window.blit(b1_text, b1t_rect)

    b2_surf, b2_rect = text_objects("",button_font)
    b2_rect.center = ((550,270))
    window.blit(b2_surf, b2_rect)
    pygame.draw.rect(window, red,(500,250,100,50))
    b2_text, b2t_rect = text_objects("Team 2", button_font)
    b2t_rect.center = ((550,270))
    window.blit(b2_text, b2t_rect)

    b3_surf, b3_rect = text_objects("",button_font)
    b3_rect.center = ((550,320))
    window.blit(b3_surf, b3_rect)
    pygame.draw.rect(window, orange,(500,300,100,50))
    b3_text, b3t_rect = text_objects("Team 3", button_font)
    b3t_rect.center = ((550,320))
    window.blit(b3_text, b3t_rect)

    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()
end_command = input("any: ")

【问题讨论】:

    标签: python python-3.x button pygame


    【解决方案1】:

    使用按钮class

    class Button(object):
        def __init__(self,x,y,width,height,text_color,background_color,text):
            self.rect=pygame.Rect(x,y,width,height)
            self.x=x
            self.y=y
            self.width=width
            self.height=height
            self.text=text
            self.text_color=text_color
            self.background_color=background_color
    
        def check(self):
            return self.rect.collidepoint(pygame.mouse.get_pos())
    
        def draw(self):
            pygame.draw.rect(screen, self.background_color,(self.rect),0)
            drawText(self.text,font,screen,self.x+self.width/2,self.y+self.height/2,self.text_color)  
            pygame.draw.rect(screen,self.text_color,self.rect,3)
    

    在代码中实现(借自skrx):

    def main():
        clock = pg.time.Clock()
        font = pg.font.Font(None, 30)
        button=Button(parameters)
    
        score = 0
        done = False
    
        while not done:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    done = True
                elif event.type == pg.MOUSEBUTTONDOWN:
                    if button.check()==True:
                        if event.button == 1:  # Left button.
                            print('Increment.')
                            score += 1
                        elif event.button == 2:  # Right button.
                            print('Decrement.')
                            score -= 1
    
            screen.fill(GRAY)
            button.draw()
            txt = font.render(str(score), True, BLUE)
            screen.blit(txt, (260, 206))
    
            pg.display.flip()
            clock.tick(10)
    
    
    if __name__ == '__main__':
        main()
        pg.quit()
    

    【讨论】:

    • 它工作得几乎完美。但是,当我左键单击时,它上升得非常快。当我放手时,分数上升了 5 或 6。与右键单击相同。有什么办法可以让 1 点击 = +1 或 -1?
    • 将刻度更改为 15
    • 我把它改成了15,但是,它有时还是会增加和减少2,而且它似乎经常没有注册右键。会不会是鼠标问题?
    • 我将 event.button 更改为 2,抱歉。我认为clock.tick 10 应该足够慢以计算每次按下的按钮。
    • 好吧,现在可以了,原来也是鼠标问题。谢谢!
    【解决方案2】:

    Here's an answer 向您展示了两个带有简单标签的按钮的解决方案。要增加或减少数字,只需检查是否按下了鼠标左键或右键。

    import pygame as pg
    
    
    pg.init()
    screen = pg.display.set_mode((640, 480))
    
    GRAY = pg.Color('gray15')
    BLUE = pg.Color('dodgerblue1')
    
    
    def main():
        clock = pg.time.Clock()
        font = pg.font.Font(None, 30)
        button_rect = pg.Rect(200, 200, 50, 30)
    
        score = 0
        done = False
    
        while not done:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    done = True
                if event.type == pg.MOUSEBUTTONDOWN:
                    if button_rect.collidepoint(event.pos):
                        if event.button == 1:  # Left button.
                            print('Increment.')
                            score += 1
                        elif event.button == 3:  # Right button.
                            print('Decrement.')
                            score -= 1
    
            screen.fill(GRAY)
            pg.draw.rect(screen, BLUE, button_rect)
            txt = font.render(str(score), True, BLUE)
            screen.blit(txt, (260, 206))
    
            pg.display.flip()
            clock.tick(30)
    
    
    if __name__ == '__main__':
        main()
        pg.quit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多