Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, time, random
- import pygame
- class c:
- def __init__(s, x, y, r):
- s.rect = pygame.Rect(x - r // 2, y - r // 2, r * 2, r * 2)
- s.rad = r
- def draw(s):
- global PDS, VP
- if s.rect.colliderect(VP):
- #print(s.rect.centerx)
- pygame.draw.circle(PDS, (255, 255, 255), (s.rect.centerx - VP.x, s.rect.centery - VP.y), s.rad)
- pygame.init()
- PDR = pygame.Rect(0, 0, 1024, 720)
- PDCENTER = pygame.math.Vector2(PDR.center)
- PDS = pygame.display.set_mode(PDR.size)
- FPS = 120
- # viewport or camera
- VP = pygame.Rect(0, 0, 1024, 720)
- PLAYER_RADIUS = pygame.math.Vector2(20)
- PLAYER = pygame.Rect(PDCENTER - PLAYER_RADIUS, PLAYER_RADIUS * 2)
- # generate NPCs
- circles = [c(random.randint(0, 10000), random.randint(0, 4000), random.randint(5, 20)) for i in range(100)]
- while True:
- events = pygame.event.get()
- for e in events:
- if e.type == pygame.QUIT: sys.exit()
- PDS.fill((0, 0, 0))
- # default player is green
- playerColor = (0, 255, 0)
- # draw NPC
- for circle in circles:
- circle.draw()
- if circle.rect.colliderect((PLAYER.x + VP.x, PLAYER.y + VP.y), PLAYER_RADIUS * 2):
- playerColor = (255, 0, 0) # player turns red if collides with NPC
- # draw player in red
- pygame.draw.circle(PDS, playerColor, PLAYER.center, PLAYER_RADIUS[0])
- pygame.display.update()
- k = pygame.key.get_pressed()
- if k[pygame.K_RIGHT]:
- VP.move_ip(1, 0)
- if k[pygame.K_LEFT]:
- VP.move_ip(-1, 0)
- if k[pygame.K_UP]:
- VP.move_ip(0, -1)
- if k[pygame.K_DOWN]:
- VP.move_ip(0, 1)
- pygame.quit()
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement