-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd_sprites.py
More file actions
53 lines (36 loc) · 1.16 KB
/
Copy pathAdd_sprites.py
File metadata and controls
53 lines (36 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#Write a Python program to create a Game screen and add two rectangular sprites to it using the Pygame library.
# Add controls to one of the sprites as well for up, down, left and right movements.
import pygame
import sys
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Rectangular Sprite Game")
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
sprite_speed = 5
sprite1 = pygame.Rect(100, 100, 50, 50)
sprite2 = pygame.Rect(400, 300, 50, 50)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
sprite2.x -= sprite_speed
if keys[pygame.K_RIGHT]:
sprite2.x += sprite_speed
if keys[pygame.K_UP]:
sprite2.y -= sprite_speed
if keys[pygame.K_DOWN]:
sprite2.y += sprite_speed
screen.fill(WHITE)
pygame.draw.rect(screen, RED, sprite1)
pygame.draw.rect(screen, BLUE, sprite2)
pygame.display.flip()
pygame.time.Clock().tick(60)
pygame.quit()
sys.exit()