我正在开发一款简单的游戏。当我在循环中绘制某些东西时,它会将其绘制不到一秒钟。这是我的代码:
import pygame, sys
from pygame.locals import*
pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((1000, 750))
BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
pygame.display.set_caption('Game')
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
"do something"
else:
posx, posy = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONUP:
if posx == 500 and posy == 500:
DISPLAYSURF.fill((40, 40, 40))
pygame.draw.rect(DISPLAYSURF, (40, 40, 40), (posx, posy, 50, 20))
pygame.display.update()
我做错了什么?
答案 0 :(得分:1)
如果鼠标位于500,500,则仅更新显示,并触发事件MOUSEBUTTONUP。我会改为:
import pygame, sys
from pygame.locals import*
pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((1000, 750))
BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
pygame.display.set_caption('Game')
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
"do something"
else:
posx, posy = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONUP:
if posx == 500 and posy == 500:
DISPLAYSURF.fill((40, 40, 40))
pygame.draw.rect(DISPLAYSURF, (40, 40, 40), (posx, posy, 50, 20))
pygame.display.update()