Untitled

 avatar
unknown
plain_text
9 months ago
1.5 kB
7
Indexable
import pygame
import sys

# Initialize Pygame
pygame.init()

# Constants
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
GRAVITY = 0.5
JUMP_STRENGTH = 10

# Colors
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# Player Class
class Player:
    def __init__(self):
        self.rect = pygame.Rect(100, SCREEN_HEIGHT - 150, 50, 50)
        self.velocity_y = 0
        self.is_jumping = False

    def jump(self):
        if not self.is_jumping:
            self.velocity_y = -JUMP_STRENGTH
            self.is_jumping = True

    def update(self):
        self.velocity_y += GRAVITY
        self.rect.y += self.velocity_y

        # Ground collision
        if self.rect.y >= SCREEN_HEIGHT - 50:
            self.rect.y = SCREEN_HEIGHT - 50
            self.is_jumping = False
            self.velocity_y = 0

# Setup
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Simple Platformer")
clock = pygame.time.Clock()
player = Player()

# Game loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player.jump()

    # Update
    player.update()

    # Drawing
    screen.fill(WHITE)
    pygame.draw.rect(screen, BLUE, player.rect)
    pygame.draw.rect(screen, GREEN, (0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50))  # Ground
    pygame.display.flip()
    clock.tick(60)
Editor is loading...
Leave a Comment