[Python] Why does item_cost not get subtracted from whiskers (my games form of currency)?

Discussão em 'Python' iniciado por Stack, Março 25, 2025.

  1. Stack

    Stack Membro Participativo

    Im currently working in Pygame, and when the item is pressed, it should compare the item_cost and the whiskers (a form of currency) first, then it should subtract them if it is less - however, even when I have given myself 30 whiskers and the cost of the item is 5, it tells me I do not have enough whiskers.

    I believe that I have left in and included all of the code being used within the calling. If anyone could help and explain - That would be appreciated.

    class Inventory:
    #holds items
    def __init__(self, defense, x, y, img, item_cost):
    self.x = x
    self.y = y
    self.width = img.get_width()
    self.height = img.get_height()
    self.item_cost = item_cost
    self.buttons = []
    self.items = 0
    self.bg = pygame.image.load('inventory.png').convert_alpha()
    self.font = pygame.font.SysFont("cherri.ttf", 25)
    self.defense = defense

    def add_slot(self, img, name):
    self.items += 1
    self.buttons.append(InventorySlot(self, img, name))
    print("works")

    def draw(self, Screen):
    Screen.blit(self.bg, (self.x - self.bg.get_width()/2 , self.y-10))

    def get_clicked(self, X, Y):
    for bu in self.buttons:
    if bu.click(X,Y):
    return bu.name, item_cost

    return None

    def update(self):
    for btn in self.buttons:
    btn.update()



    class Holder(Inventory):
    def __init__(self, x, y, img):
    self.x = x
    self.y = y
    self.width = img.get_width()
    self.height = img.get_height()
    self.hold = []
    self.items = 0
    self.bg = img
    self.buttons = []
    self.font = pygame.font.SysFont("Mulan.ttf", 25)

    def add_slot(self, img, name, cost):
    self.items += 1
    slotx = self.x - 40
    sloty = self.y-150 + (self.items-1)*100
    self.hold.append(HolderSlot(slotx, sloty, img, name, cost))

    def get_item_cost(self, name):
    for hld in self.hold:
    if hld.name == name:
    return hld.cost
    return -1

    def draw(self, screen):
    global whiskersimg
    screen.blit(self.bg, (self.x - self.bg.get_width()/2 - 100, self.y-120))
    for item in self.hold:
    item.draw(screen)
    screen.blit(whiskersimg, (item.x-40, item.y + item.height))
    text = self.font.render(str(item.cost), 1, (255,255,255))
    screen.blit(text, (item.x + item.width/2 - text.get_width()/2 - 40, item.y + item.height - 100))

    class Game(): def __init__(self):
    global browncat, browncat, whitecat, gingercat, tabbycat, whitestripecat, buyblack,buybrown,buywhite,buytabby,buyginger,buywhitestripe
    global ScreenWidth, ScreenHeight, objectWidth, objectHeight, Screen, v, x, y, Playing, Shopping, development, pause, Instructions, whiskers, TabbyCat, WhiteCat, GingerCat, BrownCat, BlackCat, WhiteStripeCat, tilesize, FPS, screen
    pygame.init()
    self.selected_defense = None #no defense selected
    self.defense = [] #array of defense
    self.Inventory = Holder(ScreenWidth - gingercat.get_width() + 70, 250, gingercat)
    self.screen = Screen
    self.selectdefense = None
    self.attack_towers = []
    self.support_towers = []

    [...]

    def playing(self):
    global Playing
    global whiskers
    global development
    global pause
    global x
    global y
    global TabbyCat # Add this line
    global WhiteCat # Add this line
    global GingerCat # Add this line
    global BlackCat # Add this line
    global BrownCat # Add this line
    global WhiteStripeCat # Add this line

    [...]

    while Playing == True:
    [...]
    if pause == False and development == False:
    pygame.time.delay(10)
    Screen.fill((0))
    self.Background(self.mapim) # moved background
    gamegrid = Grid(30, 44, 40) # grid create
    gamegrid.draw(Screen) # draw the grid

    self.charactergroup.update() # update
    self.charactergroup.draw(Screen) # draw the enemyim on screen
    timer.update()
    timer.output(Screen) # output not draw
    whisker.output(Screen, Screen) # outputs whisker
    self.new()
    self.update()
    #self.all_sprites.draw(Screen)
    #for df in self.defense:
    #df.draw(self.Screen)
    self.Inventory.add_slot(buytabby, "Buy Tabby Cat", 5)
    self.Inventory.add_slot(buyginger, "Buy Ginger cat", 10)
    self.Inventory.add_slot(buywhite, "Buy White Cat", 15)
    self.Inventory.add_slot(buyblack, "Buy Black Cat", 20)
    self.Inventory.add_slot(buybrown, "Buy Brown Cat", 35)
    self.Inventory.add_slot(buywhitestripe, "Buy White Striped Cat", 50)
    self.Inventory.draw(self.screen)
    pos = pygame.mouse.get_pos()
    defclick = self.Inventory.get_clicked(pos[0], pos[1])
    pygame.display.update()



    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    Playing = False
    if event.type == pygame.KEYDOWN:
    if event.key == K_d: # Press 'd' to enter development mode
    development = True

    elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
    mouse_pos = pygame.mouse.get_pos()
    cat = Cats(cursor_cats, mouse_pos)
    cats_group.add(cat)

    if event.type == pygame.KEYDOWN and event.key == pygame.K_i: # i opens inventory
    Inventory()

    if event.type == pygame.KEYDOWN:
    if event.key == K_p:
    pause = True

    if event.type == pygame.MOUSEBUTTONDOWN:
    if event.button == 1: #check left button pressed
    if defclick:
    cost = self.Inventory.get_item_cost(defclick) #checks prices against whiskers
    print("touched")
    if whiskers >= cost: #prices = great
    whiskers -= cost #take cost from whiskers
    self.add_defense(defclick)
    else:
    print("Not enough whiskers.")



    pygame.display.update()

    Continue reading...

Compartilhe esta Página