Python Forum

Full Version: [pygame] Inventory items not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have this inventory for my game and I finally got buying items to work, but now any other items won't work. For example - I buy a weapon, now my other weapon does nothing when equipped. Also when trying to equip another weapon whilst one is already equipped, it is supposed to first ask the player to unequip the one currently equipped. TIA. game can be viewed/downloaded - https://github.com/Sheepposu/Destination - here
I'm sorry if I sound rude or impatient. I really want to work on this game but I can't figure out the problem. I've been trying and checking all kinds of stuff, but I don't know what I am doing wrong. Is anyone going to be able to help me? If not could someone possibly point me in the direction of somewhere where I can get help. Thank you!
Im not saying this is the cause of your issue. But it illustrates the problem of people helping you.

Quote:
    def drawWeap(self, i):
        gearBool = i.itemReturn()
        if self.direct == 'Left':
            if gearBool['armor'] == None:
                self.character = gD.blit(Character[3], (self.x, self.y))
            if gearBool['armor'] != None:
                self.character = gD.blit(Character[3], (self.x, self.y))
                self.armor = gD.blit(self.color[3], (self.x, self.y))
            if gearBool['weapon'] != None:
                self.weapon = gD.blit(self.weapType[3], (self.x - round(self.width*.8), self.y + round(self.height/2)))

This entire drawWeap() function is massively redundant. First of all there should be if and elif, not only if. Secondly you should split the logic of armor and weapons from the draw method. This alone would reduce the number of lines in your code as well make your code much more readable to new people looking at it. If you copy and paste a lot of code, then you are doing something wrong. And you are doing that.

Another example:
Sure of course it works, but it makes it a nightmare to help fix bugs. As an example...when i look at this i dont know if your issue is a draw issue or a logic issue. You do not use docstrings. You do not use modules. You are not using the builtin pygame rects (You should never have to do self.x/self.y in pygame with rects). You are using time.sleep (Which is always bad in GUI).

Im sure if i wanted to devote the time i could make your 1200 line code into 300~ lines. But i dont have the time or patience. Reducing it by that much would make it much more easy to work on. Less bugs. Easier to find bugs that do come along.

My suggestion would be to rework your code better. If your asking yourself "how do i do that?". I would read (or reread) the series of tutorials and follow along through part 1 through part 8. If you do that you should be thinking your code is in bad shape. And you would know tips on how to fix it. By doing all that more people would be willing to help. No one is ever going to look through thousands of lines of code, devote time in understanding your spaghetti code...without at least pay.

If you want to look at a good structure of RPG in python/pygame, check out Mekire's RPG Cabbages and kings. Check out how loading sprites are separated into a tools module. check out the player module. You never have redundant code, he labels every single method on what it does, each method does ONE thing.

EDIT:
Another suggestion is dont make us wait. Get us straight to the problem. Dont make use type username/passwords in. Dont make us kill the first guy. Put us straight at the shop. Programmers always have cheat arguments in development to progress users to the point of the problem....eliminating the need to start at the beginning.
Thank you for your feedback. I shall devote myself to improving my code. In the future I'll be sure to use the tips I learn in order not to code like this.
You said it is capable of being compacted down to 300~ lines. I got it to 1000, so can you give me some hints on what to fix. Code updated to https://github.com/Sheepposu/Destination...ination.py

Also typing Shop for the password make the player spawn at the shop
I have already stated the issues beforehand in other threads. Fixing those issues would reduce your code.

The first simple one to reduce code is your loading of images. A simple method of loading images since yours are all in one directory is to load all PNG's in that directory. Then use the filename as the dictionary key. A simple for loop would reduce 50 lines of hard coded image loads to just a few.

All your time sleeps everywhere add a single line on every function. It is also slowing your program down and glitching your GUI. During the half second, nothing happens. No logic and no drawing to the screen.

Your drawWeap method is redundant and can be reduced by splitting logic from drawing. This occurs in all of your classes.

Things like this
                if self.direct == 'Left':
                    WeapRect = self.weapType[3]
                    WeapRect = WeapRect.get_rect()
                    WeapRect[0] = self.rect[0] - round(self.width*.8)
                    WeapRect[1] = self.rect[1] + round(self.height/2)
                elif self.direct == 'Right':
                    WeapRect = self.weapType[1]
                    WeapRect = WeapRect.get_rect()
                    WeapRect[0] = self.rect[0] + round(self.width*.8)
                    WeapRect[1] = self.rect[1] + round(self.height/2)
                elif self.direct == 'Up':
                    WeapRect = self.weapType[0]
                    WeapRect = WeapRect.get_rect()
                    WeapRect[0] = self.rect[0] + round(self.width/2)
                    WeapRect[1] = self.rect[1] - round(self.height*.8)
                elif self.direct == 'Down':
                    WeapRect = self.weapType[2]
                    WeapRect = WeapRect.get_rect()
                    WeapRect[0] = self.rect[0] + round(self.width/2)
WeapRect[1] = self.rect[1] + round(self.height*.8)
You are copying code to apply to each direction. With dictionaries, you can do this once instead of copy and pasting for each direction. You do this quite often everywhere. Which would reduce your code by 75%.

I will leave it there. Because you do not have a good track record for applying what people tell you to change. And there is no point in me spending any more time looking at your code if you are not going to apply what i suggest.
Alright thanks, I was trying to apply the stuff you said earlier but I dind't quite understand it
In what way do you not understand it?
I understand it now, if I have any questions though, I will ask in this thread
Alright, I'm working on the direction thing and I have a question - I have a time.sleep after some of the buttons to make sure it doesn't get clicked twice. How would you suggest I make sure it isn't clicked twice
Pages: 1 2