Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- pygame.init()
- # Set up the screen
- WIDTH, HEIGHT = 500, 400
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("Move the Square")
- # Set up the clock
- clock = pygame.time.Clock()
- # Square settings
- square_color = (0, 200, 255)
- square_size = 50
- x = WIDTH // 2
- y = HEIGHT // 2
- speed = 5
- running = True
- while running:
- # Handle quitting the game
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- # Get key state
- keys = pygame.key.get_pressed()
- # Move the square
- if keys[pygame.K_LEFT]:
- x -= speed
- if keys[pygame.K_RIGHT]:
- x += speed
- if keys[pygame.K_UP]:
- y -= speed
- if keys[pygame.K_DOWN]:
- y += speed
- # Clear the screen and draw the square
- screen.fill((30, 30, 30))
- pygame.draw.rect(screen, square_color, (x, y, square_size, square_size))
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement