I have this game and so far so good... kinda. I added a second enemy and I noticed that when one takes damage, the other also does. The reason why is because the enemy class uses the players function to find out how much damage has been dealt by the player and then applies that to the health bar. Can someone help me to fix that and one more thing, the sword doesn't deal damage until I stab through to the other side of the enemy. I tried to fix that problem but I couldn't figure out what I was doing wrong. Thx in advance.
Problem 1 (Damage problem) - "healthBar()/getDamageDealt()"
Problem 2 (Coordinates to see if attack hit) - "takeHit()" (Executed in "move()" function)
DestinationFunc.py
Problem 1 (Damage problem) - "healthBar()/getDamageDealt()"
Problem 2 (Coordinates to see if attack hit) - "takeHit()" (Executed in "move()" function)
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)Destination.py
import pygame import random import time import threading from pygame.locals import * from DestinationFunc import Screen_Display as SD from DestinationFunc import Button as BT """ """ width = 1000 height = 800 pygame.init() gD = pygame.display.set_mode((width, height)) #Files WoodBat = (pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/WoodenBat.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/WoodenBatRight.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/WoodenBatDown.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/WoodenBatLeft.png")) SpikeBat = (pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/Spiked_Bat.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/Spiked_BatRight.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/Spiked_BatDown.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/Spiked_BatLeft.png")) IronBat = (pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/MetalBat.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/MetalBatRight.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/MetalBatDown.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/MetalBatLeft.png")) IronSword = (pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/IronSword.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/IronSwordRight.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/IronSwordDown.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/IronSwordLeft.png")) DiamondSword = (pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/DiamondSword.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/DiamondSwordRight.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/DiamondSwordDown.png"), pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/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' : 'rachl032078', 'gg' : 'rachl1979', 'Tallish Walk' : '092404Aw'} UserArmor = {'Sheepposu' : light_blue, 'gg' : brown, 'Tallish Walk' : brown} 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.direct = None self.weapon = None self.weaponPos = None self.weapType = weapType self.damageDealt = 0 self.resist = defense if weapType == WoodBat: self.damage = .5 elif weapType == DiamondSword: self.damage = 5 def draw(self): pygame.draw.rect(gD, self.color, (self.rect)) self.drawWeap() def getDefense(self): return self.resist def drawWeap(self): if self.direct == 'Left': 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.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.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.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 getWeapPos(self): if self.weaponPos != None: return self.weaponPos def move(self, e, e2=None): keyed = pygame.key.get_pressed() self.takeHit(e.getWeapPos(), self.direct, e) if e2 != None: self.takeHit(e2.getWeapPos(), self.direct, e2) 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' elif self.x <= 0: self.x += 5 elif self.x - self.width >= width: self.x -= 5 elif self.y <= 0: self.y += 5 elif self.y - self.height >= height: self.y -= 5 self.update() def update(self): self.rect = (self.x, self.y, self.width, self.height) def getPos(self): return self.x, self.y def getDamageDealt(self): return self.damageDealt def healthBar(self, num, num2): 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 - num2, 6)) if self.rect[2] - num == 0: End_Game(Died=True) def takeHit(self, weapPos, eDirect, e): if weapPos: defense = e.getDefense() EnPos = (self.x, self.y, self.x + self.width, self.y + self.height) if eDirect == 'Right': if weapPos[0] + 50 >= float(EnPos[0]) and weapPos[0] + 50 <= float(EnPos[2]) and weapPos[1] >= float(EnPos[1]) and weapPos[1] <= float(EnPos[3]): if self.damageDealt + round(self.damage/defense) > 0: self.damageDealt += round(self.damage/defense) if eDirect == 'Left': if weapPos[0] >= float(EnPos[2]) and weapPos[0] <= float(EnPos[0]) and weapPos[1] >= float(EnPos[1]) and weapPos[1] <= float(EnPos[3]): if self.damageDealt + round(self.damage/defense) > 0: self.damageDealt += round(self.damage/defense) if eDirect == 'Up': if weapPos[0] >= float(EnPos[0]) and weapPos[0] <= float(EnPos[2]) and weapPos[1] <= float(EnPos[3]) and weapPos[1] >= float(EnPos[1]): if self.damageDealt + round(self.damage/defense) > 0: self.damageDealt += round(self.damage/defense) if eDirect == 'Down': if weapPos[0] >= float(EnPos[0]) and weapPos[0] <= float(EnPos[2]) and weapPos[1] + 50 >= float(EnPos[1]) and weapPos[1] <= float(EnPos[3]): if self.damageDealt + round(self.damage/defense) > 0: self.damageDealt += round(self.damage/defense) 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 = None self.weapon = None self.damageDealt = 0 self.weapType = weapType self.weaponPos = None self.diff = diff self.dead = False 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: pygame.draw.rect(gD, self.color, (self.rect)) self.drawWeap() def drawWeap(self): if not self.dead: if self.direct == 'Left': 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.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.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.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 move(self, p): if not self.dead: plrPos = p.getPos() plrX = plrPos[0] plrY = plrPos[1] self.takeHit(p.getWeapPos(), self.direct, 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 getDamageDealt(self): return self.damageDealt 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 update(self): self.rect = (self.x, self.y, self.width, self.height) def takeHit(self, weapPos, pDirect, p): if weapPos: defense = p.getDefense() EnPos = (self.x, self.y, self.x + self.width, self.y + self.height) if pDirect == 'Right': if weapPos[0] + 50 >= float(EnPos[0]) and weapPos[0] + 50 <= float(EnPos[2]) and weapPos[1] >= float(EnPos[1]) and weapPos[1] <= float(EnPos[3]): if self.damageDealt + round(self.damage/defense) > 0: self.damageDealt += round(self.damage/defense) if pDirect == 'Left': if weapPos[0] <= float(EnPos[2]) and weapPos[0] >= float(EnPos[0]) and weapPos[1] >= float(EnPos[1]) and weapPos[1] <= float(EnPos[3]): if self.damageDealt + round(self.damage/defense) > 0: self.damageDealt += round(self.damage/defense) if pDirect == 'Up': if weapPos[0] >= float(EnPos[0]) and weapPos[0] <= float(EnPos[2]) and weapPos[1] <= float(EnPos[3]) and weapPos[1] >= float(EnPos[1]): if self.damageDealt + round(self.damage/defense) > 0: self.damageDealt += round(self.damage/defense) if pDirect == 'Down': if weapPos[0] >= float(EnPos[0]) and weapPos[0] <= float(EnPos[2]) and weapPos[1] + 50 >= float(EnPos[1]) and weapPos[1] <= float(EnPos[3]): if self.damageDealt + round(self.damage/defense) > 0: self.damageDealt += round(self.damage/defense) def getWeapPos(self): if self.weaponPos != None: return self.weaponPos def Pause(): global pause for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_p: pause = True SD.message_display(gD, 'Paused', round(width/10), black, width/2, height/2) while pause: print('Pause = True') for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_p: pause = False 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 : brown}) UserWeap.update({newUsername : light_brown}) UserLvl.update({newUsername : '1'}) username = newUsername else: if username in signInList: password = input('Please type your password: ') if password == signInList[username]: pass 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, brown, WoodBat, 'noob') e2 = Enemy(width - 50, height - 50, 50, 50, (80, 80, 80), 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) plrPos = p.getPos() eDamage = e.getDamageDealt() e2Damage = e2.getDamageDealt() pDamage = p.getDamageDealt() t1 = threading.Thread(target=p.draw) t2 = threading.Thread(target=p.move, args=(e,)) t3 = threading.Thread(target=p.healthBar, args=(4 + eDamage, e2Damage)) t4 = threading.Thread(target=e.draw) t5 = threading.Thread(target=e.move, args=(p,)) t6 = threading.Thread(target=e.healthBar, args=(4 + pDamage,)) t8 = threading.Thread(target=e2.draw) t9 = threading.Thread(target=e2.move, args=(p,)) t10 = threading.Thread(target=e2.healthBar, args=(4 + pDamage,)) t7 = threading.Thread(target=Pause) t1.start() t2.start() t3.start() t4.start() t5.start() t6.start() t7.start() t8.start() t9.start() t10.start() t1.join() t2.join() t3.join() t4.join() t5.join() t6.join() t7.join() t8.join() t9.join() t10.join() pygame.display.update() clock.tick(fps) 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) 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()