drawing, moving, and collision problems (pygame) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: drawing, moving, and collision problems (pygame) (/thread-17509.html) |
drawing, moving, and collision problems (pygame) - SheeppOSU - Apr-14-2019 I'm making this game and do far it's been good but now I have a problem. Tje problem is my character does not appear on the screen. It did what it was supposed to before I added the enemy. Can someone tell me what I'm doing wrong. The error I'm getting is about "gD.fill(white)", on line 67 in Main.py and appeared after I pressed quit on pygame. Thx in advance. 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) def Button(gD, Butx, Buty, Butx2, Buty2, Butcolor, ShadowColor, text, textsize, textcolor, textFont, command=None, command2=None): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() smallText = pygame.font.SysFont((textFont), textsize) textSurf, textRect = Screen_Display.text_objects(text, smallText, textcolor) textRect.center = ((Butx + (Butx2/2)), Buty + (Buty2/2)) pygame.draw.rect(gD, Butcolor, (Butx, Buty, Butx2, Buty2)) gD.blit(textSurf, textRect) if Butx + Butx2 > mouse[0] > Butx and Buty + Buty2 > mouse[1] > Buty: pygame.draw.rect(gD, ShadowColor, (Butx, Buty, Butx2, Buty2)) gD.blit(textSurf, textRect) if click[0] == 1: if command != None: command() if command2 != None: command2() else: pygame.draw.rect(gD, Butcolor, (Butx, Buty, Butx2, Buty2)) gD.blit(textSurf, textRect) class Updates(): def windowRedraw(win, color): if color != None: win.fill(color) pygame.display.update()Main.py import pygame import random import time from pygame.locals import * from DestinationFunc import Screen_Display as SD from DestinationFunc import Updates as ud 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").convert() SpikeBat = pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/Spiked_Bat.png").convert() IronBat = pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/MetalBat.png").convert() IronSword = pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/IronSword.png").convert() DiamondSword = pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/DiamondSword.png").convert() #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] #GameData 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'} #Config fps = 60 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): 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.weapType = weapType def draw(self): gD.fill(white) pygame.draw.rect(gD, self.color, (self.rect)) #self.drawWeap() def drawWeap(self): if self.direct == 'Left': self.weapon = gD.blit(self.weapType, (self.x + 5, self.y + 25)) if self.direct == 'Right': self.weapon = gD.blit(self.weapType, (self.x + 45, self.y + 25)) if self.direct == 'Up': self.weapon = gD.blit(self.weapType, (self.x + 25, self.y + 5)) if self.direct == 'Down': self.weapon = gD.blit(self.weapType, (self.x + 25, self.y + 45)) self.update() def move(self): keyed = pygame.key.get_pressed() 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' self.update() def update(self): self.rect = (self.x, self.y, self.width, self.height) def getPos(self): return self.x, self.y class Enemy(): def __init__(self, x, y, width, height, color, weapType, plrX, plrY): 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.weapType = weapType self.plrX = plrX self.plrY = plrY def draw(self): gD.fill(white) pygame.draw.rect(gD, self.color, (self.rect)) #self.drawWeap() def drawWeap(self): if self.direct == 'Left': self.weapon = gD.blit(self.weapType, (self.x + 5, self.y + 25)) if self.direct == 'Right': self.weapon = gD.blit(self.weapType, (self.x + 45, self.y + 25)) if self.direct == 'Up': self.weapon = gD.blit(self.weapType, (self.x + 25, self.y + 5)) if self.direct == 'Down': self.weapon = gD.blit(self.weapType, (self.x + 25, self.y + 45)) self.update() def move(self): if self.x != None and self.y != None: if self.plrX > self.x: self.x += 1 elif self.plrX < self.x: self.x -= 1 if self.plrY > self.y: self.y += 1 elif self.plrY < self.y: self.y -= 1 self.update() def update(self): self.rect = (self.x, self.y, self.width, self.height) 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) SD.Button(gD, round(width * .3), round(height * .7), round(width * .4), round(height * .2), CustomColor1, CustomColor2, 'Confirm!', round(width/20), blue, 'arial', Main) SD.Button(gD, round(width * .75), round(height * .8), round(width * .2), round(height * .1), red, darker_red, "No!", round(width/50), blue, 'arial', SignIn) ud.windowRedraw(gD, None) clock.tick(fps) def SignIn(): global signInList global username 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 Confirm_Screen(username) else: if username in signInList: password = input('Please type your password: ') if password == signInList[username]: Confirm_Screen(username) else: print('Invalid Password') SignIn() else: print('Invalid Username') SignIn() def Main(): global username global UserArmor global UserWeap armor = UserArmor[username] weap = UserWeap[username] p = Player(width/2, height/2, 50, 50, armor, weap) e = Enemy(0 ,0, 50, 50, brown, WoodBat, None, None) play = True clock.tick(fps) while play: for event in pygame.event.get(): if event.type == pygame.QUIT: End_Game() p.draw() p.move() plrPos = p.getPos() e = Enemy(0 ,0, 50, 50, brown, WoodBat, plrPos[0], plrPos[1]) e.draw() e.move() ud.windowRedraw(gD, None) End_Game() def End_Game(): print(signInList) print(UserArmor) print(UserWeap) print(UserLvl) pygame.quit() SD.message_display(gD, "Fill in info on console", round(int(width/20)), Unique_Color, width/2, height/2) ud.windowRedraw(gD, None) SignIn()Here's the error -
RE: Drawing player and enemy (pygame) - Windspar - Apr-14-2019 Your error is coming from line 215. You called pygame quit before all drawing was done. play = FalseWhen you fill surface with color. It will erase everything that you just drawn. Button should be a class by itself. class Updates need to be remove. Most important pygame should have only one main loop. With either pygame.display.flip or pygame.display.update. Should reduce global variables. Pygame color has builtin colors. color_list = [pygame.Color('red'), pygame.Color('red3'), # darker red pygame.Color('black'), pygame.Color('lightblue'), pygame.Color('blue')] RE: Drawing player and enemy (pygame) - SheeppOSU - Apr-14-2019 Thsnkyou Drawing enemy but not player (pygame) - SheeppOSU - Apr-14-2019 I have this game. It works fine with logging in and the confirm screen. The problem is in the main loop. The player is no where to be seen. Moving the player does nothing. The screen is just plain yellow. Thx in advance 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) def windowRedraw(win, color): if color != None: win.fill(color) pygame.display.update() class Buttons(): def Button(gD, Butx, Buty, Butx2, Buty2, Butcolor, ShadowColor, text, textsize, textcolor, textFont, command=None, command2=None): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() smallText = pygame.font.SysFont((textFont), textsize) textSurf, textRect = Screen_Display.text_objects(text, smallText, textcolor) textRect.center = ((Butx + (Butx2/2)), Buty + (Buty2/2)) pygame.draw.rect(gD, Butcolor, (Butx, Buty, Butx2, Buty2)) gD.blit(textSurf, textRect) if Butx + Butx2 > mouse[0] > Butx and Buty + Buty2 > mouse[1] > Buty: pygame.draw.rect(gD, ShadowColor, (Butx, Buty, Butx2, Buty2)) gD.blit(textSurf, textRect) if click[0] == 1: if command != None: command() if command2 != None: command2() else: pygame.draw.rect(gD, Butcolor, (Butx, Buty, Butx2, Buty2)) gD.blit(textSurf, textRect)Main.py import pygame import random import time import threading from pygame.locals import * from DestinationFunc import Screen_Display as SD from DestinationFunc import Buttons 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").convert() SpikeBat = pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/Spiked_Bat.png").convert() IronBat = pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/MetalBat.png").convert() IronSword = pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/IronSword.png").convert() DiamondSword = pygame.image.load("C:/Users/Chadd/Desktop/Ayden Stuff/DiamondSword.png").convert() #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] 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'} self.signInList = signInList self.UserArmor = UserArmor self.UserWeap = UserWeap self.UserLvl = UserLvl def getListings(self): return self.signInList, self.UserArmor, self.UserWeap, self.UserLvl #Config fps = 60 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): 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.weapType = weapType def draw(self): gD.fill(white) pygame.draw.rect(gD, self.color, (self.rect)) #self.drawWeap() def drawWeap(self): if self.direct == 'Left': self.weapon = gD.blit(self.weapType, (self.x + 5, self.y + 25)) if self.direct == 'Right': self.weapon = gD.blit(self.weapType, (self.x + 45, self.y + 25)) if self.direct == 'Up': self.weapon = gD.blit(self.weapType, (self.x + 25, self.y + 5)) if self.direct == 'Down': self.weapon = gD.blit(self.weapType, (self.x + 25, self.y + 45)) self.update() def move(self): keyed = pygame.key.get_pressed() 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' self.update() def update(self): self.rect = (self.x, self.y, self.width, self.height) def getPos(self): return self.x, self.y class Enemy(): def __init__(self, x, y, width, height, color, weapType): 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.weapType = weapType def draw(self): gD.fill(white) pygame.draw.rect(gD, self.color, (self.rect)) #self.drawWeap() def drawWeap(self): if self.direct == 'Left': self.weapon = gD.blit(self.weapType, (self.x + 5, self.y + 25)) if self.direct == 'Right': self.weapon = gD.blit(self.weapType, (self.x + 45, self.y + 25)) if self.direct == 'Up': self.weapon = gD.blit(self.weapType, (self.x + 25, self.y + 5)) if self.direct == 'Down': self.weapon = gD.blit(self.weapType, (self.x + 25, self.y + 45)) self.update() def move(self, x, y, width, height, color, weapType): p = Player(x, y, width, height, color, weapType) plrPos = p.getPos() plrX = plrPos[0] plrY = plrPos[1] if self.x != None and self.y != None: if plrX > self.x: self.x += .5 elif plrX < self.x: self.x -= .5 if plrY > self.y: self.y += .5 elif plrY < self.y: self.y -= .5 self.update() def update(self): self.rect = (self.x, self.y, self.width, self.height) 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) BT.Button(gD, round(width * .3), round(height * .7), round(width * .4), round(height * .2), CustomColor1, CustomColor2, 'Confirm!', round(width/20), blue, 'arial', Confirm_QUIT) BT.Button(gD, round(width * .75), round(height * .8), round(width * .2), round(height * .1), red, darker_red, "No!", round(width/50), blue, 'arial', SignIn) SD.windowRedraw(gD, None) clock.tick(fps) def SignIn(): global username GD = GameData() listings = GD.getListings() signInList = listings[0] UserArmor = listings[1] UserWeap = listings[2] UserLvl = listings[3] 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 SignIn() Confirm_Screen(username) GD = GameData() listings = GD.getListings() UserArmor = listings[1] UserWeap = listings[2] armor = UserArmor[username] weap = UserWeap[username] p = Player(width/2, height/2, 50, 50, armor, weap) e = Enemy(0 ,0, 50, 50, brown, WoodBat) play = True clock.tick(fps) while play: for event in pygame.event.get(): if event.type == pygame.QUIT: End_Game() t1 = threading.Thread(target=p.draw) t2 = threading.Thread(target=p.move) t3 = threading.Thread(target=e.draw) t4 = threading.Thread(target=e.move(width/2, height/2, 50, 50, armor, weap)) t1.start() t2.start() t3.start() t4.start() t1.join() t2.join() t3.join() t4.join() SD.windowRedraw(gD, pygame.Color('yellow')) End_Game() def End_Game(): listings = GameData.getListings() print(listings[0]) print('') print(listings[1]) print('') print(listings[2]) print('') print(listings[3]) SD.message_display(gD, "Fill in info on console", round(int(width/20)), Unique_Color, width/2, height/2) SD.windowRedraw(gD, None) Main() pygame.quit() RE: Drawing enemy but not player (pygame) - Windspar - Apr-14-2019 Don't make new post. For same code that been alter a little. You going the wrong direction here. Let remove python time and threading modules. That another learning curve. You need to rewrite code. Do not copy and paste code. Type it out. Examples of clean code. Basic pygame setup. class Game: info = None def __init__(self, caption, width, height): # Basic pygame setup pygame.display.set_caption(caption) self.surface = pygame.display.set_mode((width, height)) self.rect = self.surface.get_rect() self.clock = pygame.time.Clock() self.fps = 30 # Class global variable Game.info = self def mainloop(self): self.running = True while self.running: for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self.running = False self.surface.fill(pygame.Color('Navy')) # Draw code here pygame.display.flip() self.clock.tick(self.fps) def main(): pygame.init() game = Game("Example", 800, 600) game.mainloop() pygame.quit() main()Button draft. Turning button into an object. class Button: def __init__(self, caption, font, rect, color, text_color): self.image = font.render(caption, 1, text_color) self.rect = rect self.text_rect = self.image.get_rect() self.text_rect.center = rect.center self.color = color def draw(self, surface): # button draw code here def onbuttonup(self, event): # click eventChallange. Let see you add on mouse over method and finish button class. pygame determining which enemy is taking damage and sword hit. - SheeppOSU - Apr-15-2019 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 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() RE: pygame determining which enemy is taking damage and sword hit. - nilamo - Apr-16-2019 For 1, each individual object (so players and enemies) should be tracking their own health (current and max). The only reason to have the player keep track of damage dealt, is if you want a counter on the pause menu or something to say "you've done 20383812 damage so far!". Otherwise, it's just extra info that isn't needed. Also, a lot of the Player and Enemy classes look the same. They could maybe be the same class, renamed to just "Creature" or something. They both have health, they both deal damage, they both move, etc. RE: pygame determining which enemy is taking damage and sword hit. - metulburr - Apr-16-2019 (Apr-15-2019, 11:41 PM)SheeppOSU Wrote: 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.It is much easier to fix issues if we can run the game. This is going to require you to clean your code. 1) You have hard coded a path that is only available on your computer. As well as i am on linux, so there is not even a C drive even. It is much better to load from a sub-directory of the game directory. In that way it can be moved to any computer with a different operating system and run. Quote: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")) 2) It is much easier to put your code up on github so people have access yo your resources. Read here for more info. Doing this work will make it easier for people to help you along the way. I am a very visual person. I need to run the program and then follow your problems along the way, not just read through the code. I could switch over all your images to pygame surfaces. However you want the help. You are going to have to make it runnable for other people. As a side note. Your numerous threads regarding this one program can be merged into one thread. After i merge these, please repost in the same thread if you come along another issue regarding this program. RE: drawing, moving, and collision problems (pygame) - SheeppOSU - Apr-16-2019 Looks like it'll take longer than expected. I'm so mad! I opened my game not knowing I already had it open. Made a bunch of changes and saved it to rename a folder that it was inside of. After I saved it I opened the original just thinking it didn't close out and saved my original. GRRRRR! RE: drawing, moving, and collision problems (pygame) - metulburr - Apr-16-2019 That is one benefit to version control software. Every change is logged and can be reverted. |