理想的结果是有一个框,玩家可以点击,然后慢慢填补。如果用户单击按钮,我在测试后尝试使用while和for循环,但它没有缓慢增加,而是只增加了整个框的一小部分。整个游戏代码如下,但我的问题似乎是第70-80行。你知道吗
import pygame, sys, time, threading, tqdm
pygame.init()
#Setting up background colors, etc
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
YELLOW =(255,255,0)
#Setting up window and caption
DISPLAYSURF_x = 460
DISPLAYSURF_y = 720
DISPLAYSURF = pygame.display.set_mode((DISPLAYSURF_x,DISPLAYSURF_y))
pygame.display.set_caption('Adventure Capitalist')
logo = pygame.image.load('Logo.png')
menu = pygame.image.load('menu.png')
DISPLAYSURF.fill(WHITE)
cash = 30
amount = 0
barlength = 102
storeBoard = pygame.image.load('storeBoard.png')
def buyDraw(amount, minxbuy, minybuy):
Font1 = pygame.font.SysFont('monaco', 24)
buySurface = Font1.render('{0}'.format(amount), True, BLACK)
buyRect = buySurface.get_rect()
buyRect.midtop = (75, minybuy)
DISPLAYSURF.blit(buySurface,buyRect)
def cashDraw(cash):
Font2 = pygame.font.SysFont('monaco', 40)
cashSurface = Font2.render(' ${0}'.format(cash), True, GREEN)
cashRect = cashSurface.get_rect()
cashRect.midtop = (420, 10)
DISPLAYSURF.blit(cashSurface,cashRect)
# amount of item, cost to buy, time taken to complete action, money per run
def capitalist(amount, cost, timez, gain, minxbuy, maxxbuy, minybuy, maxybuy, minxgain, maxxgain, minygain, maxygain, cash):
print ("ran")
# Buy button
DISPLAYSURF.fill(WHITE)
DISPLAYSURF.blit(storeBoard, (0, 0))
buyDraw(amount, minxbuy, minybuy)
cashDraw(cash)
pygame.display.flip()
button_rect = pygame.Rect(minxgain, minygain, maxxgain, maxygain)
# Coefficient to calculate the width of the rect for a given time.
coefficient = maxxgain / timez
time = 0
clock = pygame.time.Clock()
dt = 0
done = False
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
(x, y) = pygame.mouse.get_pos()
if x < maxxbuy and x > minxbuy and y < maxybuy and y > minybuy and cash >= cost:
DISPLAYSURF.fill(WHITE, (minxbuy+25, minybuy, maxxbuy+30, maxybuy))
amount += 1
buyDraw(amount, minxbuy, minybuy)
print (cash)
DISPLAYSURF.fill(WHITE, (375, 0 , 460, 51))
cash -= cost
cashDraw(cash)
pygame.display.flip()
if x < maxxgain and x > minxgain and y < maxygain and y > minygain and amount > 0:
# If mouse is over the button, increase the timer.
if time < timez: # Stop increasing if max_time is reached.
time += dt
if time >= timez:
time = timez
inc = time * coefficient
pygame.draw.rect(DISPLAYSURF, BLACK, (minxgain, minygain, inc/2, maxygain/2))
pygame.display.flip()
dt = clock.tick(60) / 100
def opening():
DISPLAYSURF.blit(logo, (155, 50))
DISPLAYSURF.blit(menu, (0 , 125))
while True:
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
(x, y) = pygame.mouse.get_pos()
if x < 375 and x > 80 and y < 545 and y > 395:
# 1 2 3 4 5 6 7 8 9 10 11 12 13
#amount, cost, time, gain, minxbuy, maxxbuy, minybuy, maxybuy, minxgain, maxxgain, minygain, maxygain, cash
# capitalist(0, 5, 1, 1, 21, 41, 21, 41, 120, 204, 20, 41, cash)
capitalist(1, 10, 3, 5, 21, 41, 21, 41, 120, 204, 20, 41, cash)
pygame.display.flip()
opening()
我不确定您的按钮应该如何工作,但我可以告诉您如何修复
capitalist
函数中的事件处理和逻辑。您的主要问题是使用事件循环来增加数量,而在本例中,您更希望在while
循环中使用pygame.mouse.get_pressed
,以查看当前按下了哪些鼠标按钮。然后检查mouse_pos
是否与某个预定义按钮(pygame.Rect
)冲突,并运行所需的代码。你知道吗绘图代码也应该与事件处理和游戏逻辑分开。我这里有一个完整的工作示例,还有一些补充说明(正如我所说的,我不确定按钮应该如何工作,所以您必须根据需要调整它们):
没有2个主回路。既不是嵌套的也不是线程的。应该只有一个循环检查事件队列。如果需要线程,那么应该在主循环中创建自己的事件分布,或者手动管理事件队列。获取并删除主循环中的所有事件,但另一个循环中需要的事件除外。另一个循环应该只删除它自己的事件。
不要直接绘制到显示表面。最好是修改一个曲面,然后将其全部或部分blit到屏幕上,并翻转()/update()它。有时将显示表面视为任何其他原因都会导致问题。
也许您应该使用子曲面来简化填充矩形的任务。
当您稍微整理一下代码时,问题应该会消失,否则它会更明显。你知道吗
相关问题 更多 >
编程相关推荐