Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import sys, pygame, os, math
- from pygame.locals import *
- running = True
- fps = 20
- class Point(object):
- def __init__(self, val=(0, 0)):
- self.x, self.y = val[0], val[1]
- def __add__(self, other):
- return Point((self.x + other.x, self.y + other.y))
- class Platform(object):
- def __init__(self, startAngle, endAngle, radius, color, width):
- self.start_angle = startAngle
- self.stop_angle = endAngle
- self.stop_radius = radius
- self.start_radius = radius - width
- self.radius = width / 2
- self.mid_radius = self.start_radius + self.radius
- self.rect = pygame.Rect(mid[0] - self.stop_radius, mid[1] - self.stop_radius, self.stop_radius * 2, self.stop_radius * 2)
- self.color = color
- self.width = width
- def draw(self, surface):
- pygame.draw.arc(surface, self.color, self.rect, (math.pi * 2) - self.stop_angle, (math.pi * 2) - self.start_angle, self.width)
- def collidePoint(self, point):
- if point.x > self.start_angle and point.x < self.stop_angle:
- if point.y > self.start_radius and point.y < self.stop_radius:
- return True
- def circleToTPA(radius):
- w = (radius * 2) + 1
- tpa = [[False] * w for i in range(w)]
- rs = radius**2
- for y in range(0, radius):
- mx = int((rs - y**2)**0.5)
- tpa[radius - y][radius - mx] = True
- tpa[radius - y][radius + mx] = True
- tpa[radius + y][radius - mx] = True
- tpa[radius + y][radius + mx] = True
- for x in range(0, radius):
- my = int((rs - x**2)**0.5)
- tpa[radius - my][radius - x] = True
- tpa[radius - my][radius + x] = True
- tpa[radius + my][radius - x] = True
- tpa[radius + my][radius + x] = True
- res = []
- for y in range(w):
- for x in range(w):
- if tpa[y][x]:
- res.append(Point((x - radius, y - radius)))
- return res
- def shiftTPA(tpa, offset):
- res = []
- p = []
- for pt in tpa:
- t = Point((pt.x, pt.y))
- if offset.y != -t.y:
- t.x /= offset.y + t.y
- else:
- t.x /= offset.y
- res.append(t + offset)
- return res
- class Player(object):
- def __init__(self):
- self.rect = pygame.Rect(0, 0, 16, 16)
- self.pos = Point()
- self.rad = 8
- self.vel = Point()
- self.mass = 1
- self.outline = circleToTPA(self.rad)
- def move(self, dx, dy, platforms):
- if dx != 0:
- self.moveAxis(dx / (fps * self.pos.y), 0, platforms)
- if dy != 0:
- self.moveAxis(0, dy / fps, platforms)
- def applyForce(self, fx, fy):
- self.vel.x += fx / self.mass
- self.vel.y += fy / self.mass
- def moveAxis(self, dx, dy, platforms):
- if dx == 0 and dy == 0:
- return False
- if dx != 0:
- tmp = Point((self.pos.x, self.pos.y))
- tmp.x += dx
- tmp.x %= 2 * math.pi
- yvel = self.vel.y
- tpa = shiftTPA(self.outline, tmp)
- col = False
- for platform in platforms:
- for pt in tpa:
- if platform.collidePoint(pt):
- return
- else:
- tmp = Point((self.pos.x, self.pos.y))
- tmp.y += dy
- if tmp.y < 1:
- tmp.y = 1
- yvel = 0
- else:
- yvel = self.vel.y
- tpa = shiftTPA(self.outline, tmp)
- col = False
- for platform in platforms:
- for pt in tpa:
- if platform.collidePoint(pt):
- self.vel.y = 0
- return
- self.pos.x, self.pos.y = tmp.x, tmp.y
- self.vel.y = yvel
- self.rect.centerx = self.pos.y * math.cos(self.pos.x)
- self.rect.centery = self.pos.y * math.sin(self.pos.x)
- def draw(self, screen):
- pygame.draw.circle(screen, (255, 0, 0), (self.rect.centerx + mid[0], self.rect.centery + mid[1]), self.rad, 0)
- def arcToRect(startAngle, endAngle, startRadius, endRadius):
- return Pygame.Rect(startRadius * math.cos(startAngle), startRadius * math.sin(startAngle), startRadius * (math.cos(endAngle) - math.cos(startAngle)), endRadius - startRadius)
- def getEvents():
- for event in pygame.event.get():
- if event.type == KEYDOWN:
- if event.key == pygame.K_UP:
- player.applyForce(0, 64)
- if event.key == pygame.K_DOWN:
- pass
- if event.key == pygame.K_LEFT:
- player.applyForce(-math.pi * 32, 0)
- if event.key == pygame.K_RIGHT:
- player.applyForce( math.pi * 32, 0)
- if event.key == ord('p'):
- print(player.vel.y, player.pos.y)
- if event.type == KEYUP:
- if event.key == pygame.K_UP:
- pass
- if event.key == pygame.K_DOWN:
- pass
- if event.key == pygame.K_LEFT:
- player.vel.x = 0
- if event.key == pygame.K_RIGHT:
- player.vel.x = 0
- if event.type == pygame.QUIT:
- running = False
- sys.exit()
- pygame.init()
- size = (608, 608)
- mid = (304, 304)
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption('Grav the Circle')
- pygame.mouse.set_visible(0)
- clock = pygame.time.Clock()
- player = Player()
- player.pos.y = 50
- player.pos.x = 0
- player.rect.x = player.pos.y * math.cos(player.pos.x)
- player.rect.y = player.pos.y * math.sin(player.pos.x)
- platforms = []
- platforms.append(Platform(0, math.pi / 2, 100, (0, 0, 255), 16))
- platforms.append(Platform(math.pi, math.pi + math.pi / 2, 150, (0, 0, 255), 32))
- while running:
- screen.fill((0, 0, 0))
- getEvents()
- player.move(player.vel.x, player.vel.y, platforms)
- player.vel.y -= 29.4 / fps
- for platform in platforms:
- platform.draw(screen)
- player.draw(screen)
- pygame.display.flip()
- clock.tick(fps)
- print('Terminating')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement