Jan-23-2023, 06:07 PM
Hi, I'm kind of new at both python and pygame. I was trying to make a (very) simple platform fight game, and of course I found some errors I can't fix by myself. The first error I got when I was trying to make the players fall off from the platform whenever they do not stand on it anymore, and now they fall infinitely through it, making the players unable to move. Here's the lines of code I changed to make that happen, because I changed the original code so that does not happen, but players just float in the air if they are not in the platform:
The second error I got was that once one player dies while being attacked by the other one, both of them can't attack anymore, and the second player's image does not change to dead.png, even though I have put it as you can see here: (The first player's image works fine)
Here's my entire code. Note that I have a different file for constant variables named constants.py. I will also paste its code here, just in case.
def apply_gravity(self): if self.jumping: self.jump_velocity += self.gravity * self.jump_slow self.y += self.jump_velocity if self.y >= 300: self.y = 300 self.jumping = False if not self.check_collision(platform) and self.jumping == False: self.y += self.jump_velocity if self.y >= 0: self.y = 300 self.deaths += 1That's inside of the Player's class, and I also put player1.check_gravity() on the main loop.
The second error I got was that once one player dies while being attacked by the other one, both of them can't attack anymore, and the second player's image does not change to dead.png, even though I have put it as you can see here: (The first player's image works fine)
while player1.health <= 0: player1.x = 300 player1.y = 200 player1.health = 100 player1.deaths += 1 player1.state = "idling" player1.image = dead_image while player2.health <= 0: player2.x = 650 player2.y = 200 player2.health = 100 player2.deaths += 1 player2.image = dead_image player2.state = "idling"The third error I got was that when a player spawns or respawns, it starts floating in the air, until they jump, making them able to even move from side to side.
Here's my entire code. Note that I have a different file for constant variables named constants.py. I will also paste its code here, just in case.
import pygame import pygame.font from pygame import display from constants import * pygame.init() screen = pygame.display.set_mode(size) pygame.display.set_caption("PUXKERBOIS ULTIMATE") pygame.display.set_icon(icon) class Player: def __init__(self, x, y, image_path, health, deaths): self.x = x self.y = y self.health = health self.deaths = deaths self.image = pygame.image.load(image_path) self.hitbox = pygame.Rect(x, y, self.image.get_width(), self.image.get_height()) self.health = 100 self.velocity = 3 self.gravity = 0.1 self.jumping = False self.jump_velocity = -3 self.jump_slow = 0.75 self.attack_image = pygame.image.load("attack.png") self.state = "idling" def move(self, dx, dy): self.x += dx * self.velocity self.y += dy * self.velocity self.hitbox.x += dx * self.velocity self.hitbox.y += dy * self.velocity def jump(self): if not self.jumping: self.jumping = True self.jump_velocity = -3.75 * self.jump_slow def attack(self, enemy): if self.hitbox.colliderect(enemy.hitbox): enemy.health -= 20 self.state = "attacking" print(enemy.health) def apply_gravity(self): if self.jumping: self.jump_velocity += self.gravity * self.jump_slow self.y += self.jump_velocity if self.y >= 300: self.y = 300 self.jumping = False def check_collision(self, object): if self.hitbox.colliderect(object.hitbox): self.y = object.y - self.hitbox.height if self.y >= height: self.deaths += 1 self.y = 300 def update(self): if self.state == "attacking": self.image = self.attack_image self.state = "idling" def draw(self, screen): screen.blit(self.image, (self.x, self.y)) def draw_hitbox(self, screen): pygame.draw.rect(screen, (255, 0, 0), self.hitbox, 2) class Platform: def __init__(self, x, y, width, height, image_path): self.x = x self.y = y self.width = width self.height = height self.image = pygame.image.load(image_path) self.hitbox = pygame.Rect(x, y, width, height) def draw(self, screen): screen.blit(self.image, (self.x, self.y)) def setcoors(): raw = input("Please insert valid parameters (player: 1|2 x:int y:int): ") raw_list = [x for x in raw.split(" ")] x = int(raw_list[1]) y = int(raw_list[2]) if len(raw_list) > 3: print("Please insert valid parameters.") else: if raw_list[0] == "1": player1.x = x player1.y = y elif raw_list[0] == "2": player2.x = x player2.y = y else: print("Please insert valid parameters.") def show_deaths(deaths): text = "Deaths: " + str(deaths) return text def show_winner(winner): text = fThe winner is {winner}!" return text player1 = Player(300, 200, "player.png", 100, 0) player2 = Player(650, 200, "player.png", 100, 0) player1.jump() #Note that I've called the jump() method here so the players fall to the platform and don't start floating (yet) player2.jump() platform = Platform(0, 60, 800, 100, "platform.png") while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONUP: player2.attack(player1) else: player2.image = pygame.image.load("player.png") if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: player1.attack(player2) else: player1.image = pygame.image.load("player.png") if event.type == pygame.KEYDOWN: if event.key == pygame.K_1: print(f"Player1: health: {player1.health}, deaths: {player1.deaths}") print(f"Player2: health: {player2.health}, deaths: {player2.deaths}") keys = pygame.key.get_pressed() if keys[pygame.K_a]: player1.move(-1, 0) if keys[pygame.K_d]: player1.move(1, 0) if keys[pygame.K_w]: player1.jump() if keys[pygame.K_LEFT]: player2.move(-1, 0) if keys[pygame.K_RIGHT]: player2.move(1, 0) if keys[pygame.K_UP]: player2.jump() if keys[pygame.K_e]: print(player1.x, player1.y) if keys[pygame.K_r]: print(player2.x, player2.y) if keys[pygame.K_x]: running = False if keys[pygame.K_t]: setcoors() if keys[pygame.K_2]: player1.draw_hitbox(screen) player2.draw_hitbox(screen) while player1.health <= 0: player1.x = 300 player1.y = 200 player1.health = 100 player1.deaths += 1 player1.state = "idling" player1.image = dead_image while player2.health <= 0: player2.x = 650 player2.y = 200 player2.health = 100 player2.deaths += 1 player2.image = dead_image player2.state = "idling" if player1.y > height: player1.deaths += 1 player1.x = 300 player1.y = 300 player1.hitbox.x = player1.x player1.hitbox.y = player1.y if player2.y > height: player2.deaths += 1 player2.x = 650 player2.y = 300 player2.hitbox.x = player2.x player2.hitbox.y = player2.y if player1.health <= 0: player2_winner_text = font.render(show_winner(player2), True, (255, 255, 255)) screen.blit(player2_winner_text, (10, 10)) player1.health = 20 if player2.health <= 0: player1_winner_text = font.render(show_winner(player2), True, (255, 255, 255)) screen.blit(player1_winner_text, (10, 10)) player2.health = 20 #This does not work player1_deaths_text = font.render(show_deaths(player1.deaths), True, (255, 255, 255)) screen.blit(player1_deaths_text, (10, 10)) player2_deaths_text = font.render(show_deaths(player2.deaths), True, (255, 255, 255)) screen.blit(player2_deaths_text, (height - 200, 10)) player1.check_collision(platform) player2.check_collision(platform) player1.update() player2.update() player1.apply_gravity() player2.apply_gravity() if player1.x <= player_width - 70: player1.x = player_width - 70 if player1.x >= width - player_width: player1.x = width - player_width if player2.x <= player_width - 70: player2.x = player_width - 70 if player2.x >= width - player_width: player2.x = width - player_width screen.blit(bg, (0, 0)) player1.draw(screen) player2.draw(screen) platform.draw(screen) pygame.display.update() pygame.quitAnd here's the constants.py file's code:
import pygame import pygame.font pygame.init() width = 1080 height = 720 size = (width, height) icon = pygame.image.load("icon.png") bg = pygame.image.load("bg.png") player_image = pygame.image.load("player.png") player_width = player_image.get_width() dead_image = pygame.image.load("dead.png") font = pygame.font.Font(None, 30) running = TrueWell, I hope I've been clear enough, any help is appreciated!