Python Forum
drawing, moving, and collision problems (pygame)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
drawing, moving, and collision problems (pygame)
#11
I'll be sure to look into that; Also I am almost ready to post it to github here - https://github.com/Sheepposu/Destination - I just have to work on a few health issues

Ok, now my only problem is when I deal damage to the enemies. It deals damage before I am even near them. Also if I'm doing something wrong again and starting to make "spaghetti" code please point me in the right direction. I first learned pygame from sentdex's tutorials. One last thing, you said I should only ever have 1 "pygame.display.update()". is it ok if I put them in different parts. For example, one for the text in the star of the game, one in the Sign In section, one for the Confirmation section, and one for the game loop. Maybe more if i create different game_loops for different levels? BTW the code and everything is at github. one last thing, turns out I actually just saved all the progress i did on a seperate .py file somehow and then opened the one that was not the one I frst worked on. I'm an idiot.
Reply
#12
I'll just be here watching some more of my 9 hour tutorial to C++
Reply
#13
in pygame there are pygame rects. The weapon would have a rect made from the image, and the enemies would have a rect of their image. Then all you have to do is
if enemy.rect.colliderect(player.weapon.rect):
    enemy.take_damage()


This is explained in this tutorial for the player and this tutorial for enemy collision.
Recommended Tutorials:
Reply
#14
I knew it! I knew the answer would be some built in function for collision. *sigh* i always end up going the extra mile
Reply
#15
Ok, I used that "colliderect" which is way easier, but it still kills them from a far. As soon as i move and my weapon spawns in, they start taking damage. I first I was thinking maybe i set it to when the players sword collides with the player, it deals damage to the enemy, but i looked again and it wasn't mixed up. I couldn't find out what was wrong with it so i'm back.

Te code is in github
Reply
#16
It is really time consuming reading your code. And thus its hard to spot the problem. For example there should be no self.x or self.y if you are using rects. Pygame rects contain that data. You should be using rects for positioning and collision. You should never have to have a list such as Pos.

I also have no idea what this is suppose to be.
Quote:
    def getWeapRect(self):
        if self.weaponPos != None:
            if self.direct == 'Left':
                return self.weaponPos[0], self.weaponPos[1], self.weaponPos[0] + 50, self.weaponPos[1] + 20
            if self.direct == 'Right':
                return self.weaponPos[0], self.weaponPos[1], self.weaponPos[0] + 50, self.weaponPos[1] + 20
            if self.direct == 'Up':
                return self.weaponPos[0], self.weaponPos[1], self.weaponPos[0] + 20, self.weaponPos[1] + 50
            if self.direct == 'Down':
                return self.weaponPos[0], self.weaponPos[1], self.weaponPos[0] + 20, self.weaponPos[1] + 50
Any pygame image can return a rect of its size. You can do this with the charactes as well as the weapons.

image = pygame.image.load(IMAGE_NAME).convert()
image_rect = image.get_rect()
and can draw that too

screen.blit(image, image_rect)
and of course collision:
if image_rect.colliderect(enemy.weapon.rect)
Because you have assigned the rect yourself, I am assuming you messed the math up somewhere. That is why pygame has these methods. To simplify it. I would suggest you reread different pygame tutorials other than sentdex because his are pretty bad.

As a side note:
Quote:
        if self.x >= 0 and self.x  + self.width <= width and self.y >= 0 and self.y + self.height <= height:
            if keyed[pygame.K_LEFT] or keyed[pygame.K_a]:
                self.x -= 1
                self.direct = 'Left'
            if keyed[pygame.K_RIGHT] or keyed[pygame.K_d]:
                self.x += 1
                self.direct = 'Right'
            if keyed[pygame.K_UP] or keyed[pygame.K_w]:
                self.y -= 1
                self.direct = 'Up'
            if keyed[pygame.K_DOWN] or keyed[pygame.K_s]:
                self.y += 1
                self.direct = 'Down'
You can do this with the screen itself too

screen = display.set_mode((800,600))
screen_rect = screen.get_rect()
to check collision with the side of the screen and make adjustments.

