After launching the game, I managed to use the Left, Right, Up and Down arrow keys to move the hero character as expected. However, after the first combat with a villain character, the hero character couldn’t move at all in despite of me trying to press any of the arrow keys.
import pygame
import random
class Hero:
def __init__(self, image_name, position, window):
self.image = pygame.image.load(image_name).convert_alpha()
self.image = pygame.transform.scale(self.image,
(int(self.image.get_width() * 0.5),
int(self.image.get_height() * 0.5)))
self.window = window
self.position = position
self.rect = self.image.get_rect(center=self.position)
self.max_hp = 100
self.current_hp = self.max_hp
def move(self):
adjusted_position = [
max(self.image.get_width()/2, min(self.window.get_width() - self.image.get_width()/2, self.position[0])),
max(self.image.get_height()/2 + 20, min(self.window.get_height()-self.image.get_height()/2, self.position[1]))
]
self.position = adjusted_position
self.rect.center = round(self.position[0]), round(self.position[1])
def draw(self):
self.window.fill((127, 127, 127))
self.window.blit(self.image, (self.position[0] - self.image.get_width()/2, self.position[1] - self.image.get_height()/2))
class Villain:
def __init__(self, image_path, position, window):
self.image = pygame.image.load(image_path).convert_alpha()
self.image = pygame.transform.scale(self.image,
(int(self.image.get_width() * 0.5),
int(self.image.get_height() * 0.5)))
self.position = position
self.window = window
self.rect = self.image.get_rect(center=self.position)
self.max_hp = 100
self.current_hp = self.max_hp
def move(self, x_range=(-10, 10), y_range=(-10, 10)):
self.position[0] += random.randint(*x_range)
self.position[1] += random.randint(*y_range)
adjusted_position = [
max(self.image.get_width()/2, min(self.window.get_width() - self.image.get_width()/2, self.position[0])),
max(self.image.get_height()/2 + 10, min(self.window.get_height() - self.image.get_height()/2, self.position[1]))
]
self.position = adjusted_position
self.rect.center = round(self.position[0]), round(self.position[1])
def draw(self):
self.window.blit(self.image, (self.position[0] - self.image.get_width()/2, self.position[1] - self.image.get_height()/2))
class Scene:
def __init__(self, window):
self.window = window
self.hero = Hero('hero.png', [400, 300], self.window)
self.villains = [Villain('villain.png', [80, 80], self.window),
Villain('villain.png', [720, 80], self.window),
Villain('villain.png', [80, 520], self.window),
Villain('villain.png', [720, 520], self.window)]
self.current_villain = None
self.in_combat = False
def check_for_combat(self):
for villain in self.villains:
if pygame.math.Vector2(villain.position).distance_to(self.hero.position) < 50:
self.in_combat = True
self.current_villain = villain
return True
return False
def handle_combat(self):
if self.in_combat and self.current_villain:
hero_damage = int(input('Enter a number between 0 and 8: '))
self.current_villain.current_hp -= hero_damage
if self.current_villain.current_hp <= 0:
print("Enemy defeated!")
self.villains.remove(self.current_villain)
self.in_combat = False
self.current_villain = None
else:
enemy_damage = random.randint(2, 8)
self.hero.current_hp -= enemy_damage
if self.hero.current_hp <= 0:
print("Hero fell!")
def handle_events(self):
keys = pygame.key.get_pressed()
move_speed = 2
if keys[pygame.K_LEFT]:
self.hero.position[0] -= move_speed
if keys[pygame.K_RIGHT]:
self.hero.position[0] += move_speed
if keys[pygame.K_UP]:
self.hero.position[1] -= move_speed
if keys[pygame.K_DOWN]:
self.hero.position[1] += move_speed
print(self.hero.position)
self.hero.move()
for villain in self.villains:
villain.move()
if not self.in_combat:
if self.check_for_combat():
return
def draw(self):
self.window.fill((127, 127, 127))
self.window.blit(self.hero.image, self.hero.position)
for villain in self.villains:
villain.draw()
pygame.display.update()
class Game:
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode((800, 600))
self.game_scene = Scene(self.window)
def run(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
self.game_scene.handle_events()
self.game_scene.handle_combat()
self.game_scene.draw()
window = pygame.display.set_mode((800, 600))
game = Game()
game.run()
My question is: what caused the hero character stop moving after the first encounter? How can I fix it?