1

我是一名业余程序员,试图用 python 和 pygame 制作游戏(我知道,不是最好的,但我知道如何比 Java 和 C++ 更好地使用它)。我遇到了一个有趣的问题:使用屏幕坐标在屏幕上定位特定的精灵,然后将该精灵移动到不同的位置。
例如,我有一个物品栏,我正在编写某种方式让玩家将物品从物品栏中放下,然后放回二维游戏场,以便玩家稍后可以捡起它。
由于项目的数量会有所不同,因此将它们的所有坐标添加到一个列表并尝试以这种方式管理它是效率低下的。试图将不断变化的项目列表连接到现场的对象变得太混乱了。

我已经附上了我到目前为止的代码,以防万一。我在网上查看过,但没有找到有关如何执行此操作的任何建议,我发现的唯一其他解决方案是以某种方式找到使用的对象spritecollide()和一个不可见的精灵,但我需要帮助。

drop 功能是我现在正在研究的部分。pos()是 2 个项目的列表,xy是您在屏幕上单击的鼠标坐标。

创建库存的类:

import pygame
from testRect import *

class Inventory(pygame.sprite.Sprite):
    # Define colors
    white = (255,255,255)
    dgreen = (0,154,0)#useables
    dblue = (0,0,154)#throwables
    dred = (154,0,0)#holdables

    def __init__(self,surface,pos,grid,gridItems,handsUsed):
        for row in range(4):
        # Add an empty array that will hold each cell
        # in this row
        grid.append([])
        for column in range(4):
            grid[row].append(0) # Append a cell
        gridItems.append([])
        for column in range(4):
            gridItems[row].append(0) # Append a cell
        self.update(surface,pos,grid,gridItems,handsUsed)

    def update(self,surface,pos,grid,gridItems,handsUsed):
        width=40
        height=40
        margin=2
        #convert click location to grid squares
        column=pos[0] // (width+margin)-20
        row=pos[1] // (height+margin)-7
        #make grid selection (toggle if selected or not)
        if row >-1 and row <4 and column >-1 and column < 4:
            if grid[row][column] == 0 and handsUsed < 2:
                grid[row][column] = 1
                handsUsed += 1
            elif grid[row][column] == 1 and handsUsed > 0:
            grid[row][column] = 0
            handsUsed -= 1
        # Draw the grid and determine type of object
        for row in range(4):
            for column in range(4):
                color = self.white
                if grid[row][column] == 1 and gridItems[row][column] == 1:
                    color = self.dgreen
                elif grid[row][column] == 1 and gridItems[row][column] == 2:
                    color = self.dblue
                elif grid[row][column] == 1 and gridItems[row][column] == 3:
                    color = self.dred
                pygame.draw.rect(surface,color,[((margin+width)*column+margin)+840,((margin+height)*row+margin)+295,width,height])

        return handsUsed#return the variable so that the master handsUsed var will also update

    def drop(self,pos,handsUsed,grid,staticsprites):
        moved = False
        if pos[0] > 838 and pos[0] < 1011 and pos[1] > 491 and pos[1] < 538 and handsUsed > 0:
            width=40
            height=40
            margin=2
            row = 0
            column = 0
            while row < 4 and moved == False:
                if grid[row][column] == 1:
                    #items selected will set their coordinates to a space near the player.
                    #Will check to see if spot is open in this priority (high to low): below, above, left, right
                    #finds the item to be moved
                    itemSelectRow = row * (((height + margin)-7))+299
                    itemSelectColumn = (column * ((width + margin)+20))+845
                    collideList = pygame.sprite.spritecollide(testRect, staticsprites, False)
                    if collideList != None:
                        #will move the item to the location since nothing is in the way
                        print collideList
                    moved = True
                    break
                elif row < 4 and column < 3:
                    print "hi"
                    column += 1
                elif row < 4 and column >= 3:
                    column = 0
                    row += 1
                else:
                    break
        return handsUsed#return the variable so that the master handsUsed var will also update
4

2 回答 2

0

I think you just need someway to put the Item to the ground and as close to player as possible, and you need some way to try out possible positions to do the item dropping. Am I right?

I've one year python exp and a few exp in game programming.

Here is the thing:

game programming is not science, you might find an easier way to do this at first, and do it better when the game shapes up. I don't know how your game looks like, but I think how items scattered (may be you are thinking about Diablo2 Item dropping or something?) on the ground is not the sweet-spot of most games. you can just do collision tests with logical map matrix with simple x, y grid, instead of some compute-intense sprite collision test.

Make it simple enough so that you can handle it, you can remember what the code does after a few weeks(or years), too complex stuff hurts.

good luck and make a great game :)

于 2012-08-27T07:16:57.697 回答
0

好的,我以为您将物品拖到库存窗口之外,例如我的世界,但 nvm。

他们单击“放置”按钮,获取 player.position 并测试播放器周围的 8 个字段,如果您真的可以放入其中一个字段,当您找到一个空的地方时,您放置并添加项目及其位置到丢弃物品的列表。

class DroppedItem:
    def __init__(item_type,x,y):
        self.itype = item_type
        self.pos = [x,y]

#you have one list with all dropped items in the map
DROPPED = []
#Them, inside your function code when you drop the item:
DROPPED.append(DroppedItem(itype,x,y))
#itype is the type of item, best use IDs #1=swordx,2=bow of death,....33=holy arrow
#x and y is the position in the ground.

我建议您熟悉类和面向对象的编程 (OOP),因为它可以让您编写更清洁、更高效的代码。对不起我的英语不好。

于 2012-08-21T16:30:51.983 回答