Advertisement
Guest User

Untitled

a guest
May 28th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. from scene import *
  2.  
  3. from Vectors import *
  4.  
  5. from Panels import *
  6.  
  7. import sound
  8.  
  9. import math
  10.  
  11. import time
  12.  
  13. import random
  14.  
  15. class Flapper(Panel):
  16. def secondaryInit(self, velocity):
  17. self.velocity = velocity
  18. def doGravity(self, gravity):
  19. self.velocity += gravity
  20. def doTouch(self):
  21. self.velocity.y = 13.0 if self.velocity.y <= 0 else self.velocity.y + 7# = Vec2(0.0, 15.0)
  22. def doVelocity(self):
  23. self.x += self.velocity.x
  24. self.y += self.velocity.y
  25. def collideWithWall(self, wall):
  26. #wall should be a panel object
  27. #simple square collision
  28. return wall.isTouched(self.x, self.y) or wall.isTouched(self.x + self.w, self.y) or wall.isTouched(self.x, self.y + self.h) or wall.isTouched(self.x + self.w, self.y + self.h)
  29.  
  30.  
  31.  
  32. class MyScene (Scene):
  33. def genWall(self):
  34. #walls are 2 70 px thick panels and have a 200 px opening to go through
  35. #there should always be a 50 px gap between the ground and the wall opening
  36. #wall colors and alphas are pre-defined in the draw function so no need to set3 them here
  37. #use 2000 once the camera thing is set up
  38. #there is a 50x180 px goal piece in between the walls
  39. midstart = random.randint(50, self.bounds.h - 250)
  40. self.walls.append(Panel(self.flapper.x + self.bounds.w - 270, 0, 70, midstart, 0.0, 0.0, 0.0, 0.0))#bottom (?)
  41. self.walls.append(Panel(self.flapper.x + self.bounds.w - 270, midstart + 200, 70, self.bounds.h, 0.0, 0.0, 0.0, 0.0))#top (?)
  42. self.goals.append(Panel(self.flapper.x + self.bounds.w - 260, midstart + 10, 50, 180, 0.0, 0.0, 0.0, 0.0))
  43. def cullExcessWalls(self):
  44. wallsCopy = self.walls[:]
  45. for i in range(len(wallsCopy)):
  46. if wallsCopy[i].x < self.flapper.x - 1000:
  47. self.walls.pop(i)
  48. def setup(self):
  49. # This will be called before the first frame is drawn.
  50. self.flapper = Flapper(10.0, 300.0, 50, 50, 1.0, 0.0, 0.0, 1.0)
  51. self.flapper.secondaryInit(Vec2(6.0, 0.0))
  52. self.walls = []
  53. self.goals = []
  54. self.deathparticles = []
  55. self.ticks = 0
  56. self.points = 0
  57. self.deadtime = 0.0
  58. self.gamestate = "play"
  59. sound.load_effect('Jump')
  60. sound.load_effect('Clank')
  61. def draw(self):
  62. background(0.0, 0.0, 0.4)
  63. if (self.gamestate == "play"):
  64. #fill(self.flapper.r, self.flapper.g, self.flapper.b, self.flapper.a)
  65. #rect(300, self.flapper.y, self.flapper.w, self.flapper.h)
  66. image('_Image_4', 300, self.flapper.y, self.flapper.w, self.flapper.h)
  67. self.flapper.doGravity(Vec2(0.0, -.63))
  68. self.flapper.doVelocity()
  69.  
  70. fill(0.0, 1.0, 0.0, 1.0)
  71. for i in self.walls:
  72. rect(i.x - self.flapper.x + 300, i.y, i.w, i.h)
  73.  
  74. fill(0.8, 0.5, 0.0, 1.0)
  75. for i in self.goals:
  76. rect(i.x - self.flapper.x + 300, i.y, i.w, i.h)
  77.  
  78. if (self.ticks % 100 == 0):
  79. self.genWall()
  80. self.cullExcessWalls()
  81.  
  82. for i in self.goals:
  83. if (self.flapper.collideWithWall(i)):
  84. self.points += 1
  85. self.goals.pop(self.goals.index(i))
  86. break
  87.  
  88. for i in self.walls:
  89. if (self.flapper.collideWithWall(i)):
  90. self.gamestate = "dead"
  91. sound.play_effect('Clank', 1.0)
  92. self.deadtime = time.time()
  93. for i in range(219):
  94. z = Flapper(300 + self.flapper.w / 2, self.flapper.y + self.flapper.h / 2, 7.0, 7.0, 1.0, 0.0, 0.0, 0.0)
  95. xdir = random.random() - .5
  96. ydir = random.random() - .5
  97. dist = math.sqrt(xdir ** 2 + ydir ** 2)
  98. if dist == 0:
  99. xdir = 0
  100. ydir = 0
  101. else:
  102. xdir /= dist
  103. ydir /= dist
  104. z.secondaryInit(Vec2(xdir, ydir) * random.randint(0, 20))
  105. self.deathparticles.append(z)
  106. break
  107.  
  108. tint(1.0, 1.0, 1.0, 1.0)
  109. text("Points: " + str(self.points), "Arial", 20.0, 5.0, self.bounds.h - 25.0, 3)
  110.  
  111. if (self.flapper.y < 0 or self.flapper.y + self.flapper.h > self.bounds.h + 20):
  112. self.gamestate = "dead"
  113. self.deadtime = time.time()
  114. sound.play_effect('Clank', 1.0)
  115. for i in range(219):
  116. z = Flapper(300 + self.flapper.w / 2, self.flapper.y + self.flapper.h / 2, 7.0, 7.0, 1.0, 0.0, 0.0, 0.0)
  117. xdir = random.random() - .5
  118. ydir = random.random() - .5
  119. dist = math.sqrt(xdir ** 2 + ydir ** 2)
  120. if dist == 0:
  121. xdir = 0
  122. ydir = 0
  123. else:
  124. xdir /= dist
  125. ydir /= dist
  126. z.secondaryInit(Vec2(xdir, ydir) * random.randint(0, 20))
  127. self.deathparticles.append(z)
  128. self.ticks += 1
  129. elif (self.gamestate == "dead"):
  130. #fill(self.flapper.r, self.flapper.g, self.flapper.b, self.flapper.a)
  131. #rect(300, self.flapper.y, self.flapper.w, self.flapper.h)
  132.  
  133. fill(0.0, 1.0, 0.0, 1.0)
  134. for i in self.walls:
  135. rect(i.x - self.flapper.x + 300, i.y, i.w, i.h)
  136. fill(0.8, 0.5, 0.0, 1.0)
  137. for i in self.goals:
  138. rect(i.x - self.flapper.x + 300, i.y, i.w, i.h)
  139. fill(1.0, 0.0, 0.0, 1.0)
  140. for i in self.deathparticles:
  141. ellipse(i.x, i.y, i.w, i.h)
  142. i.doGravity(Vec2(0.0, -0.63))
  143. i.doVelocity()
  144. tint(1.0, 1.0, 1.0, 1.0)
  145. text("You Died", "Arial", 32.0, self.bounds.w / 2, self.bounds.h / 2 + 32, 5)
  146. text("Points: " + str(self.points), "Arial", 32.0, self.bounds.w / 2, self.bounds.h / 2 - 32, 5)
  147. def touch_began(self, touch):
  148. if (self.gamestate == "play"):
  149. self.flapper.doTouch()
  150. sound.play_effect('Jump', 1.0)
  151. elif (self.gamestate == "dead" and (self.deadtime + 1.0) < time.time()):
  152. self.setup()
  153.  
  154. def touch_moved(self, touch):
  155. pass
  156.  
  157. def touch_ended(self, touch):
  158. pass
  159.  
  160. run(MyScene())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement