Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 单击相应按钮[pygame]后增加显示的数字_Python_Python 3.x_Button_Pygame - Fatal编程技术网

Python 单击相应按钮[pygame]后增加显示的数字

Python 单击相应按钮[pygame]后增加显示的数字,python,python-3.x,button,pygame,Python,Python 3.x,Button,Pygame,对于我的pygame“得分”板,我试图使其在单击一个彩色矩形时,旁边的数字将增加。我将如何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

对于我的pygame“得分”板,我试图使其在单击一个彩色矩形时,旁边的数字将增加。我将如何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: ")
这显示了两种简单标签按钮的解决方案。要增加或减少数字,只需检查是否按下了鼠标左键或右键

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()
这显示了两种简单标签按钮的解决方案。要增加或减少数字,只需检查是否按下了鼠标左键或右键

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()

使用按钮
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借用):


使用按钮
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借用):


它几乎可以完美地工作。然而,当我左键单击时,它会快速上升。当我放手的时候,分数上升了5到6分。与右键单击相同。有没有办法让1次点击=+1或-1?将勾号改为15,但是,它有时会增加2次,有时会减少2次,而且通常似乎不会记录右击。这可能是鼠标问题吗?我将
事件按钮更改为2,对不起。我认为,
clock.tick 10
应该足够慢,可以计算每次按下的按钮数。好了,现在它工作了,原来这也是鼠标的问题。谢谢它几乎可以完美地工作。然而,当我左键单击时,它会快速上升。当我放手的时候,分数上升了5到6分。与右键单击相同。有没有办法让1次点击=+1或-1?将勾号改为15,但是,它有时会增加2次,有时会减少2次,而且通常似乎不会记录右击。这可能是鼠标问题吗?我将
事件按钮更改为2,对不起。我认为,
clock.tick 10
应该足够慢,可以计算每次按下的按钮数。好了,现在它工作了,原来这也是鼠标的问题。谢谢