also you do not need to load the same image more than once. Its a waste of resources and redundant coding. Check here for a title of rotation.
Recommended Tutorials:
Reply
#17
Ok i see, now I got it, I'll make some images for the players first though, then I'll wire everything up, gtg to school.
Reply
#18
Everything is now working, my new code that's working for me is in github. Next I'm going to input leveling up, different levels, and stats. I'll be back if I can't figure out why something isn't working
Reply
#19
Sorry the title is unspecific but I really couldn't think of what to type in there. Anyways my problem is that the game is ending too early. The level is supposed to complete when both monsters are defeated. If I kill monster 2, then 1, it works. If I do it in the opposite order it ends the game after killing the first monster. I spent a lot of time trying to figure out why it would do this but I didn't manage to find that. Thx in advance for your help. BTW everything is here - https://github.com/sheepposu/destination - if you need a visual of the game

Destination.py
import pygame
import random
import time
from DestinationFunc import Screen_Display as SD
from DestinationFunc import Button as BT
from pathlib import Path

"""
Goal 1 - Adventuring across borders of pygame.
Goal 2 - Use Krita ans get some more armor for enemies and players.
Goal 3 - Adding leveling and Exp
Goal 4 - Add upgrading stas with stat pts gained by leveling up
"""

width = 1000
height = 800
pygame.init()
gD = pygame.display.set_mode((width, height))

#Files
file = Path(__file__).parent
EnWoodenArmor = (pygame.image.load(str(file) + "\Destination Pics\EnWoodArmorUp.png"),
               pygame.image.load(str(file) + "\Destination Pics\EnWoodArmorRight.png"),
               pygame.image.load(str(file) + "\Destination Pics\EnWoodArmor.png"),
               pygame.image.load(str(file) + "\Destination Pics\EnWoodArmorLeft.png"))
EnDiamondArmor = (pygame.image.load(str(file) + "\Destination Pics\EnDiamondArmorUp.png"),
               pygame.image.load(str(file) + "\Destination Pics\EnDiamondArmorRight.png"),
               pygame.image.load(str(file) + "\Destination Pics\EnDiamondArmor.png"),
               pygame.image.load(str(file) + "\Destination Pics\EnDiamondArmorLeft.png"))
En2WoodenArmor = (pygame.image.load(str(file) + "\Destination Pics\En2WoodArmorUp.png"),
               pygame.image.load(str(file) + "\Destination Pics\En2WoodArmorRight.png"),
               pygame.image.load(str(file) + "\Destination Pics\En2WoodArmor.png"),
               pygame.image.load(str(file) + "\Destination Pics\En2WoodArmorLeft.png"))
En2DiamondArmor = (pygame.image.load(str(file) + "\Destination Pics\En2DiamondArmorUp.png"),
               pygame.image.load(str(file) + "\Destination Pics\En2DiamondArmorRight.png"),
               pygame.image.load(str(file) + "\Destination Pics\En2DiamondArmor.png"),
               pygame.image.load(str(file) + "\Destination Pics\En2DiamondArmorLeft.png"))
WoodenArmor = (pygame.image.load(str(file) + "\Destination Pics\WoodArmorUp.png"),
               pygame.image.load(str(file) + "\Destination Pics\WoodArmorRight.png"),
               pygame.image.load(str(file) + "\Destination Pics\WoodArmor.png"),
               pygame.image.load(str(file) + "\Destination Pics\WoodArmorLeft.png"))
DiamondArmor = (pygame.image.load(str(file) + "\Destination Pics\DiamondArmorUp.png"),
               pygame.image.load(str(file) + "\Destination Pics\DiamondArmorRight.png"),
               pygame.image.load(str(file) + "\Destination Pics\DiamondArmor.png"),
               pygame.image.load(str(file) + "\Destination Pics\DiamondArmorLeft.png"))
WoodBat = (pygame.image.load(str(file) + "\Destination Pics\WoodenBat.png"),
           pygame.image.load(str(file) + "\Destination Pics\WoodenBatRight.png"),
           pygame.image.load(str(file) + "\Destination Pics\WoodenBatDown.png"),
           pygame.image.load(str(file) + "\Destination Pics\WoodenBatLeft.png"))
SpikeBat = (pygame.image.load(str(file) + "\Destination Pics\Spiked_Bat.png"),
            pygame.image.load(str(file) + "\Destination Pics\Spiked_BatRight.png"),
            pygame.image.load(str(file) + "\Destination Pics\Spiked_BatDown.png"),
            pygame.image.load(str(file) + "\Destination Pics\Spiked_BatLeft.png"))
