pygame.MOUSEBUTTONDOWN pygame.VIDEORESIZE 错误?

发布于 2024-12-06 20:02:57 字数 1006 浏览 1 评论 0原文

几天前我发布了类似的问题,但我已经改进了问题: 当我在同一个事件循环中同时使用 MOUSEBUTTONDOWN 和 VIDEORESIZE 时,它不起作用...当我单击鼠标时,它还会打印仅在 MOUSEBUTTONUP 事件发生时才应该打印的语句...尝试一下,这个问题是杀了我..

import pygame
from pygame.locals import *
from sys import exit

LEFT = 1
size = 520,630
running = 1
screen = pygame.display.set_mode(size, RESIZABLE, 32)
#screen = pygame.display.set_mode((size))
while running:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      running = 0
    if event.type == pygame.VIDEORESIZE:
      size = event.size
      pygame.display.set_caption("Window resized to "+str(event.size)) 
    if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
       print "You pressed the left mouse button at (%d, %d)" % event.pos
    if event.type == pygame.MOUSEBUTTONUP and event.button == LEFT:
       print "You released the left mouse button at (%d, %d)" % event.pos

    screen = pygame.display.set_mode(size, RESIZABLE, 32)
    screen.fill((0, 0, 0))
    pygame.display.flip()

I posted a similar question a few days ago but i have refined the problem:
when i have both MOUSEBUTTONDOWN and VIDEORESIZE in the same event loop it doent work... when i click the mouse down it also prints the statement which should only get printed when the MOUSEBUTTONUP event occurs.... TRY it your selves this problem is killing me..

import pygame
from pygame.locals import *
from sys import exit

LEFT = 1
size = 520,630
running = 1
screen = pygame.display.set_mode(size, RESIZABLE, 32)
#screen = pygame.display.set_mode((size))
while running:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      running = 0
    if event.type == pygame.VIDEORESIZE:
      size = event.size
      pygame.display.set_caption("Window resized to "+str(event.size)) 
    if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
       print "You pressed the left mouse button at (%d, %d)" % event.pos
    if event.type == pygame.MOUSEBUTTONUP and event.button == LEFT:
       print "You released the left mouse button at (%d, %d)" % event.pos

    screen = pygame.display.set_mode(size, RESIZABLE, 32)
    screen.fill((0, 0, 0))
    pygame.display.flip()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山人契 2024-12-13 20:02:58

不要在每次事件时都更新屏幕,这会大大减慢您的程序速度。在我的计算机上它太慢了,我无法理解鼠标事件。只需移动几行代码即可修复该问题并且运行良好。

import pygame
from pygame.locals import *
from sys import exit

LEFT = 1
size = 520,630
running = 1
screen = pygame.display.set_mode(size, RESIZABLE, 32)
while running:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      running = 0
    if event.type == pygame.VIDEORESIZE:
      size = event.size
      pygame.display.set_caption("Window resized to "+str(event.size)) 
      screen.fill((0, 0, 0))
      pygame.display.flip()
    if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
       print "You pressed the left mouse button at (%d, %d)" % event.pos
    if event.type == pygame.MOUSEBUTTONUP and event.button == LEFT:
       print "You released the left mouse button at (%d, %d)" % event.pos

Don't update the screen at every event, it slows down your program considerably. It's so slow on my computer I can't make any sense of the mouse events. Just moving a couple of lines of your code fixes that and it runs fine.

import pygame
from pygame.locals import *
from sys import exit

LEFT = 1
size = 520,630
running = 1
screen = pygame.display.set_mode(size, RESIZABLE, 32)
while running:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      running = 0
    if event.type == pygame.VIDEORESIZE:
      size = event.size
      pygame.display.set_caption("Window resized to "+str(event.size)) 
      screen.fill((0, 0, 0))
      pygame.display.flip()
    if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
       print "You pressed the left mouse button at (%d, %d)" % event.pos
    if event.type == pygame.MOUSEBUTTONUP and event.button == LEFT:
       print "You released the left mouse button at (%d, %d)" % event.pos
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文