I am making my own pygame game, but I want to reduce the size of the player's hitbox in order to make dodging bullets easier.
import math
import pygame
from src import util
class Player(pygame.sprite.Sprite):
def init(self, group):
super().init(group)
self.weapon = None
self.experience = 0
self.animation = [pygame.image.load(util.sprites("frame_0_delay-0.05s.png")),
pygame.image.load(util.sprites("frame_1_delay-0.05s.png")),
pygame.image.load(util.sprites("frame_2_delay-0.05s.png")),
pygame.image.load(util.sprites("frame_3_delay-0.05s.png")),
pygame.image.load(util.sprites("frame_4_delay-0.05s.png")),
pygame.image.load(util.sprites("frame_5_delay-0.05s.png")),
pygame.image.load(util.sprites("frame_6_delay-0.05s.png")),
pygame.image.load(util.sprites("frame_7_delay-0.05s.png"))]
self.frame = 0
right_player_animation = self.animation[self.frame]
self.image = right_player_animation
self.rect = self.image.get_rect()
self.hitbox = self.rect.inflate(-40, -20)
self.direction = pygame.math.Vector2()
self.facing = "right"
self.speed = 5
self.speed_modifier = 1
self.kill_count = 0
def input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
self.direction.y = -1
elif keys[pygame.K_s]:
self.direction.y = 1
else:
self.direction.y = 0
if keys[pygame.K_d]:
self.direction.x = 1
elif keys[pygame.K_a]:
self.direction.x = -1
else:
self.direction.x = 0
if keys[pygame.K_LSHIFT]:
self.speed_modifier = 0.5
else:
self.speed_modifier = 1
def set_position(self, pos):
self.rect = self.image.get_rect(center=pos)
def set_weapon(self, weapon):
self.weapon = weapon
def attack(self):
if self.weapon:
self.weapon.attack()
pygame.mixer.Channel(0).play(pygame.mixer.Sound(util.sound("gun_fire.mp3")))
def update(self):
self.input()
self.rect.center += self.direction * self.speed * self.speed_modifier
self.frame += 1
mouse_x, mouse_y = pygame.mouse.get_pos()
dx, dy = mouse_x - 640, mouse_y - 360
look_direction = math.degrees(math.atan2(-dy, dx))
if look_direction < 0:
look_direction = 360 + look_direction
if 270 > look_direction > 90:
self.facing = 'left'
else:
self.facing = 'right'
animation_index = (self.frame // 3) % len(self.animation)
if self.facing == "right":
self.image = self.animation[animation_index]
else:
self.image = pygame.transform.flip(self.animation[animation_index], True, False)
if self.weapon:
self.weapon.direction = look_direction
self.weapon.set_position(self.rect.center)
self.rect
then you have to also move itshitbox
- pygame will NOT move it automatically. – furas Apr 25 '23 at 20:16