IronBat = (pygame.image.load(str(file) + "\Destination Pics\MetalBat.png"),
           pygame.image.load(str(file) + "\Destination Pics\MetalBatRight.png"),
           pygame.image.load(str(file) + "\Destination Pics\MetalBatDown.png"),
           pygame.image.load(str(file) + "\Destination Pics\MetalBatLeft.png"))
IronSword = (pygame.image.load(str(file) + "\Destination Pics\IronSword.png"),
             pygame.image.load(str(file) + "\Destination Pics\IronSwordRight.png"),
             pygame.image.load(str(file) + "\Destination Pics\IronSwordDown.png"),
             pygame.image.load(str(file) + "\Destination Pics\IronSwordLeft.png"))
DiamondSword = (pygame.image.load(str(file) + "\Destination Pics\DiamondSword.png"),
                pygame.image.load(str(file) + "\Destination Pics\DiamondSwordRight.png"),
                pygame.image.load(str(file) + "\Destination Pics\DiamondSwordDown.png"),
                pygame.image.load(str(file) + "\Destination Pics\DiamondSwordLeft.png"))

#Colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
darker_red = (200,0,0)
green = (0,255,0)
lightest_blue = (0,0,255)
lighter_blue = (0,0,200)
light_blue = (0,0,160)
blue = (0,0,120)
brown = (165,42,42)
light_brown = (139,69,19)
CustomColor1 = (48,150,140)
CustomColor2 = (36,112.5,105)
Unique_Color = (190,140,210)
ColorList = [black, red, green, blue, white, brown, lightest_blue, lighter_blue, light_blue]

pause = False

class GameData():
    def __init__(self):
        signInList = {'Sheepposu' : 'password', 'gg' : 'password', 'Tallish Walk' : '092404Aw'}
        UserArmor = {'Sheepposu' : DiamondArmor, 'gg' : WoodenArmor, 'Tallish Walk' : WoodenArmor}
        UserWeap = {'Sheepposu' : DiamondSword, 'gg' : WoodBat, 'Tallish Walk' : WoodBat}
        UserLvl = {'Sheepposu' : '1', 'gg' : '1', 'Tallish Walk' : '1'}
        UserDefense = {'Sheepposu' : 5, 'gg' : 1, 'Tallsih Walk' : 1}
        self.defense = UserDefense
        self.signInList = signInList
        self.UserArmor = UserArmor
        self.UserWeap = UserWeap
        self.UserLvl = UserLvl

    def getListings(self):
        return self.signInList, self.UserArmor, self.UserWeap, self.UserLvl, self.defense

        

#Config
fps = 100
pygame.display.set_caption('Destination')
clock = pygame.time.Clock()
gD.fill(blue)
clock.tick(15)






#Variables
Confirm = True
username = None

