我已经附上代码,我到目前为止,以防有帮助。我在网上查过了,但是没有找到任何关于如何做到这一点的建议,我找到的唯一的解决方案就是用spritecollide()
和一个看不见的精灵找到这个对象,但是我需要帮助。在
drop函数是我现在正在研究的部分。pos()
是一个由两个项目组成的列表,x
和{
创建库存的类:
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
好吧,他们,我还以为你把物品拖到了库存窗口之外,比如说minecraft,但是nvm。在
你点击“放下”按钮,你就会得到玩家位置测试玩家周围的8个区域,如果你真的可以在其中的一个字段中插入一个,当你找到一个空的地方时,你就把这个项目连同它的位置一起放到一个被丢弃的项目列表中。在
我建议你熟悉类和面向对象编程(OOP) 因为它可以让你写更干净高效的代码。 对不起,我的英语不好。在
我想你只需要找个方法把物品放在地上,尽可能靠近玩家,你需要一些方法来尝试一些可能的位置来完成物品的掉落。我说得对吗?在
我有一年的python经验和一些游戏编程经验。在
事情是这样的:
游戏编程不是一门科学,一开始你可能会找到一种更简单的方法来做,当游戏成型后,你会做得更好。我不知道你的游戏看起来如何,但我认为物品是如何散落的(可能你在考虑暗黑破坏神2的物品掉落或其他什么的?)地面不是大多数比赛的最佳场地。你可以用简单的x,y网格来做逻辑映射矩阵的碰撞测试,而不是一些计算密集的sprite碰撞测试。在
让它足够简单,这样你就可以处理它,你可以记住代码在几周(或几年)后做了什么,太复杂的东西会伤害你。在
祝你好运,玩一场精彩的游戏:)
相关问题 更多 >
编程相关推荐