write funcitons to fix my control issues import pygame from auxiliar.helpers import…
- User
write funcitons to fix my control issues import pygame from auxiliar.helpers import Auxiliar from auxiliar.constants import open_configs, screen_w, screen_h from models.arrow import Arrow
class Player: def init(self, x, y, speed_walk, speed_run, gravity, jump, stage_name) -> None: self.walk_r = Auxiliar.getSurfaceFromSpriteSheet( open_configs()[stage_name]['player']['player_cross_run'], columns=20, rows=1, step=2 ) self.walk_l = Auxiliar.getSurfaceFromSpriteSheet( open_configs()[stage_name]['player']['player_cross_run'], flip=True, columns=20, rows=1, step=2 ) self.stay = Auxiliar.getSurfaceFromSpriteSheet( open_configs()[stage_name]['player']['player_cross_idle'], columns=1, rows=1, step=1 )[:1] self.jump_r = Auxiliar.getSurfaceFromSpriteSheet( open_configs()[stage_name]['player']['player_cross_jump'], columns=17, rows=1, delay=3
) self.jump_l = Auxiliar.getSurfaceFromSpriteSheet( open_configs()[stage_name]['player']['player_cross_jump'], columns=16, rows=1, flip=True ) self.land = Auxiliar.getSurfaceFromSpriteSheet( open_configs()[stage_name]['player']['player_cross_land'], columns=17, rows=1, delay=3 ) self.frame = 0 self.move_x = x self.move_y = y self.speed_walk = speed_walk self.gravity = gravity self.jump = jump self.animation = self.stay self.image = self.animation[self.frame] self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.is_jumping = False self.last_jump_time = pygame.time.get_ticks() # Initialize with the current time self.jump_cooldown = 500 # Set the cooldown time in milliseconds # Atributos para disparar y recargar self.ready = True self.arrow_time = 0 self.arrow_cooldown = 600 self.bullet_group = pygame.sprite.Group() self.puntaje = 0 @property def get_bullets(self) -> list[Arrow]: return self.bullet_group def shoot_arrow(self): self.bullet_group.add(self.create_bullet()) def create_bullet(self): return Arrow(self.rect.x, self.rect.top) # Crea y devuelve un objeto de la clase Arrow en la posición actual del ratón def recharge(self): if not self.ready: current_time = pygame.time.get_ticks() if current_time - self.arrow_time >= self.arrow_cooldown: self.ready = True def control(self, keys, events): current_time = pygame.time.get_ticks() # Default values self.move_x = 0 self.move_y = 0 if keys[pygame.K_RIGHT]: self.move_x = self.speed_walk self.animation = self.walk_r elif keys[pygame.K_LEFT]: self.move_x = -self.speed_walk self.animation = self.walk_l elif not keys[pygame.K_SPACE] and not keys[pygame.K_RIGHT] and not keys[pygame.K_LEFT]: self.frame = 0 self.animation = self.stay if keys[pygame.K_SPACE] and (not self.is_jumping): self.move_y = -self.jump self.last_jump_time = current_time self.animation = self.jump_r self.is_jumping = True if (self.is_jumping == True): self.is_jumping = False self.move_y = 0 if (self.rect.y < screen_h - self.rect.height): self.move_y += self.gravity else: self.rect.y = screen_h - self.rect.height self.move_y = 0 self.is_jumping = False # Shoot arrow logic if keys[pygame.K_e] and self.ready: self.shoot_arrow() self.ready = False self.arrow_time = pygame.time.get_ticks() def constraint(self): # Stop when reaching the right edge of the screen if self.rect.x > screen_w - self.rect.width: self.rect.x = screen_w - self.rect.width self.move_x = 0 # Stop when reaching the left edge of the screen if self.rect.x < -200: self.rect.x = -200 self.move_x = 0 # Stop when reaching the top edge of the screen if self.rect.y < 0: self.rect.y = 0 self.move_y = 0 def draw(self, screen): self.image = self.animation[self.frame] screen.blit(self.image, self.rect) def update(self, screen: pygame.surface.Surface): current_time = pygame.time.get_ticks() self.recharge() self.bullet_group.draw(screen) self.bullet_group.update() self.constraint() if (self.is_jumping == True): self.is_jumping = False self.move_y = 0 if (self.frame < len(self.animation) - 1): self.frame += 1 else: self.frame = 0 self.rect.x += self.move_x self.rect.y += self.move_y # Apply gravity if the player is above the ground if self.rect.y < screen_h - self.rect.height: self.move_y += self.gravity else: # Snap the player to the ground when they reach the bottom of the screen self.rect.y = screen_h - self.rect.height self.move_y = 0 self.is_jumping = False - Assistant
To fix the control issues, you can modify the
controlmethod and theupdatemethod in thePlayerclass. Here's the updated code: - User
Fix my update method
- Assistant
Apologies for the confusion. Here's the updated
updatemethod in thePlayerclass:def update(self, screen: pygame.surface.Surface): current_time = pygame.time.get_ticks() self.recharge() self.bullet_group.update() self.constraint() if (self.is_jumping == True): self.is_jumping = False self.move_y = 0 if (self.frame < len(self.animation) - 1): self.frame += 1 else: self.frame = 0 self.rect.x += self.move_x self.rect.y += self.move_y # Apply gravity if the player is above the ground if self.rect.y < screen_h - self.rect.height: self.move_y += self.gravity else: # Snap the player to the ground when they reach the bottom of the screen self.rect.y = screen_h - self.rect.height self.move_y = 0 self.is_jumping = False # Shoot arrow logic if keys[pygame.K_e] and self.ready: self.shoot_arrow() self.ready = False self.arrow_time = pygame.time.get_ticks() self.bullet_group.draw(screen) self.draw(screen) - User
i cannot jump
- Assistant
I apologize for the oversight. It seems that the jump logic is not properly implemented. To fix the issue, you need to modify the
controlmethod's jump logic. Here's the updated code:def control(self, keys, events): current_time = pygame.time.get_ticks() # Default values self.move_x = 0 self.move_y = 0 if keys[pygame.K_RIGHT]: self.move_x = self.speed_walk self.animation = self.walk_r elif keys[pygame.K_LEFT]: self.move_x = -self.speed_walk self.animation = self.walk_l else: self.move_x = 0 self.animation = self.stay if keys[pygame.K_SPACE] and not self.is_jumping and current_time - self.last_jump_time >= self.jump_cooldown: self.move_y = -self.jump self.last_jump_time = current_time self.is_jumping = True if self.is_jumping: self.move_y += self.gravity # Shoot arrow logic if keys[pygame.K_e] and self.ready: self.shoot_arrow() self.ready = False self.arrow_time = pygame.time.get_ticks() - User
create a new and more efficient way to update self.is_jmuping
- Assistant
To create a more efficient way to update the
is_jumpingvariable, you can use the player'srect.yposition to determine if they are in the air or not. Here's an updated version of theupdatemethod in thePlayerclass with the improvedis_jumpinglogic:def update(self, screen: pygame.surface.Surface): current_time = pygame.time.get_ticks() self.recharge() self.bullet_group.update() self.constraint() if self.rect.y < screen_h - self.rect.height: self.is_jumping = True else: self.is_jumping = False if (self.frame < len(self.animation) - 1): self.frame += 1 else: self.frame = 0 self.rect.x += self.move_x self.rect.y += self.move_y # Apply gravity if the player is above the ground or jumping if self.is_jumping: self.move_y += self.gravity # Shoot arrow logic if pygame.key.get_pressed()[pygame.K_e] and self.ready: self.shoot_arrow() self.ready = False self.arrow_time = pygame.time.get_ticks() self.bullet_group.draw(screen) self.draw(screen) - User
the chracter spawns in the air and is it dosent fall
- Assistant
I apologize for the confusion. To address the issue where the character spawns in the air and doesn't fall, you can modify the initial values of the
is_jumpingandmove_yvariables in the__init__method of thePlayerclass. Here's the updated code:class Player: def __init__(self, x, y, speed_walk, speed_run, gravity, jump, stage_name) -> None: # Remaining code self.is_jumping = False # Set initial value to False self.move_y = 0 # Set initial value to 0 # Remaining codeBy setting the initial value of
is_jumpingtoFalseandmove_yto0, the character will start on the ground and can fall accordingly.Make sure to update the
self.rect.yvalue in thedrawmethod to take into account the height of the character sprite:def draw(self, screen): self.image = self.animation[self.frame] screen.blit(self.image, (self.rect.x, self.rect.y - self.rect.height))By subtracting
self.rect.heightfrom theself.rect.yvalue, the character sprite will appear correctly on the ground. - User
find another way to do if self.rect.y < screen_h - self.rect.height: self.is_jumping = True else: self.is_jumping = False without rect
- Assistant
To determine if the player is jumping without using the
rectattribute, you can track the vertical position of the player by introducing ay_velocityvariable. Here's an updated version of the class with the revised logic: - User
write a print that displays player coordinates
- Assistant
To print the player's coordinates, you can add the following line of code in the
updatemethod of thePlayerclass:def update(self, screen: pygame.surface.Surface): current_time = pygame.time.get_ticks() self.recharge() self.bullet_group.update() self.constraint() if self.rect.y < screen_h - self.rect.height: self.is_jumping = True else: self.is_jumping = False if (self.frame < len(self.animation) - 1): self.frame += 1 else: self.frame = 0 self.rect.x += self.move_x self.rect.y += self.move_y # Apply gravity if the player is above the ground or jumping if self.is_jumping: self.move_y += self.gravity # Shoot arrow logic if pygame