基于pygame中的屏幕坐标的对象位置

时间:2012-08-21 02:59:42

标签: python pygame game-engine

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

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

drop函数是我现在正在处理的部分。 pos()是包含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

2 个答案:

答案 0 :(得分: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) 因为它允许您编写更清晰,更有效的代码。 对不起我的英文不好。

答案 1 :(得分:0)

我认为你只需要将项目放在地上并尽可能靠近玩家,你需要一些方法来尝试可能的位置来完成项目的丢弃。我是对的吗?

我有一年的python exp和游戏编程中的一些exp。

事情就是这样:

游戏编程不是科学,你可能会首先找到一种更简单的方法来做到这一点,并且在游戏形成时做得更好。我不知道你的游戏是怎么样的,但是我觉得物品分散的情况(你可能正在考虑放弃Diablo2 Item或其他东西吗?)并不是大多数游戏的最佳选择。您可以使用简单的x,y网格进行碰撞测试,而不是使用一些计算密集的精灵碰撞测试。

让它变得简单以便你可以处理它,你可以记住代码在几周(或几年)之后所做的事情,太复杂的东西会伤害。

祝你好运并打出一场精彩的比赛:)