class Player():
    def __init__(self, x, y, width, height, color, weapType, defense):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.rect = (x, y, width, height)
        self.color = color
        self.armor = None
        self.direct = 'Down'
        self.weapon = None
        self.weaponPos = None
        self.weapType = weapType
        self.damageDealt = 0
        self.resist = defense
        self.damageTaken = 0
        self.levelComplete = False
        self.levelComplete2 = False
        if weapType == WoodBat:
            self.damage = .5

        elif weapType == DiamondSword:
            self.damage = 5
        
    def draw(self):
        self.drawWeap()
        
    def getDefense(self):
        return self.resist

    def getDamage(self):
        return self.damage

    def lvlCom(self):
        return self.levelComplete

    def lvlCom2(self):
        return self.levelComplete2

    def lvlReset(self):
        self.levelComplete = False

    def drawWeap(self):
        if self.direct == 'Left':
            self.armor = gD.blit(self.color[3], (self.x, self.y))
            self.weapon = gD.blit(self.weapType[3], (self.x - round(self.width*.8), self.y + round(self.height/2)))
            self.weaponPos = (self.x - round(self.width*.8), self.y + round(self.height/2))
        if self.direct == 'Right':
            self.armor = gD.blit(self.color[1], (self.x, self.y))
            self.weapon = gD.blit(self.weapType[1], (self.x + round(self.width*.8), self.y + round(self.height/2)))
            self.weaponPos = (self.x + round(self.width*.8), self.y + round(self.height/2))
        if self.direct == 'Up':
            self.armor = gD.blit(self.color[0], (self.x, self.y))
            self.weapon = gD.blit(self.weapType[0], (self.x + round(self.width/2), self.y - round(self.height*.8)))
            self.weaponPos = (self.x + round(self.width/2), self.y - round(self.height*.8))
        if self.direct == 'Down':
            self.armor = gD.blit(self.color[2], (self.x, self.y))
            self.weapon = gD.blit(self.weapType[2], (self.x + round(self.width/2), self.y + round(self.height*.8)))
            self.weaponPos = (self.x + round(self.width/2), self.y + round(self.height*.8))

        self.update()

    def getRect(self):
        if self.weaponPos != None:
            if self.direct == 'Left':
                colorRect = self.color[3].get_rect()
                colorRect[0] = self.x
                colorRect[1] = self.y
                return colorRect
            if self.direct == 'Right':
                colorRect = self.color[1].get_rect()
                colorRect[0] = self.x
                colorRect[1] = self.y
                return colorRect
            if self.direct == 'Up':
                colorRect = self.color[0].get_rect()
                colorRect[0] = self.x
                colorRect[1] = self.y
                return colorRect
            if self.direct == 'Down':
                colorRect = self.color[2].get_rect()
                colorRect[0] = self.x
                colorRect[1] = self.y
                return colorRect

    def collision(self, e):
        if self.weaponPos != None:
            Pos = e.getRect()
            if Pos != None:
                if self.direct == 'Left':
                    WeapRect = self.weapType[3]
                    WeapRect = WeapRect.get_rect()
                    WeapRect[0] = self.x - round(self.width*.8)
                    WeapRect[1] = self.y + round(self.height/2)
                if self.direct == 'Right':
                    WeapRect = self.weapType[1]
                    WeapRect = WeapRect.get_rect()
                    WeapRect[0] = self.x + round(self.width*.8)
                    WeapRect[1] = self.y + round(self.height/2)
                if self.direct == 'Up':
                    WeapRect = self.weapType[0]
                    WeapRect = WeapRect.get_rect()
                    WeapRect[0] = self.x + round(self.width/2)
                    WeapRect[1] = self.y - round(self.height*.8)
                if self.direct == 'Down':
                    WeapRect = self.weapType[2]
                    WeapRect = WeapRect.get_rect()
                    WeapRect[0] = self.x + round(self.width/2)
                    WeapRect[1] = self.y + round(self.height*.8)
                
                if Pos.colliderect(WeapRect):
                    e.damageTake(round(self.damage/e.getDefense(), 1))
            
    def move(self, e, e2=None):
        keyed = pygame.key.get_pressed()
        self.collision(e)
        ePos = e.getPos()
        posDiff = (self.x - ePos[0], self.y - ePos[1])
        death = e.checkDeath()
        if e2 != None:
            self.collision(e2)
            ePos2 = e2.getPos()
            posDiff2 = (self.x - ePos2[0], self.y - ePos2[1])
            death2 = e.checkDeath()

        if keyed[pygame.K_LEFT] or keyed[pygame.K_a]:
            self.x -= 1
            self.direct = 'Left'
        if keyed[pygame.K_RIGHT] or keyed[pygame.K_d]:
            self.x += 1
            self.direct = 'Right'
        if keyed[pygame.K_UP] or keyed[pygame.K_w]:
            self.y -= 1
            self.direct = 'Up'
        if keyed[pygame.K_DOWN] or keyed[pygame.K_s]:
            self.y += 1
            self.direct = 'Down'
        if death == False and death2 == False:
            if self.x <= 0:
                self.x += 5
                    
            if self.x + self.width >= width:
                self.x -= 5
            
            if self.y <= 0:
                self.y += 5
            
            if self.y + self.height >= height:
                self.y -= 5
        elif death == False and e2 == None:
            if self.x <= 0:
                self.x += 5
                    
            if self.x + self.width >= width:
                self.x -= 5
            
            if self.y <= 0:
                self.y += 5
            
            if self.y + self.height >= height:
                self.y -= 5

        elif death == True and death2 == True:
            if self.x <= 0:
                self.x = width - self.width
                self.levelComplete = True
                self.levelComplete2 = True   
            if self.x + self.width >= width:
                self.x = 0
                self.levelComplete = True
                self.levelComplete2 = True
            if self.y <= 0:
                self.y = height - self.height
                self.levelComplete = True
                self.levelComplete2 = True
            if self.y + self.height >= height:
                self.y = 0
                self.levelComplete = True
                self.levelComplete2 = True

        elif death == True and e2 == None:
            if self.x <= 0:
                self.x = width - self.width
                self.levelComplete = True
                self.levelComplete2 = True
            if self.x + self.width >= width:
                self.x = 0
                self.levelComplete = True
                self.levelComplete2 = True
            if self.y <= 0:
                self.y = height - self.height
                self.levelComplete = True
                self.levelComplete2 = True
            if self.y + self.height >= height:
                self.y = 0
                self.levelComplete = True
                self.levelComplete2 = True
        self.update()

    def update(self):
        self.rect = (self.x, self.y, self.width, self.height)

    def getPos(self):
        return self.x, self.y, self.x + self.width, self.y + self.height

    def getDamageDealt(self):
        return self.damageTaken

    def healthBar(self, num):
        pygame.draw.rect(gD, black, (self.rect[0], self.rect[1] - 20, self.rect[2], 10))
        pygame.draw.rect(gD, red, (self.rect[0] + 2, self.rect[1] - 18, self.rect[2] - num, 6))
        if self.rect[2] - num <= 0:
            End_Game(Died=True)

    def damageTake(self, damage=None, receive=None):
        if receive == None:
            self.damageTaken += damage
            self.healthBar(self.damageTaken)
        elif receive == True:
            return self.damageTaken
        elif receive == False:
            self.healthBar(self.damageTaken)

