Advertisement
mixster

mixster

Sep 18th, 2010
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.14 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys, pygame, os, math
  3. from pygame.locals import *
  4.  
  5. running = True
  6. fps = 20
  7.  
  8. class Point(object):
  9.     def __init__(self, val=(0, 0)):
  10.         self.x, self.y = val[0], val[1]
  11.  
  12.     def __add__(self, other):
  13.         return Point((self.x + other.x, self.y + other.y))
  14.  
  15. class Platform(object):
  16.     def __init__(self, startAngle, endAngle, radius, color, width):
  17.         self.start_angle  = startAngle
  18.         self.stop_angle  = endAngle
  19.         self.stop_radius = radius
  20.         self.start_radius = radius - width
  21.         self.radius = width / 2
  22.         self.mid_radius = self.start_radius + self.radius
  23.         self.rect = pygame.Rect(mid[0] - self.stop_radius, mid[1] - self.stop_radius, self.stop_radius * 2, self.stop_radius * 2)
  24.         self.color = color
  25.         self.width  = width
  26.  
  27.     def draw(self, surface):
  28.         pygame.draw.arc(surface, self.color, self.rect, (math.pi * 2) - self.stop_angle, (math.pi * 2) - self.start_angle, self.width)
  29.  
  30.     def collidePoint(self, point):
  31.         if point.x > self.start_angle and point.x < self.stop_angle:
  32.             if point.y > self.start_radius and point.y < self.stop_radius:
  33.                 return True
  34.  
  35. def circleToTPA(radius):
  36.     w = (radius * 2) + 1
  37.     tpa = [[False] * w for i in range(w)]
  38.        
  39.     rs = radius**2
  40.     for y in range(0, radius):
  41.         mx = int((rs - y**2)**0.5)
  42.         tpa[radius - y][radius - mx] = True
  43.         tpa[radius - y][radius + mx] = True
  44.         tpa[radius + y][radius - mx] = True
  45.         tpa[radius + y][radius + mx] = True
  46.  
  47.     for x in range(0, radius):
  48.         my = int((rs - x**2)**0.5)
  49.         tpa[radius - my][radius - x] = True
  50.         tpa[radius - my][radius + x] = True
  51.         tpa[radius + my][radius - x] = True
  52.         tpa[radius + my][radius + x] = True
  53.  
  54.     res = []
  55.     for y in range(w):
  56.         for x in range(w):
  57.             if tpa[y][x]:
  58.                 res.append(Point((x - radius, y - radius)))
  59.  
  60.     return res
  61.  
  62. def shiftTPA(tpa, offset):
  63.     res = []
  64.     p = []
  65.     for pt in tpa:
  66.         t = Point((pt.x, pt.y))
  67.         if offset.y != -t.y:
  68.             t.x /= offset.y + t.y
  69.         else:
  70.             t.x /= offset.y
  71.  
  72.         res.append(t + offset)
  73.  
  74.     return res
  75.  
  76. class Player(object):
  77.     def __init__(self):
  78.         self.rect = pygame.Rect(0, 0, 16, 16)
  79.         self.pos  = Point()
  80.         self.rad  = 8
  81.         self.vel  = Point()
  82.         self.mass = 1
  83.         self.outline = circleToTPA(self.rad)
  84.  
  85.     def move(self, dx, dy, platforms):
  86.         if dx != 0:
  87.             self.moveAxis(dx / (fps * self.pos.y), 0, platforms)
  88.         if dy != 0:
  89.             self.moveAxis(0, dy / fps, platforms)
  90.        
  91.     def applyForce(self, fx, fy):
  92.         self.vel.x += fx / self.mass
  93.         self.vel.y += fy / self.mass
  94.  
  95.     def moveAxis(self, dx, dy, platforms):
  96.         if dx == 0 and dy == 0:
  97.             return False
  98.  
  99.         if dx != 0:
  100.             tmp = Point((self.pos.x, self.pos.y))
  101.             tmp.x += dx
  102.             tmp.x %= 2 * math.pi
  103.             yvel = self.vel.y
  104.  
  105.             tpa = shiftTPA(self.outline, tmp)
  106.             col = False
  107.  
  108.             for platform in platforms:
  109.                 for pt in tpa:
  110.                     if platform.collidePoint(pt):
  111.                         return
  112.  
  113.         else:
  114.             tmp = Point((self.pos.x, self.pos.y))
  115.             tmp.y += dy
  116.             if tmp.y < 1:
  117.                 tmp.y = 1
  118.                 yvel = 0
  119.             else:
  120.                 yvel = self.vel.y
  121.  
  122.             tpa = shiftTPA(self.outline, tmp)
  123.             col = False
  124.  
  125.             for platform in platforms:
  126.                 for pt in tpa:
  127.                     if platform.collidePoint(pt):
  128.                         self.vel.y = 0
  129.                         return
  130.  
  131.         self.pos.x, self.pos.y = tmp.x, tmp.y
  132.         self.vel.y = yvel
  133.         self.rect.centerx = self.pos.y * math.cos(self.pos.x)
  134.         self.rect.centery = self.pos.y * math.sin(self.pos.x)
  135.  
  136.     def draw(self, screen):
  137.         pygame.draw.circle(screen, (255, 0, 0), (self.rect.centerx + mid[0], self.rect.centery + mid[1]), self.rad, 0)
  138.  
  139. def arcToRect(startAngle, endAngle, startRadius, endRadius):
  140.     return Pygame.Rect(startRadius * math.cos(startAngle), startRadius * math.sin(startAngle), startRadius * (math.cos(endAngle) - math.cos(startAngle)), endRadius - startRadius)
  141.  
  142. def getEvents():
  143.     for event in pygame.event.get():
  144.         if event.type == KEYDOWN:
  145.             if event.key == pygame.K_UP:
  146.                 player.applyForce(0, 64)
  147.             if event.key == pygame.K_DOWN:
  148.                 pass
  149.             if event.key == pygame.K_LEFT:
  150.                 player.applyForce(-math.pi * 32, 0)
  151.             if event.key == pygame.K_RIGHT:
  152.                 player.applyForce( math.pi * 32, 0)
  153.             if event.key == ord('p'):
  154.                 print(player.vel.y, player.pos.y)
  155.         if event.type == KEYUP:
  156.             if event.key == pygame.K_UP:
  157.                 pass
  158.             if event.key == pygame.K_DOWN:
  159.                 pass
  160.             if event.key == pygame.K_LEFT:
  161.                 player.vel.x = 0
  162.             if event.key == pygame.K_RIGHT:
  163.                 player.vel.x = 0
  164.         if event.type == pygame.QUIT:
  165.             running = False
  166.             sys.exit()
  167.  
  168.  
  169. pygame.init()
  170. size = (608, 608)
  171. mid  = (304, 304)
  172. screen = pygame.display.set_mode(size)
  173. pygame.display.set_caption('Grav the Circle')
  174. pygame.mouse.set_visible(0)
  175.  
  176. clock = pygame.time.Clock()
  177.  
  178. player = Player()
  179. player.pos.y = 50
  180. player.pos.x = 0
  181. player.rect.x = player.pos.y * math.cos(player.pos.x)
  182. player.rect.y = player.pos.y * math.sin(player.pos.x)
  183.  
  184. platforms = []
  185. platforms.append(Platform(0, math.pi / 2, 100, (0, 0, 255), 16))
  186. platforms.append(Platform(math.pi, math.pi + math.pi / 2, 150, (0, 0, 255), 32))
  187.  
  188. while running:
  189.     screen.fill((0, 0, 0))
  190.     getEvents()
  191.     player.move(player.vel.x, player.vel.y, platforms)
  192.     player.vel.y -= 29.4 / fps
  193.     for platform in platforms:
  194.         platform.draw(screen)
  195.     player.draw(screen)
  196.     pygame.display.flip()
  197.     clock.tick(fps)
  198.  
  199. print('Terminating')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement