0% found this document useful (0 votes)
19 views3 pages

Metin

sdasdsadsad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Metin

sdasdsadsad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame

import sys

# Initialize Pygame
pygame.init()

# Set screen to half the display width


# This configures the game window to half of the screen width and full screen
height
full_screen_info = pygame.display.Info()
SCREEN_WIDTH, SCREEN_HEIGHT = full_screen_info.current_w // 2,
full_screen_info.current_h // 2
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Game")

# Colors (background is black, player is white)


BLACK = (225, 225, 225)
WHITE = (0, 0, 0)

# Player settings
player_size = 40 # Player size in pixels
player_x, player_y = SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 # Player start position
at the center
player_vel_x, player_vel_y = 0, 0 # Initial player velocities
walk_speed = 5 # Maximum walking speed
jump_force = 10 # Force applied for jumping
gravity = 0.5 # Gravity effect on player (pulling down)
on_ground = False # Tracks if player is on the ground
air_cntrl_pwr = 15 # Air control adjustment (to limit horizontal speed in the air)
grnd_cntrl_pwr = 10 # Ground control adjustment (for horizontal speed on the
ground)

# Wall jump and dash settings


wall_jump_force_x = 6 # Horizontal force applied during wall jump
wall_jump_force_y = 10 # Vertical force applied during wall jump
dash_speed = 12 # Speed during dash
dash_duration = 200 # Duration of dash in milliseconds
can_dash = True # Track if dash is available
last_dash_time = 0 # Track last time dash was used for cooldown
is_dashing = False # Check if player is currently dashing
dash_direction_x = 0 # Dash direction for x-axis
dash_direction_y = 0 # Dash direction for y-axis

# Wall slide settings


wall_slide_speed = 1.5 # Speed when sliding down walls

# Clock for FPS (frames per second)


clock = pygame.time.Clock()

# Game loop
running = True
while running:
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

# Movement and controls


keys = pygame.key.get_pressed()

# Ground movement control


if on_ground:
# Horizontal movement on ground
if keys[pygame.K_a]: # Move left
player_vel_x -= walk_speed / grnd_cntrl_pwr
if player_vel_x < -walk_speed: # Limit to max speed
player_vel_x = -walk_speed
elif keys[pygame.K_d]: # Move right
player_vel_x += walk_speed / grnd_cntrl_pwr
if player_vel_x > walk_speed:
player_vel_x = walk_speed
else:
# Deceleration if no left/right keys are pressed
if player_vel_x < 0:
player_vel_x += walk_speed / grnd_cntrl_pwr
if player_vel_x > 0:
player_vel_x = 0
elif player_vel_x > 0:
player_vel_x -= walk_speed / grnd_cntrl_pwr
if player_vel_x < 0:
player_vel_x = 0
else:
# Air control (less responsive than ground movement)
if keys[pygame.K_a]:
player_vel_x -= walk_speed / air_cntrl_pwr
if player_vel_x < -walk_speed:
player_vel_x = -walk_speed
elif keys[pygame.K_d]:
player_vel_x += walk_speed / air_cntrl_pwr
if player_vel_x > walk_speed:
player_vel_x = walk_speed

# Jumping on the ground


if keys[pygame.K_SPACE] and on_ground:
player_vel_y = -jump_force # Apply jump force upward
on_ground = False # Player leaves the ground

# Wall Slide logic


# Check if the player is touching left or right wall while falling
touching_wall_left = player_x <= 0
touching_wall_right = player_x + player_size >= SCREEN_WIDTH
if (touching_wall_left or touching_wall_right) and not on_ground and
player_vel_y > 0:
player_vel_y = wall_slide_speed # Slow down descent while sliding

# Wall Jump logic


if keys[pygame.K_SPACE] and (touching_wall_left or touching_wall_right) and not
on_ground:
player_vel_y = -wall_jump_force_y # Apply upward force
if touching_wall_left: # Push right if on the left wall
player_vel_x = wall_jump_force_x
elif touching_wall_right: # Push left if on the right wall
player_vel_x = -wall_jump_force_x

# Dash logic with 8-direction support


if keys[pygame.K_LSHIFT] and can_dash and not is_dashing and not on_ground:
is_dashing = True
dash_direction_x = 0
dash_direction_y = 0
# Determine dash direction based on key inputs
if keys[pygame.K_UP]:
dash_direction_y = -1
elif keys[pygame.K_DOWN]:
dash_direction_y = 1
if keys[pygame.K_a]:
dash_direction_x = -1
elif keys[pygame.K_d]:
dash_direction_x = 1

# Normalize dash speed for diagonal dashes


if dash_direction_x != 0 and dash_direction_y != 0:
dash_direction_x *= 0.707 # Diagonal factor for consistent dash speed
dash_direction_y *= 0.707

last_dash_time = pygame.time.get_ticks() # Record dash time for cooldown


can_dash = False # Prevent multiple dashes until reset

# Apply dash movement if dashing


if is_dashing:
if pygame.time.get_ticks() - last_dash_time <= dash_duration:
player_vel_x = dash_direction_x * dash_speed
player_vel_y = dash_direction_y * dash_speed
else:
is_dashing = False # End dash after dash duration

# Gravity applies if player is not dashing


if not is_dashing:
player_vel_y += gravity

# Update player position with calculated velocity


player_x += player_vel_x
player_y += player_vel_y

# Ground collision detection


if player_y + player_size > SCREEN_HEIGHT:
player_y = SCREEN_HEIGHT - player_size # Stop at ground level
player_vel_y = 0 # Stop vertical movement
on_ground = True # Reset ground state
can_dash = True # Reset dash ability
else:
on_ground = False # Player is in the air

# Wall collision detection to keep player within bounds


if player_x < 0:
player_x = 0
elif player_x + player_size > SCREEN_WIDTH:
player_x = SCREEN_WIDTH - player_size

# Render the game visuals


screen.fill(BLACK) # Background color
pygame.draw.rect(screen, WHITE, (player_x, player_y, player_size, player_size))
# Draw player

# Refresh the screen and maintain consistent FPS


pygame.display.flip()
clock.tick(60) # Limit frame rate to 60 FPS

You might also like