gpt4 book ai didi

python - 用户单击时如何移动无框架pygame窗口?

转载 作者:太空宇宙 更新时间:2023-11-03 13:54:58 25 4
gpt4 key购买 nike

我想创建一个没有框架的 pygame 窗口,当用户点击它并移动鼠标时它会移动。我试过这个脚本,但是当我点击窗口时,打印的是“0”而不是“1”

我的脚本有问题。

# coding : utf-8
import pygame
from pygame.locals import *
from random import randint
from os import environ
from math import sqrt
pygame.init()

max_fps = 250

clock = pygame.time.Clock()
window_size_x, window_size_x = 720, 360

infos = pygame.display.Info()
environ['SDL_VIDEO_WINDOW_POS'] = str(int(infos.current_w / 2)) + ',' + str(int(infos.current_h / 2)) # center the window
screen = pygame.display.set_mode((window_size_x, window_size_x), pygame.NOFRAME)

def move_window(): # move the windows when custom bar is hold
window_x, window_y = eval(environ['SDL_VIDEO_WINDOW_POS'])
mouse_x, mouse_y = pygame.mouse.get_pos()
dist_x , dist_y = mouse_x - window_x, mouse_y - window_y # calculate the distance between mouse and window origin

for event in pygame.event.get():
if event.type != MOUSEBUTTONUP: # while bar is hold
print('1')
mouse_x, mouse_y = pygame.mouse.get_pos()
environ['SDL_VIDEO_WINDOW_POS'] = str(mouse_x - dist_x) + ',' + str(mouse_x - dist_x)
screen = pygame.display.set_mode((window_size_x, window_size_x), pygame.NOFRAME) # rebuild window

def main():
run = True
while run :
screen.fill((255, 255, 255))

pygame.display.update()
clock.tick(60) # build frame with 60 frame per second limitation

for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
print('0')
move_window()

if __name__ == '__main__':
main()

最佳答案

编写一个函数,根据先前的鼠标位置(start_xstart_y)和鼠标位置(new_x)移动窗口, new_y)

def move_window(start_x, start_y, new_x, new_y):
global window_size_x, window_size_y

window_x, window_y = eval(environ['SDL_VIDEO_WINDOW_POS'])
dist_x, dist_y = new_x - start_x, new_y - start_y
environ['SDL_VIDEO_WINDOW_POS'] = str(window_x + dist_x) + ',' + str(window_y + dist_y)

# Windows HACK
window_size_x += 1 if window_size_x % 2 == 0 else -1

screen = pygame.display.set_mode((window_size_x, window_size_y), pygame.NOFRAME)

在这个函数中有一行非常重要:

window_size_x += 1 if window_size_x % 2 == 0 else -1

此行将窗口的宽度交替改变 +1 和 -1。在 Windows 系统上似乎有一个错误,如果窗口的大小没有改变,它会忽略新的位置参数。
这个“hack”是一种解决方法,只要位置发生变化,它就会稍微改变窗口的大小。

另一种没有闪烁的方法可能如下所示。不过请注意,此版本速度明显较慢:

def move_window(start_x, start_y, new_x, new_y):
global window_size_x, window_size_y
buffer_screen = pygame.Surface((window_size_x, window_size_y))
buffer_screen.blit(pygame.display.get_surface(), pygame.display.get_surface().get_rect())

window_x, window_y = eval(environ['SDL_VIDEO_WINDOW_POS'])
dist_x, dist_y = new_x - start_x, new_y - start_y
environ['SDL_VIDEO_WINDOW_POS'] = str(window_x + dist_x) + ',' + str(window_y + dist_y)

window_size_x += 1 if window_size_x % 2 == 0 else -1

screen = pygame.display.set_mode((window_size_x, window_size_y), pygame.NOFRAME)
screen.blit(buffer_screen, buffer_screen.get_rect())
pygame.display.flip()

更改 MOUSEMOTIONMOUSEBUTTONUP 的位置:

def main():
run = True
pressed = False
start_pos = (0,0)
while run :

# [...]

for event in pygame.event.get():

if event.type == MOUSEBUTTONDOWN:
pressed = True
start_pos = pygame.mouse.get_pos()

elif event.type == MOUSEMOTION:
if pressed:
new_pos = pygame.mouse.get_pos()
move_window(*start_pos, *new_pos)
pygame.event.clear(pygame.MOUSEBUTTONUP)

elif event.type == MOUSEBUTTONUP:
pressed = False
new_pos = pygame.mouse.get_pos()
move_window(*start_pos, *new_pos)

完整示例程序:

# coding : utf-8
import pygame
from pygame.locals import *
from os import environ
pygame.init()

clock = pygame.time.Clock()
window_size_x, window_size_y = 720, 360

infos = pygame.display.Info()
environ['SDL_VIDEO_WINDOW_POS'] = str(int(infos.current_w/2)) + ',' + str(int(infos.current_h/2))
screen = pygame.display.set_mode((window_size_x, window_size_x), pygame.NOFRAME)

def move_window(start_x, start_y, new_x, new_y):
global window_size_x, window_size_y

window_x, window_y = eval(environ['SDL_VIDEO_WINDOW_POS'])
dist_x, dist_y = new_x - start_x, new_y - start_y
environ['SDL_VIDEO_WINDOW_POS'] = str(window_x + dist_x) + ',' + str(window_y + dist_y)

window_size_x += 1 if window_size_x % 2 == 0 else -1
screen = pygame.display.set_mode((window_size_x, window_size_y), pygame.NOFRAME)

def main():
run = True
pressed = False
start_pos = (0,0)
while run :
screen.fill((255, 255, 255))
pygame.display.update()
clock.tick(60)

for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False

if event.type == MOUSEBUTTONDOWN:
pressed = True
start_pos = pygame.mouse.get_pos()

elif event.type == MOUSEMOTION:
if pressed:
new_pos = pygame.mouse.get_pos()
move_window(*start_pos, *new_pos)
pygame.event.clear(pygame.MOUSEBUTTONUP)

elif event.type == MOUSEBUTTONUP:
pressed = False
new_pos = pygame.mouse.get_pos()
move_window(*start_pos, *new_pos)

if __name__ == '__main__':
main()

此解决方案不再适用于 Windows 系统和 Pygame 2.0。但是,可以使用 WINAPI 函数 MoveWindow 更改窗口的位置。 :

import pygame
from ctypes import windll

pygame.init()
screen = pygame.display.set_mode((400, 400), pygame.NOFRAME)
clock = pygame.time.Clock()

def moveWin(new_x, new_y):
hwnd = pygame.display.get_wm_info()['window']
w, h = pygame.display.get_surface().get_size()
windll.user32.MoveWindow(hwnd, new_x, new_y, w, h, False)

window_pos = [100, 100]
moveWin(*window_pos)

run = True
while run :
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
elif event.type == pygame.MOUSEMOTION:
if pygame.mouse.get_pressed()[0]:
window_pos[0] += event.rel[0]
window_pos[1] += event.rel[1]
moveWin(*window_pos)

screen.fill((255, 255, 255))
pygame.display.update()
clock.tick(60)

关于python - 用户单击时如何移动无框架pygame窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57674156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com