|
| 1 | +import os |
| 2 | +import random |
| 3 | +import sys |
| 4 | + |
| 5 | +import pygame |
| 6 | +from pygame.locals import * |
| 7 | + |
| 8 | + |
| 9 | +class Satyr(pygame.sprite.Sprite): |
| 10 | + def __init__(self, position, ipath, scale=0.5): |
| 11 | + pygame.sprite.Sprite.__init__(self) |
| 12 | + |
| 13 | + img = pygame.image.load(ipath) |
| 14 | + rect = img.get_rect(center=position) |
| 15 | + w, h = rect.size[0], rect.size[1] |
| 16 | + w, h = int(w * scale), int(h * scale) |
| 17 | + img = pygame.transform.scale(img, (w, h)) |
| 18 | + |
| 19 | + rect = img.get_rect(center=position) |
| 20 | + |
| 21 | + self.image = img |
| 22 | + self.rect = rect |
| 23 | + self.rect.center = position |
| 24 | + |
| 25 | + def draw(self, surface, position): |
| 26 | + self.rect.center = position |
| 27 | + surface.blit(self.image, position) |
| 28 | + |
| 29 | + |
| 30 | +class Image(pygame.sprite.Sprite): |
| 31 | + def __init__(self, position, ipath, scale=1.0): |
| 32 | + pygame.sprite.Sprite.__init__(self) |
| 33 | + |
| 34 | + img = pygame.image.load(ipath) |
| 35 | + w, h = img.get_rect().size[0], img.get_rect().size[1] |
| 36 | + w, h = int(w * scale), int(h * scale) |
| 37 | + img = pygame.transform.scale(img, (w, h)) |
| 38 | + |
| 39 | + rect = img.get_rect(center=position) |
| 40 | + |
| 41 | + self.image = img |
| 42 | + self.rect = rect |
| 43 | + self.rect.center = position |
| 44 | + self.dx = 1 if random.random() < 0.5 else -1 |
| 45 | + self.dy = 1 if random.random() < 0.5 else 1 |
| 46 | + |
| 47 | + def draw(self, surface): |
| 48 | + surface.blit(self.image, self.rect.center) |
| 49 | + |
| 50 | + def update(self, width, height): |
| 51 | + x, y = self.rect.center |
| 52 | + |
| 53 | + if x + self.image.get_rect().size[0] >= width: |
| 54 | + self.dx = -1 |
| 55 | + elif x <= 0: |
| 56 | + self.dx = 1 |
| 57 | + |
| 58 | + if y + self.image.get_rect().size[1] >= height: |
| 59 | + self.dy = -1 |
| 60 | + elif y <= 0: |
| 61 | + self.dy = 1 |
| 62 | + |
| 63 | + x, y = x + 1 * self.dx, y + 1 * self.dy |
| 64 | + self.rect.center = x, y |
| 65 | + |
| 66 | + |
| 67 | +def start(): |
| 68 | + pygame.init() |
| 69 | + |
| 70 | + FPS = 30 |
| 71 | + width = 400 |
| 72 | + height = 400 |
| 73 | + DISPLAYSURF = pygame.display.set_mode((width, height)) |
| 74 | + DISPLAYSURF.fill((255, 255, 255)) |
| 75 | + pygame.display.set_caption('Key Events') |
| 76 | + fps_clock = pygame.time.Clock() |
| 77 | + |
| 78 | + satyr = Satyr((200, 200), './images/Satyr_01_Idle_000.png', scale=0.25) |
| 79 | + |
| 80 | + ball = Image((random.randint(0, width), random.randint(0, height)), './images/ball.png', scale=0.25) |
| 81 | + ball_group = pygame.sprite.Group() |
| 82 | + ball_group.add(ball) |
| 83 | + |
| 84 | + while True: |
| 85 | + for event in pygame.event.get(): |
| 86 | + if event.type == QUIT: |
| 87 | + pygame.quit() |
| 88 | + sys.exit() |
| 89 | + |
| 90 | + DISPLAYSURF.fill((255, 255, 255, 0)) |
| 91 | + |
| 92 | + if pygame.sprite.spritecollide(satyr, ball_group, False): |
| 93 | + print(f'collided {ball.rect.center}') |
| 94 | + |
| 95 | + satyr.draw(DISPLAYSURF, pygame.mouse.get_pos()) |
| 96 | + ball.draw(DISPLAYSURF) |
| 97 | + ball.update(width, height) |
| 98 | + |
| 99 | + pygame.display.update() |
| 100 | + fps_clock.tick(FPS) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == '__main__': |
| 104 | + os.environ['SDL_VIDEO_CENTERED'] = '1' |
| 105 | + start() |
0 commit comments