制作一个可拖动的黑盒子

2024-10-02 10:29:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图做一个黑盒子,我可以点击和拖动,但我似乎被卡住了。有人能帮我看看哪里出了问题吗?你知道吗

def main_game():
    import pygame
    import math
    from time import *

    pygame.init()

    displayWidth = 268
    displayHeight = 552

    cyan = (0,255,255)
    red = (255,0,0)
    dark_red = (150,0,0)
    black = (0,0,0)
    grey = (200,200,200)
    dark_cyan = (0,230,230)

    my_rect = pygame.Rect([100,100,100,100])

    gameDisplay = pygame.display.set_mode((displayWidth,displayHeight))
    pygame.display.set_caption('Test')

    pygame.display.update()

    gameExit = False

主游戏循环:

    while not gameExit:
        for event in pygame.event.get():
            print event
            gameDisplay.fill(grey)

            pygame.draw.rect(gameDisplay, black, my_rect)

            if event.type == pygame.QUIT:
                ExitFunction()

            pygame.display.update()
            if event.type == pygame.MOUSEBUTTONDOWN:
                is_inside = my_rect.collidepoint(pygame.mouse.get_pos())

                if is_inside == 1:
                    gameExit = True
                    DragScreen()

这是我徒劳的尝试之一,我创建了一个新函数,试图将矩形定位到鼠标的坐标。你知道吗

def DragScreen():
    import pygame
    from time import *

    pygame.init()

    displayWidth = 268
    displayHeight = 552

    cyan = (0,255,255)
    red = (255,0,0)
    dark_red = (150,0,0)
    black = (0,0,0)
    grey = (200,200,200)
    dark_cyan = (0,230,230)

    gameDisplay = pygame.display.set_mode((displayWidth, displayHeight))
    pygame.display.set_caption('Test')

    pygame.display.update()

    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            print event
            gameDisplay.fill(grey)

            pygame.draw.rect(gameDisplay, black, pygame.mouse.get_pos())

            if event.type == pygame.QUIT:
                ExitFunction()

我将添加一个pygame.MOUSEBUTTONUP按钮事件停止拖动长方体

            pygame.display.update()

我创建了这个函数,只要在调用它时退出即可(工作正常)。你知道吗

def ExitFunction():
    import pygame
    from time import*

    pygame.init()

    pygame.quit()
    quit()


    pygame.quit()
    quit()
main_game()

Tags: rectimporteventdisplayredpygamegreyblack
1条回答
网友
1楼 · 发布于 2024-10-02 10:29:08

所以基本上,你可以测试点击对象。并将对象移动到鼠标位置:

################################################################################
# Imports ######################################################################
################################################################################

from pygame.locals import *
import pygame, os, sys

################################################################################
# Screen Setup #################################################################
################################################################################

pygame.init()
scr = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Box Test')

################################################################################
# Make Square ##################################################################
################################################################################

surf = pygame.Surface((64, 64)); surf.fill((255, 0, 0))
rect = surf.get_rect(); rect.center = (320, 240)

################################################################################
# Game Loop ####################################################################
################################################################################

while True:
    pygame.display.update()
    scr.fill((0, 0, 0))
    scr.blit(surf, rect)

    if pygame.mouse.get_pressed()[0]:
        if rect.collidepoint(pygame.mouse.get_pos()):
            rect.center = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

################################################################################
################################################################################
################################################################################

相关问题 更多 >

    热门问题