Pygame - 剪切到库存槽中的项目

时间:2016-03-13 12:26:35

标签: python python-2.7 user-interface pygame

使用pygame我可以通过用鼠标拖动精灵来简单地移动精灵。然而,试图让它们进入路线比我想象的更难。我有搜索并尝试自己,但我找不到一个好方法。

例如,物品或库存类需要什么功能才能实现这一目标?让我们说我想把一个药水从我的手槽插入我的库存槽中,当我将它们重新定位时它们就会落到位。

这可能是无关紧要的,但我正在尝试重建一个类似的库存系统,就像在魔法和魔法3中一样。

编辑:此代码不是我引以为荣的内容...现在它基本上是很多在屏幕上绘制的图片。 here是图片的链接。

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


display_width = 288
display_height = 224

white = (255, 255, 255)


pygame.init()


class Player(pygame.sprite.Sprite):
    def __init__(self):
        super(Player, self).__init__()

        self.name = 'test_001'
        self.race = 'human'

        # armour piece ['name', modifier]
        self.armour_head = ['armour', 'none', 0]
        self.armour_chest = ['armour', 'none', 0]
        self.armour_foot = ['armour', 'Shoes', 1]
        self.armour_leg = ['armour', 'pants', 1]
        self.armour_hand =  ['armour', 'none', 0]
        self.inventory = []

    def obtain_item(item_get):
        self.inventory.append(get_item)



class healthPotion:
    def __init__(self, x, y, health_modifier, mana_modifier):
        self.x = x
        self.y = y
        self.img = "Health_potion.png"
        self.health_modifier = health_modifier
        self.mana_modifier = mana_modifier
        self.load()

    def draw(self, screen):
        screen.blit(self.img, [self.x, self.y])

    def load(self):
        self.img = pygame.image.load(self.img)

class Inventory:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.img_back = "Backround_inv.png"
        self.img_box = "Inv_box.png"
        self.img_leftArrow = "Inv_arrow_left.png"
        self.img_RightArrow = "Inv_arrow_right.png"
        self.load()

    def load(self):
        self.img_back = pygame.image.load(self.img_back)
        self.img_box = pygame.image.load(self.img_box)
        self.img_leftArrow = pygame.image.load(self.img_leftArrow)
        self.img_RightArrow = pygame.image.load(self.img_RightArrow)

    def draw(self, screen):
        screen.blit(self.img_back, [self.x, self.y])
        screen.blit(self.img_box, [self.x + 124, self.y])
        screen.blit(self.img_box, [self.x + 16, self.y + 32])
        screen.blit(self.img_box, [self.x + 96, self.y + 32])
        screen.blit(self.img_box, [self.x + 124, self.y + 32])
        screen.blit(self.img_box, [self.x + 156, self.y + 32])
        screen.blit(self.img_box, [self.x, self.y + 64])
        screen.blit(self.img_box, [self.x + 32, self.y + 64])
        screen.blit(self.img_box, [self.x + 124, self.y + 64])
        screen.blit(self.img_box, [self.x, self.y + 96])
        screen.blit(self.img_box, [self.x + 32, self.y + 96])
        screen.blit(self.img_box, [self.x + 124, self.y + 96])
        screen.blit(self.img_box, [self.x, self.y + 124])
        screen.blit(self.img_box, [self.x + 32, self.y + 124])
        screen.blit(self.img_box, [self.x + 64, self.y + 192])
        screen.blit(self.img_box, [self.x + 96, self.y + 192])
        screen.blit(self.img_box, [self.x + 124, self.y + 192])
        screen.blit(self.img_box, [self.x + 156, self.y + 192])
        screen.blit(self.img_box, [self.x + 188, self.y + 192])
        screen.blit(self.img_box, [self.x + 220, self.y + 192])

def main():

    inventory = Inventory(0, 0)
    potion_1 = healthPotion(0, 0, 10, 0)

    GameOn = True

    while GameOn:


        events = pygame.event.get()

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

        # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT     
        screen.fill(white)
        inventory.draw(screen)
        potion_1.draw(screen)


        # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT

        pygame.display.update()
    # Be IDLE friendly. If you forget this line, the program will 'hang'
    # on exit.
    pygame.quit()

pygame.init()
screen = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('INV')

main()

0 个答案:

没有答案