Почему у Player_Height нет четкого индикатора, чтобы увеличиваться, но, тем не менее, когда игрок движется из стороны в Python

Программы на Python
Anonymous
 Почему у Player_Height нет четкого индикатора, чтобы увеличиваться, но, тем не менее, когда игрок движется из стороны в

Сообщение Anonymous »

Логика Player_Height предназначена для того, чтобы держать игрока на земле, но все же что -то заставляет высоту игрока (не переменную) заметно увеличивать окно Pygame. < /p>
Вот есть Два файла ~ 50 строк, которые обрабатывают логику, если кто -то хочет устранение неполадок: < /p>
player.py:
import pygame
import math

RED = (255, 0, 0)

def draw_player(screen, player_x, player_y, player_height, walk_angle, is_walking_left, player_width):
# Head - Red circle
head_radius = 20
pygame.draw.circle(screen, RED, (player_x, player_y - player_height // 2 - head_radius), head_radius)

# Body - Red rectangle (control width directly here)
pygame.draw.rect(screen, RED, (player_x - player_width // 2, player_y - player_height // 2, player_width, player_height))

# Arms and legs (same as before)
arm_length = 30
arm_offset = 10
leg_length = 40
leg_offset = 10

# Calculate the base position for arms and legs
base_y = player_y - player_height // 2

# Left arm calculation
arm1_x = player_x - arm_offset + math.cos(math.radians(walk_angle)) * arm_length
arm1_y = base_y + math.sin(math.radians(walk_angle)) * arm_length

# Right arm calculation
arm2_x = player_x + arm_offset + math.cos(math.radians(walk_angle + 180)) * arm_length
arm2_y = base_y + math.sin(math.radians(walk_angle + 180)) * arm_length

# Draw the arms
pygame.draw.line(screen, RED, (player_x, base_y), (arm1_x, arm1_y), 5) # Left arm
pygame.draw.line(screen, RED, (player_x, base_y), (arm2_x, arm2_y), 5) # Right arm

# Walking logic for legs
if is_walking_left:
# Left leg moves forward (opposite of right leg)
leg1_x = player_x - leg_offset + math.cos(math.radians(walk_angle)) * leg_length
leg1_y = player_y + player_height // 2 - math.sin(math.radians(walk_angle)) * leg_length

# Right leg moves backward
leg2_x = player_x + leg_offset + math.cos(math.radians(walk_angle + 180)) * leg_length
leg2_y = player_y + player_height // 2 - math.sin(math.radians(walk_angle + 180)) * leg_length
else:
# Right leg moves forward (opposite of left leg)
leg1_x = player_x - leg_offset + math.cos(math.radians(walk_angle + 180)) * leg_length
leg1_y = player_y + player_height // 2 - math.sin(math.radians(walk_angle + 180)) * leg_length

# Left leg moves backward
leg2_x = player_x + leg_offset + math.cos(math.radians(walk_angle)) * leg_length
leg2_y = player_y + player_height // 2 - math.sin(math.radians(walk_angle)) * leg_length

# Draw the legs
pygame.draw.line(screen, RED, (player_x, player_y + player_height // 2), (leg1_x, leg1_y), 5) # Left leg
pygame.draw.line(screen, RED, (player_x, player_y + player_height // 2), (leg2_x, leg2_y), 5) # Right leg
< /code>
main.py:
import pygame
import sys
from player import draw_player

pygame.init()

# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
RED = (255, 0, 0)
BLACK = (0, 0, 0)

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Parkour Runner")

# Player variables
player_x = SCREEN_WIDTH // 2
player_y = SCREEN_HEIGHT - 100
player_height = 50
player_width = 7 # Set the body width to 7
walk_angle = 0 # Controls the swing of arms and legs
is_walking_left = False # Initially walking to the right
player_velocity_y = 0 # Player vertical velocity (for gravity)
on_ground = False # Whether the player is standing on a platform

# Platform data (x, y, width, height)
platforms = [
pygame.Rect(100, SCREEN_HEIGHT - 50, 50, 50), # First platform
pygame.Rect(250, SCREEN_HEIGHT - 100, 50, 100), # Second platform
pygame.Rect(400, SCREEN_HEIGHT - 150, 50, 150), # Third platform
]

# Main game loop
run = True
while run:
screen.fill((255, 255, 255)) # Fill screen with white background

# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

# Handle player movement
keys = pygame.key.get_pressed()
if keys[pygame.K_a]: # Move left
player_x -= 5
is_walking_left = True # Walking left
walk_angle += 5 # Increase the walking angle for animation
elif keys[pygame.K_d]: # Move right
player_x += 5
is_walking_left = False # Walking right
walk_angle += 5 # Increase the walking angle for animation

# Update player vertical movement (gravity)
if not on_ground:
player_velocity_y += 1 # Simulate gravity
else:
player_velocity_y = 0 # Reset velocity when on the ground

# Move player vertically
player_y += player_velocity_y

# Check for collision with platforms
on_ground = False
for platform in platforms:
# Check if the player is falling (is above the platform) and if they are within the platform's range
if player_y + player_height // 2 = platform.top:
if platform.left < player_x < platform.right: # Check if player is above the platform and within the horizontal range
player_y = platform.top - player_height // 2 # Place player on top of the platform
on_ground = True
break

# Clamp the player’s position to prevent falling off the screen
if player_y + player_height // 2 > SCREEN_HEIGHT:
player_y = SCREEN_HEIGHT - player_height // 2 # Keep player at the bottom of the screen

# Draw platforms
for platform in platforms:
pygame.draw.rect(screen, BLACK, platform)

# Draw the player
draw_player(screen, player_x, player_y, player_height, walk_angle, is_walking_left)

# Update the screen
pygame.display.flip()

# Frame rate (FPS)
pygame.time.Clock().tick(60)

# Quit Pygame
pygame.quit()
sys.exit()

< /code>
Я попытался изменить эту логику специально, чтобы разделить высоту игрока, чтобы каким -то образом отменить ее, но эта логика мне кажется звучной: < /p>
# Handle player movement
keys = pygame.key.get_pressed()
if keys[pygame.K_a]: # Move left
player_x -= 5
is_walking_left = True # Walking left
walk_angle += 5 # Increase the walking angle for animation
elif keys[pygame.K_d]: # Move right
player_x += 5
is_walking_left = False # Walking right
walk_angle += 5 # Increase the walking angle for animation

# Update player vertical movement (gravity)
if not on_ground:
player_velocity_y += 1 # Simulate gravity
else:
player_velocity_y = 0 # Reset velocity when on the ground

# Move player vertically
player_y += player_velocity_y

# Check for collision with platforms
on_ground = False
for platform in platforms:
# Check if the player is falling (is above the platform) and if they are within the platform's range
if player_y + player_height // 2 = platform.top:
if platform.left < player_x < platform.right: # Check if player is above the platform and within the horizontal range
player_y = platform.top - player_height // 2 # Place player on top of the platform
on_ground = True
break

# Clamp the player’s position to prevent falling off the screen
if player_y + player_height // 2 > SCREEN_HEIGHT:
player_y = SCREEN_HEIGHT - player_height // 2 # Keep player at the bottom of the screen


Подробнее здесь: https://stackoverflow.com/questions/793 ... t-yet-as-t

Вернуться в «Python»