Untitled
unknown
plain_text
a year ago
2.8 kB
3
Indexable
import pygame # Pygame setup pygame.init() # set screen params screen = pygame.display.set_mode((1280, 720)) clock = pygame.time.Clock() running = True dt = 0 # Load the image for your entity (replace "siurek.png" with the actual path to your image) entity_image = pygame.image.load("siurek.png") class Entity: def __init__(self, image, x, y): self.image = image self.rect = self.image.get_rect() self.rect.center = (x, y) self.velocity = pygame.Vector2(0, 0) # Initialize entity's velocity self.gravity = pygame.Vector2(0, 1) # Define gravity vector self.move_speed = 7 # Set the movement speed self.on_ground = False # Flag to track if the entity is on the ground def draw(self, screen): screen.blit(self.image, self.rect) def update(self, dt): # Apply gravity to the vertical velocity self.velocity += self.gravity # Update the entity's position based on velocity self.rect.x += self.velocity.x self.rect.y += self.velocity.y # Prevent the entity from falling below the screen if self.rect.y > screen.get_height() - self.rect.height: self.rect.y = screen.get_height() - self.rect.height self.velocity.y = 0 self.on_ground = True else: self.on_ground = False # Prevent the entity from moving past the screen boundaries if self.rect.x < 0: self.rect.x = 0 self.velocity.x = 0 elif self.rect.x > screen.get_width() - self.rect.width: self.rect.x = screen.get_width() - self.rect.width self.velocity.x = 0 player = Entity(entity_image, screen.get_width() / 2, 0) # Start the player at the top while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill((255, 255, 255)) # Fill the screen with white player.update(dt) # Update the player's position # Draw the entity on the screen player.draw(screen) keys = pygame.key.get_pressed() if keys[pygame.K_w] and player.on_ground: player.velocity.y = -15 # Jump by setting a negative vertical velocity player.on_ground = False # Update the on_ground flag if keys[pygame.K_a]: player.velocity.x = -player.move_speed # Move left by setting a negative horizontal velocity if keys[pygame.K_d]: player.velocity.x = player.move_speed # Move right by setting a positive horizontal velocity if not (keys[pygame.K_a] or keys[pygame.K_d]): player.velocity.x = 0 # Stop horizontal movement when no keys are pressed pygame.display.flip() dt = clock.tick(60) / 1000 pygame.quit()
Editor is loading...