Pygame - 矩形出现并立即消失
Pygame - rect appears and immediately disappears
我正在重写我的问题
Pygame - rect disappears before I reach it因为我好像没有描述清楚
完整代码:
#! /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)
就像我原来的问题一样,当我按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()
您应该将 screen.fill((255, 255, 255))
移出 while
循环。这会导致问题,因为每次程序循环时,它都会将显示器显示为白色,覆盖之前的内容。解决此问题的一种方法是将 screen.fill((255, 255, 255))
移出循环并将其放在 pygame.draw.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)
您也可以将它移到别处,或者想出一种在填充 window.
后在每个循环中绘制矩形的方法
希望对您有所帮助。
我正在重写我的问题 Pygame - rect disappears before I reach it因为我好像没有描述清楚
完整代码:
#! /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)
就像我原来的问题一样,当我按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()
您应该将 screen.fill((255, 255, 255))
移出 while
循环。这会导致问题,因为每次程序循环时,它都会将显示器显示为白色,覆盖之前的内容。解决此问题的一种方法是将 screen.fill((255, 255, 255))
移出循环并将其放在 pygame.draw.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)
您也可以将它移到别处,或者想出一种在填充 window.
后在每个循环中绘制矩形的方法希望对您有所帮助。