Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.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 我正在快速播放一幅图像,但它在大约一毫秒后消失了_Python_Pygame - Fatal编程技术网

Python 我正在快速播放一幅图像,但它在大约一毫秒后消失了

Python 我正在快速播放一幅图像,但它在大约一毫秒后消失了,python,pygame,Python,Pygame,我试图在点击一行中的按钮时在屏幕上闪烁一个图像 screen.blit(buttonPlusImage, [random.randrange(560, 680), random.randrange(330, 450)]) 但是,由于屏幕刷新,图像不久后就消失了。我正在使用这三行代码更新显示:pygame.display.flip、FPS.tick144和screen.fillwhite。我怎样才能使图像保持在屏幕上,而不随屏幕消失。fillwhit

我试图在点击一行中的按钮时在屏幕上闪烁一个图像

screen.blit(buttonPlusImage,
           [random.randrange(560, 680),
            random.randrange(330, 450)])
但是,由于屏幕刷新,图像不久后就消失了。我正在使用这三行代码更新显示:pygame.display.flip、FPS.tick144和screen.fillwhite。我怎样才能使图像保持在屏幕上,而不随屏幕消失。fillwhite?我想这就是原因

我把代码粘贴进去了

我改变了一切

我把巴顿的所有信息都保存在字典里。如果按钮正常、悬停或单击,还有一种保存信息的状态。我用pygame.mouse.get_来更改它,而不是用MOUSEBUTTONDOWN。我可以使用状态选择要显示的图像,但我在选中pygame.mouse.get_时设置了图像


我没有改变一切

避免在应用程序循环中出现多个事件循环。注意,从队列中删除所有事件。因此,第一个或第二个事件循环将获得事件。但不是两个循环。您可以通过在循环中获取一次事件并多次使用事件列表来消除此问题:

而主循环: events=pygame.event.get [...] 如果按钮01碰撞点: 开关按钮01事件 [...] 对于事件中的事件: [...] 如果你想让图像留在屏幕上,那么你必须在每一帧中绘制它。添加一个存储按钮位置的按钮,并在主应用程序循环中绘制位置。功能开关按钮01必须返回以下位置:

ButtonPlupos=无 而主循环: [...] 如果按钮01碰撞点: ButtonPlupos=开关Button01事件,ButtonPlupos 如果按钮为USPOS: screen.blitbuttonPlusImage,buttonpuspos 功能switchButton01不闪烁按钮,它只是返回位置。此外,事件列表被传递给函数,函数处理列表中的事件,而不是检索自己的列表。在任何cas中,都会返回图像的位置。新位置或允许为无的当前位置:

def开关按钮01事件,按钮按钮按钮: 全局按钮02 button02=pygame.transform.ScaleButton02100100 screen.blitbutton02[580350] 对于事件中的事件: 如果event.type==pygame.MOUSEBUTTONDOWN: 全局clickNum clickNum+=1 返回random.randrange560680,random.randrange330450 返回按钮按钮按钮 完整代码:

导入pygame 随机输入 pygame.init 变量 mainLoop=True font=pygame.font.SysFont'comicsansms',25 白色=[255,255,255] 绿色=[0,150,0] 灰色=[140140140] 黑色=[0,0,0] clickNum=0 单击金额=1 FPS=pygame.time.Clock screen=pygame.display.set_mode1300700 功能 def开关按钮01事件,按钮按钮按钮: 全局按钮02 button02=pygame.transform.ScaleButton02100100 screen.blitbutton02[580350] 对于事件中的事件: 如果event.type==pygame.MOUSEBUTTONDOWN: 全局clickNum clickNum+=1 返回random.randrange560680,random.randrange330450 返回按钮按钮按钮 加载图像 button01=pygame.image.load'button_100.png' button02=pygame.image.load'button\u 100hovered.png' icon=pygame.image.load'icon_128.png' buttonPlusImage=pygame.image.load'buttonPlus_32.png' 窗口本身和图标 pygame.display.set_字幕增量冒险 pygame.display.set_iconicon ButtonPlupos=无 而主循环: pygame.display.flip FPS.144 屏幕。填充白色 events=pygame.event.get 游戏中的实际内容 button01=pygame.transform.ScaleButton01100100 screen.blitbutton01[580350] 单击计数器\u text=font.renderClick counter:,True,black,white screen.blitclick_counter_text[525,50] button01rect=button01.get_rectcenter=630400 button01collidepoint=button01rect.collidepointpygame.mouse.get\u pos 单击\u counter=font.renderstrclickNum、True、黑色、白色 screen.blitclick_计数器[700,50] 如果按钮01碰撞点: ButtonPlupos=开关Button01事件,ButtonPlupos 如果按钮为USPOS: screen.blitbuttonPlusImage,buttonpuspos 退出 对于事件中的事件: 如果event.type==pygame.QUIT: pygame.退出 退出 如果event.type==pygame.KEYDOWN和event.key==pygame.K_转义: pygame.退出 退出
清除屏幕后保存并重新绘制鼠标按钮,当您按下鼠标按钮时将其显示,但当鼠标按钮从未按下位置更改为按下位置时,会创建一条消息,但当您按住鼠标按钮时,不会创建该事件。所以你们只画了一次,当你们按下按钮的时候,它就不会再画了。您可能需要pygame.mouse.get_来测试是否一直按下按钮-在每个循环中检查它。或者更好地学习 如何使用类来保存有关对象(如按钮)的信息。
import pygame
import random
pygame.init()
​
# variables
mainLoop = True
font = pygame.font.SysFont('comicsansms', 25)
white = [255, 255, 255]
green = [0, 150, 0]
gray = [140, 140, 140]
black = [0, 0, 0]
clickNum = 0
clickAmount = 1
FPS = pygame.time.Clock()
screen = pygame.display.set_mode((1300, 700))
​
# functions
def switchButton01():
    global button02
    button02 = pygame.transform.scale(button02, (100, 100))
    screen.blit(button02, [580, 350])
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            global clickNum
            clickNum += 1
            screen.blit(buttonPlusImage, [random.randrange(560, 680), random.randrange(330, 450)])
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            pygame.quit()
            quit()
​
# load images
button01 = pygame.image.load('button_100.png')
button02 = pygame.image.load('button_100hovered.png')
icon = pygame.image.load('icon_128.png')
buttonPlusImage = pygame.image.load('buttonPlus_32.png')
​
# window itself and icon
pygame.display.set_caption("incremental adventures")
pygame.display.set_icon(icon)
​
while mainLoop:
    pygame.display.flip()
    FPS.tick(144)
    screen.fill(white)
​
    # actual content in the game
​
    button01 = pygame.transform.scale(button01, (100, 100))
    screen.blit(button01, [580, 350])
    click_counter_text = font.render("Click counter: ", True, black, white)
    screen.blit(click_counter_text, [525, 50])
    button01rect = button01.get_rect(center=(630, 400))
    button01collidepoint = button01rect.collidepoint(pygame.mouse.get_pos())
    click_counter = font.render((str(clickNum)), True, black, white)
    screen.blit(click_counter, [700, 50])
    if button01collidepoint:
        switchButton01()
​
​
    # quits
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            pygame.quit()
            quit()
import pygame
import random

# --- constants --- (UPPER_CASE_NAMES)

WHITE = (255, 255, 255)
GREEN = (0, 150, 0)
RED   = (255, 0, 0)
GRAY  = (140, 140, 140)
BLACK = (0, 0, 0)

FPS = 25 # 144

# --- functions ---

# empty

# --- main ---

pygame.init()
screen = pygame.display.set_mode((1300, 700))

# - objects -

button01 = dict()
button01['image_normal'] = pygame.surface.Surface((100,100))#
button01['image_normal'].fill(GRAY)
#button01['image_normal'] = pygame.image.load('button_100.png')
#button01['image_normal'] = pygame.transform.scale(button01['image_normal'], (100, 100))
button01['image_hovered'] = pygame.surface.Surface((100,100))
button01['image_hovered'].fill(GREEN)
#button01['image_hovered'] = pygame.image.load('button_100hovered.png')
#button01['image_hovered'] = pygame.transform.scale(button02['image_hovered'], (100, 100))
button01['image_clicked'] = pygame.surface.Surface((100,100))
button01['image_clicked'].fill(RED)
#button01['image_clicked'] = pygame.image.load('button_100clicked.png')
#button01['image_clicked'] = pygame.transform.scale(button02['image_clicked'], (100, 100))
button01['state'] = 'normal'
button01['image'] = button01['image_normal']
button01['rect'] = button01['image'].get_rect(center=(630, 400))

plus_image = dict()
plus_image['image'] = pygame.surface.Surface((30, 30))
plus_image['image'].fill(BLACK)
plus_image['rect'] = plus_image['image'].get_rect()
plus_image['state'] = 'hidden'
#screen.blit(buttonPlusImage, [random.randrange(560, 680), random.randrange(330, 450)])

# - other -

click_number = 0

# - mainloop -

mainloop = True
clock = pygame.time.Clock()

while mainloop:

    pygame.display.flip()
    clock.tick(FPS)
    screen.fill(WHITE)

    # draw elements

    screen.blit(button01['image'], button01['rect'])
    if plus_image['state'] == 'displayed':
        screen.blit(plus_image['image'], plus_image['rect'])

    # check collisions and clicks

    if not button01['rect'].collidepoint(pygame.mouse.get_pos()):
        button01['state'] = 'normal'
        button01['image'] = button01['image_normal']
    elif pygame.mouse.get_pressed()[0]:
        button01['state'] = 'clicked'
        button01['image'] = button01['image_clicked']
    else:
        button01['state'] = 'hovered'
        button01['image'] = button01['image_hovered']

    #if button01['state'] == 'clicked':
    #    plus_image['rect'].center = [random.randrange(560, 680), random.randrange(330, 450)]
    #    plus_image['state'] = 'displayed'
    #else:
    #    plus_image['state'] = 'hidden'

    # other events

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainloop = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                mainloop = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            #if button01['state'] == 'hovered':
            if button01['rect'].collidepoint(event.pos):
                click_number += 1
                print('click_number:', click_number)
                plus_image['rect'].center = [random.randrange(560, 680), random.randrange(330, 450)]
                plus_image['state'] = 'displayed'
                button01['rect'].center = [random.randrange(560, 680), random.randrange(330, 450)]
        if event.type == pygame.MOUSEBUTTONUP:
            plus_image['state'] = 'hidden'

# - end -            

pygame.quit()
quit()