python pygame-如何创建具有多个图像的拖放? [英] python pygame - how to create a drag and drop with multiple images?

查看:79
本文介绍了python pygame-如何创建具有多个图像的拖放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在尝试使用pygame在python中创建一个拼图游戏.唯一的问题是我在创建带有多个图像的木板时遇到了麻烦,我可以在屏幕上拖动这些图像(不需要像一个难题,什么都只是一个简单的拖放操作).我正在从计算机上的本地文件夹加载图像.有人可以帮我吗?

So I've been trying to create a jigsaw puzzle using pygame in python.The only problem is that I'm having trouble creating the board with multiple images that i can drag along the screen (no need to connect the images like a puzzle what so ever just a simple drag and drop). I'm loading my images from a local folder on my computer. Can someone please help me?

这是我到目前为止尝试过的,但是图像从同一位置开始,并且pygame将它们全部识别为一张图像

This is what i have tried so far but the images start at the same postion and pygame recognize them all as one image

import socket
import os
import shutil
import pygame
from pygame.locals import*



def dragdrop(path):
    dirs = os.listdir(path)
    def load_image(name, colorkey=None):
        fullname=os.path.join("data", name)
        try:
            image=pygame.image.load(fullname)
        except pygame.error, message:
            print "Impossible de charger l'image:",name
            raise SystemExit, message
        image = image.convert()
        if colorkey is not None:
            if colorkey is -1:
                colorkey = image.get_at((0, 0))
            image.set_colorkey(colorkey, RLEACCEL)
        return image, image.get_rect()

    class Ichrom(pygame.sprite.Sprite):
        def __init__(self,image,initpos):
            pygame.sprite.Sprite.__init__(self)
            self.pos = initpos
            self.image, self.rect=image
            self.button=(0,0,0)#mouse buttons not pressed
            self.selected = 0
            self.mouseover=False
            self.focused=False
            print "init chrom at ",self.pos

        def rollover(self):
            """Test if the mouse fly over the chromosome 
            self.mouseover==True if mouse flying over the chrom,
            False if not"""
            mpos=pygame.mouse.get_pos()#mouseposition
            #test if mouse roll over the sprite
            if self.rect.collidepoint(mpos):
                self.mouseover=True
            else:
                self.mouseover=False
        def update(self):
            self.button=pygame.mouse.get_pressed()
            mpos = pygame.mouse.get_pos()
            self.selected=self.rect.collidepoint(mpos)
            #the mouse flies over a chromosome
            if (self.mouseover):
                #print "mouse pos:",mpos
                if self.button==(1,0,0):     # )  
                    pos = pygame.mouse.get_pos()
                    self.rect.midtop = pos

    def main():
        pygame.init()
        screen = pygame.display.set_mode((1000,1000))
        pygame.display.set_caption("Karyotyper")
        pygame.mouse.set_visible(True)

        background = pygame.Surface(screen.get_size())
        background = background.convert()
        background.fill((0, 0, 0))

        screen.blit(background,(0, 0))
        pygame.display.flip()


        """i1=load_image('C:/Users/Yonatan/Desktop/House.png', -1)
        i2=load_image('C:/Users/Yonatan/Desktop/US.jpeg', -1)
        i3=load_image('C:\Users\Yonatan\Desktop\imgres.jpg', -1)
        fuck= [i1, i2, i3]"""

        img=[]
        count=0
        for i in dirs:

            spr = Ichrom(load_image(path + "/" +i,-1),(count,count))
            count = count+30
            img.append(spr)

        allsprites = pygame.sprite.RenderPlain((fck))
        clock = pygame.time.Clock()

        while 1:
            clock.tick(60)
            for event in pygame.event.get():
                if event.type == QUIT:
                    return
                elif event.type == KEYDOWN and event.key == K_ESCAPE:
                    return
                if event.type ==pygame.MOUSEBUTTONDOWN:
                    #need to be modified to handle a list of chromosomes
                    for i in fck:
                        i.rollover()
            allsprites.update()
            screen.blit(background,(0,0))
            allsprites.draw(screen)
            pygame.display.flip()

    main()

推荐答案

您将一个位置传递给sprites,并将其存储在 self.pos 中,但是绘制了一个 Sprite 子画面群组使用 rect 属性:

You pass a position to your sprites and store it in self.pos, but to draw a Sprite a sprite group uses the rect attribute:

来自 pygame.sprite.Group.draw() :

将包含的Sprite绘制到Surface参数.这会将Sprite.image属性用作源表面,并将Sprite.rect属性用作位置.

Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect for the position.

这篇关于python pygame-如何创建具有多个图像的拖放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