Pygame 不会在鼠标位置呈现矩形?
Pygame won't render rect at mouse position?
我写了这个简单的代码,当单击鼠标按钮时在鼠标位置生成一个矩形。但由于某种原因它不会渲染矩形。单击时打印“mouse”,但没有 rect!
import pygame
from pygame.locals import *
WHITE = (255,255,255)
while True:
pygame.init()
DISPLAY = pygame.display.set_mode((400,400))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
if event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.quit()
if event.type == MOUSEBUTTONDOWN:
pygame.draw.rect(DISPLAY,WHITE,(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1],20,20))
print('mouse')
pygame.display.update()
非常感谢任何形式的帮助!
您实际上确实渲染了矩形!问题是您重新初始化 window 的速度太快,以至于您在 while 循环
中看不到它
此代码有效:
import pygame
from pygame.locals import *
WHITE = (255, 255, 255)
pygame.init()
DISPLAY = pygame.display.set_mode((400, 400))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
if event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.quit()
if event.type == MOUSEBUTTONDOWN:
pygame.draw.rect(DISPLAY, WHITE, (pygame.mouse.get_pos()[
0], pygame.mouse.get_pos()[1], 20, 20))
print('mouse')
pygame.display.update()
您必须在应用程序循环之前初始化 pygame (pygame.init()
) and create the display surface (pygame.display.set_mode()
)。
import pygame
from pygame.locals import *
pygame.init()
DISPLAY = pygame.display.set_mode((400,400))
WHITE = (255,255,255)
run = True
while run:
for event in pygame.event.get():
if event.type == QUIT:
run = False
if event.type == KEYDOWN and event.key == K_ESCAPE:
run = False
if event.type == MOUSEBUTTONDOWN:
print('mouse')
pygame.draw.rect(DISPLAY, WHITE, (*event.pos, 20, 20))
pygame.display.update()
pygame.quit()
exit()
但是,典型的 PyGame 应用程序循环必须:
- 通过调用
pygame.event.pump()
or pygame.event.get()
. 来处理事件
- 根据输入事件和时间(分别为帧)更新对象的游戏状态和位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象)
- 通过调用
pygame.display.update()
or pygame.display.flip()
更新显示
- 限制每秒帧数以限制 CPU 使用
pygame.time.Clock.tick
重绘场景是每一帧。单击鼠标按钮时,该事件仅发生一次。将鼠标点击位置添加到列表中,并在应用程序循环中在列表中存储的位置处绘制矩形:
import pygame
from pygame.locals import *
pygame.init()
DISPLAY = pygame.display.set_mode((400,400))
WHITE = (255,255,255)
clock = pygame.time.Clock()
pos_list = []
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
run = False
if event.type == KEYDOWN and event.key == K_ESCAPE:
run = False
if event.type == MOUSEBUTTONDOWN:
print('mouse')
pos_list.append(event.pos)
DISPLAY.fill(0)
for pos in pos_list:
pygame.draw.rect(DISPLAY, WHITE, (*pos, 20, 20))
pygame.display.update()
pygame.quit()
exit()
问题是您在 while 循环中初始化 window。
你必须这样做:
import pygame
from pygame.locals import *
WHITE = (255,255,255)
pygame.init()
DISPLAY = pygame.display.set_mode((400,400))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
if event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.quit()
if event.type == MOUSEBUTTONDOWN:
pygame.draw.rect(DISPLAY,WHITE,(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1],20,20))
print('mouse')
pygame.display.update()
我写了这个简单的代码,当单击鼠标按钮时在鼠标位置生成一个矩形。但由于某种原因它不会渲染矩形。单击时打印“mouse”,但没有 rect!
import pygame
from pygame.locals import *
WHITE = (255,255,255)
while True:
pygame.init()
DISPLAY = pygame.display.set_mode((400,400))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
if event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.quit()
if event.type == MOUSEBUTTONDOWN:
pygame.draw.rect(DISPLAY,WHITE,(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1],20,20))
print('mouse')
pygame.display.update()
非常感谢任何形式的帮助!
您实际上确实渲染了矩形!问题是您重新初始化 window 的速度太快,以至于您在 while 循环
中看不到它此代码有效:
import pygame
from pygame.locals import *
WHITE = (255, 255, 255)
pygame.init()
DISPLAY = pygame.display.set_mode((400, 400))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
if event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.quit()
if event.type == MOUSEBUTTONDOWN:
pygame.draw.rect(DISPLAY, WHITE, (pygame.mouse.get_pos()[
0], pygame.mouse.get_pos()[1], 20, 20))
print('mouse')
pygame.display.update()
您必须在应用程序循环之前初始化 pygame (pygame.init()
) and create the display surface (pygame.display.set_mode()
)。
import pygame
from pygame.locals import *
pygame.init()
DISPLAY = pygame.display.set_mode((400,400))
WHITE = (255,255,255)
run = True
while run:
for event in pygame.event.get():
if event.type == QUIT:
run = False
if event.type == KEYDOWN and event.key == K_ESCAPE:
run = False
if event.type == MOUSEBUTTONDOWN:
print('mouse')
pygame.draw.rect(DISPLAY, WHITE, (*event.pos, 20, 20))
pygame.display.update()
pygame.quit()
exit()
但是,典型的 PyGame 应用程序循环必须:
- 通过调用
pygame.event.pump()
orpygame.event.get()
. 来处理事件
- 根据输入事件和时间(分别为帧)更新对象的游戏状态和位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象) - 通过调用
pygame.display.update()
orpygame.display.flip()
更新显示
- 限制每秒帧数以限制 CPU 使用
pygame.time.Clock.tick
重绘场景是每一帧。单击鼠标按钮时,该事件仅发生一次。将鼠标点击位置添加到列表中,并在应用程序循环中在列表中存储的位置处绘制矩形:
import pygame
from pygame.locals import *
pygame.init()
DISPLAY = pygame.display.set_mode((400,400))
WHITE = (255,255,255)
clock = pygame.time.Clock()
pos_list = []
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
run = False
if event.type == KEYDOWN and event.key == K_ESCAPE:
run = False
if event.type == MOUSEBUTTONDOWN:
print('mouse')
pos_list.append(event.pos)
DISPLAY.fill(0)
for pos in pos_list:
pygame.draw.rect(DISPLAY, WHITE, (*pos, 20, 20))
pygame.display.update()
pygame.quit()
exit()
问题是您在 while 循环中初始化 window。
你必须这样做:
import pygame
from pygame.locals import *
WHITE = (255,255,255)
pygame.init()
DISPLAY = pygame.display.set_mode((400,400))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
if event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.quit()
if event.type == MOUSEBUTTONDOWN:
pygame.draw.rect(DISPLAY,WHITE,(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1],20,20))
print('mouse')
pygame.display.update()