class Enemy():
    def __init__(self, x, y, width, height, color, weapType, diff):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.rect = (x, y, width, height)
        self.color = color
        self.direct = 'Down'
        self.weapon = None
        self.armor = None
        self.damageDealt = 0
        self.weapType = weapType
        self.weaponPos = None
        self.diff = diff
        self.dead = False
        self.damageTaken = 0
        if weapType == WoodBat:
            self.damage = .5
        if weapType == SpikeBat:
            self.damage = 1
        if diff == 'noob':
            self.resist = 1
        if diff == 'easy':
            self.resist = 2

    def getDefense(self):
        return self.resist
        
        
    def draw(self):
        if not self.dead:
            self.character()

    def character(self):
        if self.direct == 'Left':
            self.armor = gD.blit(self.color[1], (self.x, self.y))
            self.weapon = gD.blit(self.weapType[3], (self.x - round(self.width*.8), self.y + round(self.height/2)))
            self.weaponPos = (self.x - round(self.width*.8), self.y + round(self.height/2))
        if self.direct == 'Right':
            self.armor = gD.blit(self.color[3], (self.x, self.y))
            self.weapon = gD.blit(self.weapType[1], (self.x + round(self.width*.8), self.y + round(self.height/2)))
            self.weaponPos = (self.x + round(self.width*.8), self.y + round(self.height/2))
        if self.direct == 'Up':
            self.armor = gD.blit(self.color[0], (self.x, self.y))
            self.weapon = gD.blit(self.weapType[0], (self.x + round(self.width/2), self.y - round(self.height*.8)))
            self.weaponPos = (self.x + round(self.width/2), self.y - round(self.height*.8))
        if self.direct == 'Down':
            self.armor = gD.blit(self.color[2], (self.x, self.y))
            self.weapon = gD.blit(self.weapType[2], (self.x + round(self.width/2), self.y + round(self.height*.8)))
            self.weaponPos = (self.x + round(self.width/2), self.y + round(self.height*.8))

        self.update()

    def checkDeath(self):
        return self.dead

    def getRect(self):
        if self.weaponPos != None:
            if self.direct == 'Left':
                colorRect = self.color[3].get_rect()
                colorRect[0] = self.x
                colorRect[1] = self.y
                return colorRect
            if self.direct == 'Right':
                colorRect = self.color[1].get_rect()
                colorRect[0] = self.x
                colorRect[1] = self.y
                return colorRect
            if self.direct == 'Up':
                colorRect = self.color[0].get_rect()
                colorRect[0] = self.x
                colorRect[1] = self.y
                return colorRect
            if self.direct == 'Down':
                colorRect = self.color[2].get_rect()
                colorRect[0] = self.x
                colorRect[1] = self.y
                return colorRect

    def collision(self, p):
        if self.weaponPos != None:
            Pos = p.getRect()
            if Pos != None:
                if self.direct == 'Left':
                    WeapRect = self.weapType[3]
                    WeapRect = WeapRect.get_rect()
                    WeapRect[0] = self.x - round(self.width*.8)
                    WeapRect[1] = self.y + round(self.height/2)
                if self.direct == 'Right':
                    WeapRect = self.weapType[1]
                    WeapRect = WeapRect.get_rect()
                    WeapRect[0] = self.x + round(self.width*.8)
                    WeapRect[1] = self.y + round(self.height/2)
                if self.direct == 'Up':
                    WeapRect = self.weapType[0]
                    WeapRect = WeapRect.get_rect()
                    WeapRect[0] = self.x + round(self.width/2)
                    WeapRect[1] = self.y - round(self.height*.8)
                if self.direct == 'Down':
                    WeapRect = self.weapType[2]
                    WeapRect = WeapRect.get_rect()
                    WeapRect[0] = self.x + round(self.width/2)
                    WeapRect[1] = self.y + round(self.height*.8)

                if Pos.colliderect(WeapRect):
                    p.damageTake(round(self.damage/p.getDefense(), 1))

    def move(self, p):
        if not self.dead:
            plrPos = p.getPos()
            plrX = plrPos[0]
            plrY = plrPos[1]
            self.collision(p)
            DownRight = ['Down', 'Right']
            UpRight = ['Right', 'Up']
            UpLeft = ['Up', 'Left']
            DownLeft = ['Down', 'Left']
            AnyDirect = ['Down', 'Up', 'Left', 'Right']
            
            if plrX > self.x and plrY > self.y:
                self.x += .2
                self.y += .2
                if self.diff == 'noob':
                    num = random.randint(1, 200)
                elif self.diff == 'easy':
                    num = random.randint(1, 100)
                if num < 3:
                    self.direct = random.choice(DownRight)
            elif plrX > self.x and plrY < self.y:
                if self.diff == 'noob':
                    num = random.randint(1, 200)
                elif self.diff == 'easy':
                    num = random.randint(1, 100)
                if num < 3:
                    self.direct = random.choice(DownRight)
                self.x += .2
                self.y -= .2
            elif plrX < self.x and plrY < self.y:
                if self.diff == 'noob':
                    num = random.randint(1, 200)
                elif self.diff == 'easy':
                    num = random.randint(1, 100)
                if num < 3:
                    self.direct = random.choice(DownRight)
                self.x -= .2
                self.y -= .2
            elif plrX < self.x and plrY > self.y:
                if self.diff == 'noob':
                    num = random.randint(1, 200)
                elif self.diff == 'easy':
                    num = random.randint(1, 100)
                if num < 3:
                    self.direct = random.choice(DownRight)
                self.x -= .2
                self.y += .2
            elif plrX == self.x and plrY > self.y:
                if self.diff == 'noob':
                    num = random.randint(1, 200)
                elif self.diff == 'easy':
                    num = random.randint(1, 100)
                if num < 3:
                    self.direct = random.choice(DownRight)
                    self.direct = 'Down'
                self.y += .2
            elif plrX == self.x and plrY < self.y:
                if self.diff == 'noob':
                    num = random.randint(1, 200)
                elif self.diff == 'easy':
                    num = random.randint(1, 100)
                if num < 3:
                    self.direct = random.choice(DownRight)
                    self.direct = 'Up'
                self.y -= .2
            elif plrY == self.y and plrX > self.x:
                if self.diff == 'noob':
                    num = random.randint(1, 200)
                elif self.diff == 'easy':
                    num = random.randint(1, 100)
                if num < 3:
                    self.direct = random.choice(DownRight)
                    self.direct = 'Right'
                self.x += .2
            elif plrY == self.y and plrX < self.x:
                if self.diff == 'noob':
                    num = random.randint(1, 200)
                elif self.diff == 'easy':
                    num = random.randint(1, 100)
                if num < 3:
                    self.direct = random.choice(DownRight)
                    self.direct = 'Left'
                self.x -= .2
            elif plrX == self.x and plrY == self.y:
                if self.diff == 'noob':
                    num = random.randint(1, 200)
                elif self.diff == 'easy':
                    num = random.randint(1, 100)
                if num < 3:
                    self.direct = random.choice(DownRight)
                    self.direct = random.choice(AnyDirect)
                    
            self.update()

    def plrTele(self, x, y):
        self.x = x
        self.y = y

    def getPos(self):
        return self.x, self.y, self.x + self.width, self.y + self.height

    def getDamageDealt(self):
        return self.damageTaken

    def healthBar(self, num):
        if not self.dead:
            pygame.draw.rect(gD, black, (self.rect[0], self.rect[1] - 20, self.rect[2], 10))
            pygame.draw.rect(gD, red, (self.rect[0] + 2, self.rect[1] - 18, self.rect[2] - num, 6))
            if self.rect[2] - num <= 0:
                print('It\'s dead')
                self.dead = True

    def damageTake(self, damage=None, receive=None):
        if receive == None:
            self.damageTaken += damage
            self.healthBar(self.damageTaken)
        elif receive == True:
            return self.damageTaken
        elif receive == False:
            self.healthBar(self.damageTaken)

    def update(self):
        self.rect = (self.x, self.y, self.width, self.height)

    def getWeapPos(self):
        if self.weaponPos != None:
            return self.weaponPos


