简体   繁体   English

如何在pygame中在屏幕上拖动对象?

[英]How to drag an object around the screen in pygame?

I am making tower of hanoi with pygame and I am having a hard time getting the mouse to be able to drag a disk around the screen.我正在用 pygame 制作河内塔,但我很难让鼠标能够在屏幕上拖动磁盘。 I want each disk to have a collidepoint.我希望每个磁盘都有一个碰撞点。 I know that creating a rectangle this way pygame.rect.Rect(50, 80, 100, 100) works because that has a collide point, but I want to use my Disk class to create a disk and that disk should have a collide point.我知道以这种方式创建一个矩形 pygame.rect.Rect(50, 80, 100, 100) 因为它有一个碰撞点,但是我想使用我的 Disk 类来创建一个磁盘并且该磁盘应该有一个碰撞点.

tower_of_hanoi.py Tower_of_hanoi.py

import pygame
import game_objects
import settings


def main():
    screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
    screen.fill(settings.white)
    game_objects.game.run()


if __name__ == "__main__":
    main()

game.py游戏.py

import pygame
import game_objects
import settings
from peg import Peg
from disk import Disk

class Game:
    def __init__(self, fps):
        self.fps = fps

    def run(self):
        pygame.init()
        running = True
        disk1_drag = False
        clock = pygame.time.Clock()

        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False

                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        if game_objects.disk1.collidepoint(event.pos):
                            disk1_drag = True

                elif event.type == pygame.MOUSEBUTTONUP:
                    if event.button == 1:
                        disk1_drag = False

                elif event.type == pygame.MOUSEMOTION:
                    if disk1_drag:
                        mouse_x, mouse_y = event.pos
                        settings.disk1_x = mouse_x
                        settings.disk1_y = mouse_y

            Peg.draw_peg(game_objects.left_peg)
            Peg.draw_peg(game_objects.middle_peg)
            Peg.draw_peg(game_objects.right_peg)

            Disk.draw_disk(game_objects.disk1)
            Disk.draw_disk(game_objects.disk2)
            Disk.draw_disk(game_objects.disk3)

            pygame.display.update()
            clock.tick(self.fps)

disk.py磁盘文件

import pygame
import game_objects
import settings


def main():
    screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
    screen.fill(settings.white)
    game_objects.game.run()


if __name__ == "__main__":
    main()

peg.py peg.py

import pygame
import game_objects


class Peg:
    def __init__(self, x, y, width, height, color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color

    def draw_peg(self):
        pygame.draw.rect(game_objects.screen, self.color, (self.x, self.y, self.width, self.height))

game_objects.py游戏对象.py

import pygame
from settings import *
from game import Game
from peg import Peg
from disk import Disk

game = Game(fps)
screen = pygame.display.set_mode((screen_width, screen_height))

left_peg = Peg(left_peg_x, peg_y, peg_width, peg_height, brown)
middle_peg = Peg(middle_peg_x, peg_y, peg_width, peg_height, brown)
right_peg = Peg(right_peg_x, peg_y, peg_width, peg_height, brown)

disk1 = Disk(disk1_x, disk1_y, disk1_width, disk1_height, disk1_color)
disk2 = Disk(disk2_x, disk2_y, disk2_width, disk2_height, disk2_color)
disk3 = Disk(disk3_x, disk3_y, disk3_width, disk3_height, disk3_color)

settings.py设置.py

white = [255, 255, 255]
black = [0, 0, 0]
red = [255, 0, 0]
green = [0, 255, 0]
blue = [0, 0, 255]
brown = [118, 83, 42]

screen_width = 1000
screen_height = 600

fps = 60

peg_width = 10
peg_height = 400

peg_y = 200

left_peg_x = 200
middle_peg_x = 500
right_peg_x = 800

disk1_width = 50
disk1_height = 30

disk2_width = 100
disk2_height = 30

disk3_width = 150
disk3_height = 30

disk1_x = left_peg_x - disk1_width / 2 + peg_width / 2
disk1_y = screen_height - disk1_height - disk2_height - disk3_height

disk2_x = left_peg_x - disk2_width / 2 + peg_width / 2
disk2_y = screen_height - disk3_height - disk2_height

disk3_x = left_peg_x - disk3_width / 2 + peg_width / 2
disk3_y = screen_height - disk3_height

disk1_color = red
disk2_color = green
disk3_color = blue

You have to change the attributes of the object ( disk1.x , disk1.y ) rather than intilal settings ( disk1_x , disk1_y ) when you drag the object:拖动对象时,您必须更改对象的属性( disk1.xdisk1.y )而不是初始设置( disk1_xdisk1_y ):

class Game:
    # [...]

    def run(self):
        # [...]

        while running:
            for event in pygame.event.get():
                # [...]
  
                elif event.type == pygame.MOUSEMOTION:
                    if disk1_drag:
                        mouse_x, mouse_y = event.pos
                        disk1.x = mouse_x             # <----
                        disk1.y = mouse_y             # <----

Further more, you need to clear the display in every frame by pygame.Surface.fill :此外,您需要通过pygame.Surface.fill清除每一帧中的pygame.Surface.fill

class Game:
    # [...]

    def run(self):
        # [...]

        while running:
            # [...]

            screen.fill(white)       # <----
            Peg.draw_peg(left_peg)
            Peg.draw_peg(middle_peg)
            Peg.draw_peg(right_peg)

            Disk.draw_disk(disk1)
            Disk.draw_disk(disk2)
            Disk.draw_disk(disk3)

            pygame.display.update()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM