【发布时间】: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