def Confirm_QUIT():
    global Confirm
    Confirm = False

def Confirm_Screen(username):
    global Confirm
    clock.tick(fps)
    while Confirm:
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                End_Game()
        gD.fill(blue)
        SD.message_display(gD, 'You\'re %s, correct?' %username, round(width/17), Unique_Color, round(width/2), height * .25)
        b1 = BT((round(width * .3), round(height * .7), round(width * .4), round(height * .2)),  'Confirm!', round(width/20), blue)
        b2 = BT((round(width * .75), round(height * .8), round(width * .2), round(height * .1)), "No!", round(width/50), blue)
        b1.draw(gD, CustomColor1)
        b2.draw(gD, red)
        b1.optClick(gD, CustomColor2, CustomColor1, Confirm_QUIT)
        b2.optClick(gD, darker_red, red, End_Game, pygame.quit)
        pygame.display.update()
        clock.tick(15)

def SignIn():
    global username
    GD = GameData()
    listings = GD.getListings()
    signInList = listings[0]
    
    username = input('(Type "new" for new account) Type in username: ')

    if username == 'new':
        newUsername = input('Type your Username: ')
        newPassword = input('Type your Password: ')
        signInList.update({newUsername : newPassword})
        UserArmor.update({newUsername : WoodenArmor})
        UserWeap.update({newUsername : WoodenBat})
        UserLvl.update({newUsername : '1'})
        username = newUsername
        print('Now return to Destination and confirm')

    else:
        if username in signInList:
            password = input('Please type your password: ')
            if password == signInList[username]:
                print('Now return to Destination and confirm')
            else:
                print('Invalid Password')
                time.sleep(2)
                SignIn()
        else:
            print('Invalid Username')
            time.sleep(2)
            SignIn()

def Main():
    global username
    global pause
    SignIn()
    Confirm_Screen(username)
    GD = GameData()
    listings = GD.getListings()
    UserArmor = listings[1]
    UserWeap = listings[2]
    UserDefense = listings[4]
    defense = UserDefense[username]
    armor = UserArmor[username]
    weap = UserWeap[username]
    
    p = Player(width/2, height/2, 50, 50, armor, weap, defense)
    e = Enemy(0 ,0, 50, 50, EnWoodenArmor, WoodBat, 'noob')
    e2 = Enemy(width - 50, height - 50, 50, 50, En2WoodenArmor, SpikeBat, 'easy')
    play = True
    while play:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                End_Game()

        if not pause:
            gD.fill(green)
            p.draw()
            e.draw()
            e2.draw()
            p.healthBar(p.getDamageDealt())
            e.damageTake(receive=False)
            e2.damageTake(receive=False)
            p.move(e, e2)
            p.move(e, e2)
            e.move(p)
            e2.move(p)
            Com = p.lvlCom()
            Com2 = p.lvlCom2()
            pygame.display.update()
            clock.tick(fps)
            if Com == True and Com2 == True:
                play = False
                
    End_Game()

def End_Game(Died=None):
    play = False
    GD = GameData()
    listings = GD.getListings()
    print(listings[0])
    print('')
    print(listings[1])
    print('')
    print(listings[2])
    print('')
    print(listings[3])
    OverPosY = 0
    if Died == None:
        gD.fill(red)
        SD.message_display(gD, 'Congratulations! You win', round(width/10), black, round(width/2), round(height/2))
        pygame.display.update()
    if Died:
        while True:
            OverPosY += 1
            gD.fill(red)
            SD.message_display(gD, 'Game Over', round(width/10), black, round(width/2), OverPosY)
            pygame.display.update()
            if OverPosY == height/2:
                time.sleep(5)
                pygame.quit()

SD.message_display(gD, "Fill in info on console", round(int(width/20)), Unique_Color, width/2, height/2)
pygame.display.update()
Main()
pygame.quit()
DestinationFunc.py
import pygame

class Screen_Display():
    def text_objects(text, font, color):
        textSurface = font.render(text, True, color)
        return textSurface, textSurface.get_rect()

    def message_display(gD, text, size, color, centerX, centerY):
        font = pygame.font.SysFont('arial', size)
        textSurf, TextRect = Screen_Display.text_objects(text, font, color)
        TextRect.center = ((centerX),(centerY))
        gD.blit(textSurf, TextRect)

class Button():
    def __init__(self, rect, text, textsize, textcolor):
        self.rect = rect
        self.font = pygame.font.SysFont('arial', textsize)
        self.textSurf, self.textRect = Screen_Display.text_objects(text, self.font, textcolor)
        self.textRect.center = ((rect[0] + (rect[2]/2), rect[1] + (rect[3]/2)))

    def draw(self, gD, ButColor):
        pygame.draw.rect(gD, ButColor, (self.rect))
        gD.blit(self.textSurf, self.textRect)

    def optClick(self, gD, ShadowColor, ButColor, command=None, command2=None):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if self.rect[0] + self.rect[2] > mouse[0] > self.rect[0] and self.rect[1] + self.rect[3] > mouse[1] > self.rect[1]:
            pygame.draw.rect(gD, ShadowColor, (self.rect))
            gD.blit(self.textSurf, self.textRect)
            if click[0] == 1:
                    if command != None:
                        command()
                    if command2 != None:
                        command2()
            else:
                pygame.draw.rect(gD, ButColor, (self.rect))
                gD.blit(self.textSurf, self.textRect)
Reply
#20
I forgot to mention another thing regarding play-ability.
Output:
metulburr@ubuntu:~/repos/Destination/Destination$ python3.6 Destination.py Traceback (most recent call last): File "Destination.py", line 21, in <module> EnWoodenArmor = (pygame.image.load(str(file) + "\Destination Pics\EnWoodArmorUp.png"), pygame.error: Couldn't open .\Destination Pics\EnWoodArmorUp.png
Windows uses \ and / as a separator, while linux only uses /. It would make it so much easier if you just used /

Or use:
>>> import os
>>> os.sep
'/'
Or better yet put the directory path in one variable, and combine it with the image.

WoodBat = pygame.image.load(os.path.join(image_path, 'WoodenBat.png'))
or even better yet load all the images in that directory at once and put them in a list with the name as the filename.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyGame] Problem with collision of player and enemy Nekotrooper 1 684 Dec-08-2023, 03:29 PM
Last Post: deanhystad
  pygame installation problems Gheryk 5 8,621 Nov-29-2023, 08:49 PM
Last Post: E_Mohamed
  can't get collision detection to work in platform game chairmanme0wme0w 10 3,810 Aug-19-2022, 03:51 PM
Last Post: deanhystad
  [PyGame] Problems with jump code in pygame Joningstone 4 5,377 Aug-23-2021, 08:23 PM
Last Post: deanhystad
  [PyGame] drawing images onto pygame window djwilson0495 1 3,489 Feb-22-2021, 05:39 PM
Last Post: nilamo
  [PyGame] Collision in not happening onizuka 3 3,413 Sep-07-2020, 11:30 AM
Last Post: metulburr
  [PyGame] No collision detection onizuka 6 3,666 Aug-18-2020, 01:29 PM
Last Post: onizuka
  [PyGame] pygame, help with making a function to detect collision between player and enemy. Kris1996 3 3,341 Mar-07-2020, 12:32 PM
Last Post: Kris1996
  Problem with collision detection... michael1789 4 3,274 Nov-12-2019, 07:49 PM
Last Post: michael1789
  Pygame sprite not moving michael1789 1 2,842 Nov-10-2019, 03:54 AM
Last Post: michael1789

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020