Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.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-rect出现并立即消失_Python_Python 2.7_Pygame - Fatal编程技术网

Python Pygame-rect出现并立即消失

Python Pygame-rect出现并立即消失,python,python-2.7,pygame,Python,Python 2.7,Pygame,我正在改写我的问题 因为我觉得我没有说清楚 完整代码: #! /usr/bin/python import pygame, time, sys, random, os from pygame.locals import * from time import gmtime, strftime pygame.init() w = 640 h = 400 screen = pygame.display.set_mode((w, h),RESIZABLE) clock = pygame.time

我正在改写我的问题 因为我觉得我没有说清楚

完整代码:

#! /usr/bin/python

import pygame, time, sys, random, os
from pygame.locals import *
from time import gmtime, strftime

pygame.init()

w = 640
h = 400

screen = pygame.display.set_mode((w, h),RESIZABLE)
clock = pygame.time.Clock()
x = y = 100

def starting():
    basicfont = pygame.font.SysFont(None, 48)
    text = basicfont.render('Starting...', True, (255, 255, 255), (0, 0, 255))
    textrect = text.get_rect()
    textrect.centerx = screen.get_rect().centerx
    textrect.centery = screen.get_rect().centery
    screen.fill((0, 0, 255))
    screen.blit(text, textrect)
    pygame.display.update()

def taskbar():
    basicfont = pygame.font.SysFont(None, 24)
    pygame.draw.rect(screen, ((0, 255, 0)), (0, h-40, w, 40), 0)
    text = basicfont.render(strftime("%Y-%m-%d", gmtime()), True, (0, 0, 0))
    text2 = basicfont.render(strftime("%H:%M:%S", gmtime()), True, (0, 0, 0))
    screen.blit(text, (w - 100, h - 37))
    screen.blit(text2, (w - 100, h - 17))
    logoimage = pygame.image.load("C:\Users\chef\Desktop\logo.png").convert()
    logoimage = pygame.transform.scale(logoimage, (40, 40))
    screen.blit(logoimage, (0, h-40),)

def start():
    button1, button2, button3 = pygame.mouse.get_pressed()
    mousex, mousey = pygame.mouse.get_pos()
    if 0 < mousex < 40 and h-40 < mousey < h:
        if button1:
            pygame.draw.rect(screen, ((255, 0, 0)), (0, int(h/2), int(w/6), int(h/2)-40), 0)
    pygame.display.update()

starting()
#time.sleep(2)

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()  
        elif event.type==VIDEORESIZE:
            w = event.dict['size'][0]
            h = event.dict['size'][1]
            screen=pygame.display.set_mode(event.dict['size'],RESIZABLE)
    screen.fill((255, 255, 255))
    taskbar()
    start()
    pygame.display.update()
    clock.tick(60)
#/usr/bin/python
导入pygame、时间、系统、随机、操作系统
从pygame.locals导入*
从时间导入gmtime,strftime
pygame.init()
w=640
h=400
screen=pygame.display.set_模式((w,h),可调整大小)
clock=pygame.time.clock()
x=y=100
def starting():
basicfont=pygame.font.SysFont(无,48)
text=basicfont.render('Starting…',True,(255,255,255),(0,0,255))
textrect=text.get_rect()
textrect.centerx=屏幕。get_rect().centerx
textrect.centery=screen.get_rect().centery
屏幕填充((0,0,255))
screen.blit(text,textrect)
pygame.display.update()
def任务栏():
basicfont=pygame.font.SysFont(无,24)
pygame.draw.rect(屏幕,((0,255,0)),(0,h-40,w,40),0)
text=basicfont.render(strftime(“%Y-%m-%d”,gmtime()),True,(0,0,0))
text2=basicfont.render(strftime(“%H:%M:%S”,gmtime()),True,(0,0,0))
屏幕显示(文本,(w-100,h-37))
屏幕显示(文本2,(w-100,h-17))
logoimage=pygame.image.load(“C:\Users\chef\Desktop\logo.png”).convert()
logoimage=pygame.transform.scale(logoimage,(40,40))
屏幕blit(logoimage,(0,h-40),)
def start():
button1,button2,button3=pygame.mouse.get_pressed()
mousex,mousey=pygame.mouse.get_pos()
如果0

就像我原来的问题一样,当我按下logoimage.png时,红色的矩形会弹出一秒钟,然后立即消失。如何使矩形保持不变,直到我在其外部按下按钮?

您需要标记一个已单击的布尔值。比如:

btnClicked = false

def start():
    button1, button2, button3 = pygame.mouse.get_pressed()
    mousex, mousey = pygame.mouse.get_pos()
    if button1:
        if 0 < mousex < 40 and h-40 < mousey < h:
            btnClicked = true
        else:
            btnClicked = false

    if btnClicked:
        pygame.draw.rect(screen, ((255, 0, 0)), (0, int(h/2), int(w/6), int(h/2)-40), 0)
    pygame.display.update()
btnClicked=false
def start():
button1,button2,button3=pygame.mouse.get_pressed()
mousex,mousey=pygame.mouse.get_pos()
如果按钮1:
如果0
您应该将
屏幕移出
循环。填充((255,255,255))
。这会造成问题,因为每次程序循环时,它都会将显示器涂成白色,并将之前的内容写入其中。解决此问题的一种方法是将
screen.fill((255,255,255))
移出循环,并将其置于
pygame.draw.rect
函数之前。这将在放置rect之前为屏幕上色,并且不会继续填充它。例如:

if button1:
            screen.fill((255, 255, 255))
            pygame.draw.rect(screen, ((255, 0, 0)), (0, int(h/2), int(w/6), int(h/2)-40), 0)
您也可以将其移动到其他位置,或者在填充窗口后,找到一种绘制每个循环的方法


希望这有帮助。

你应该编辑上一个问题,而不是问新问题。@JosepValls的可能重复项我知道这是我原来问题的重复项,我只是问了一个新问题,因为我认为新问题比编辑旧问题更好。