-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite_collision.py
More file actions
66 lines (48 loc) · 2.26 KB
/
Copy pathSprite_collision.py
File metadata and controls
66 lines (48 loc) · 2.26 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
54
55
56
57
58
59
60
61
62
63
64
65
66
import pygame
import random
SCREEN_WIDTH, SCREEN_HEIGHT = 500, 400
MOVEMENT_SPEED = 5
FONT_SIZE = 72
pygame.init()
background_image = pygame.transform.scale(pygame.image.load("Bg.png"), (SCREEN_WIDTH, SCREEN_HEIGHT))
font = pygame.font.SysFont("Times New Roman", FONT_SIZE)
class Sprite(pygame.sprite.Sprite):
def __init__(self, color, height, width):
super().__init__()
self.image = pygame.Surface([width, height])
pygame.draw.rect(self.image, color, pygame.Rect(0, 0, width, height))
self.rect = self.image.get_rect()
def move(self, x_change, y_change):
self.rect.x = max(0, min(self.rect.x + x_change, SCREEN_WIDTH - self.rect.width))
self.rect.y = max(0, min(self.rect.y + y_change, SCREEN_HEIGHT - self.rect.height))
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Sprite Collision")
all_sprites = pygame.sprite.Group()
sprite1 = Sprite(pygame.Color('black'), 20, 30)
sprite1.rect.x, sprite1.rect.y = random.randint(0, SCREEN_WIDTH - sprite1.rect.width), random.randint(0, SCREEN_HEIGHT - sprite1.rect.height)
all_sprites.add(sprite1)
sprite2 = Sprite(pygame.Color('red'), 20, 30)
sprite2.rect.x, sprite2.rect.y = random.randint(0, SCREEN_WIDTH - sprite2.rect.width), random.randint(0, SCREEN_HEIGHT - sprite2.rect.height)
all_sprites.add(sprite2)
running, won = True, False
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_x):
running = False
if not won:
keys = pygame.key.get_pressed()
x_change = (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * MOVEMENT_SPEED
y_change = (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * MOVEMENT_SPEED
sprite1.move(x_change, y_change)
if sprite1.rect.colliderect(sprite2.rect):
all_sprites.remove(sprite2)
won = True
screen.blit(background_image, (0, 0))
all_sprites.draw(screen)
if won:
win_text = font.render("You win!", True, pygame.Color('black'))
screen.blit(win_text, ((SCREEN_WIDTH - win_text.get_width()) // 2, (SCREEN_HEIGHT - win_text.get_height()) // 2))
pygame.display.flip()
clock.tick(60)
pygame.quit()