2n2u

Untitled

Nov 22nd, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. from pygame import *
  2. from pygame.sprite import *
  3. import random
  4. import copy
  5. import pyganim
  6.  
  7. windowSize = (800, 480)
  8. gravity = 0.1
  9. newlist = []
  10. pipedistance = 80
  11. margin = 71
  12. pipescale = 0.5
  13.  
  14.  
  15. def rot_center(image, angle, scale):
  16. """rotate an image while keeping its center and size"""
  17. orig_rect = image.get_rect()
  18. rot_image = pygame.transform.rotozoom(image, angle, scale)
  19. rot_rect = orig_rect.copy()
  20. rot_rect.center = rot_image.get_rect().center
  21. rot_image = rot_image.subsurface(rot_rect).copy()
  22. return rot_image
  23.  
  24.  
  25. class Pipe(Sprite):
  26. def __init__(self):
  27. Sprite.__init__(self)
  28. self.image = image.load("pipe.png").convert_alpha()
  29. self.rect = self.image.get_rect()
  30. self.resize()
  31.  
  32. def resize(self):
  33. y = random.randint(0, windowSize[1]-pipedistance)
  34. print(y)
  35. self.image = pygame.transform.rotozoom(self.image, 0, pipescale)
  36. self.rect = self.image.get_rect().move(windowSize[0]-margin, windowSize[1]-y)
  37. self.rect.size = (self.rect.size[0]-10, self.rect.size[1]-y)
  38. print("pipe1 rect: " + str(self.rect))
  39. # def update(self):
  40. # self.rect.move(-1, 0)
  41.  
  42. class Bird(Sprite):
  43. def __init__(self):
  44. Sprite.__init__(self)
  45. self.image = pygame.transform.scale(image.load("bird.png").convert_alpha(), (50, 50))
  46. self.copy = pygame.transform.scale(image.load("bird.png").convert_alpha(), (50, 50))
  47. self.startposition = (windowSize[0] / 2 - (windowSize[0] / 4), windowSize[1] / 2)
  48. self.rect = self.image.get_rect().move(self.startposition)
  49. self.velocity = 0
  50. self.up = []
  51. self.down = []
  52. # Set up scaling animation
  53.  
  54. def generateframes(self, nframes, direction, step):
  55. for i in range(0, nframes, step):
  56. if direction == 'up':
  57. self.up.append(rot_center(self.image, i, 1.0))
  58. elif direction == 'down':
  59. self.down.append(rot_center(self.image, - i, 1.0))
  60.  
  61. def update(self):
  62. self.velocity += gravity
  63. self.rect = self.image.get_rect().move(self.rect.x, self.rect.y + self.velocity)
  64.  
  65.  
  66. # def createanimationlist(imglist, seconds):
  67. # for i in range(0, len(imglist)):
  68. # newlist.append((imglist[i], seconds))
  69. # return newlist
  70.  
  71.  
  72. def main():
  73. pygame.init()
  74. fps = 60 # frames per second setting
  75. skip = 3
  76. fpsclock = pygame.time.Clock()
  77. screen = display.set_mode(windowSize)
  78. background = pygame.image.load("background.png").convert_alpha()
  79. x = windowSize[0]
  80. x1 = 0
  81. clicked = 0
  82. display.set_caption("Flappy Bird")
  83. bird1 = Bird()
  84. birds = Group(bird1)
  85. bird1.generateframes(70, 'up', 1)
  86. pipe1 = Pipe()
  87. pipe2 = copy.deepcopy(pipe1)
  88. print(pipe2.rect.size)
  89. pipe2.image = pygame.transform.flip(pipe2.image, False, True)
  90. print("rect1size = " + str(pipe1.rect.size))
  91. print("rect2size = " + str(pipe2.rect.size))
  92. pipe2.rect.y = 0-pipe2.rect.size[1]
  93. print("pipe1 rect: " + str(pipe1.rect))
  94. print("pipe2 rect: " + str(pipe2.rect))
  95. pipes = Group(pipe1, pipe2)
  96.  
  97. # animatelist = createanimationlist(bird1.up, 0.001)
  98. # animObj = pyganim.PygAnimation(animatelist, loop=False)
  99.  
  100. while True:
  101. ev = event.poll()
  102. if ev.type == pygame.QUIT:
  103. break
  104. x -= skip
  105. x1 -= skip
  106.  
  107. screen.blit(background, (x, 0))
  108. screen.blit(background, (x1, 0))
  109. if x < -windowSize[0]:
  110. x = windowSize[0]
  111. if x1 < -windowSize[0]:
  112. x1 = windowSize[0]
  113. birds.draw(screen)
  114. pipes.draw(screen)
  115. if ev.type == MOUSEBUTTONDOWN:
  116. bird1.image = bird1.copy
  117. # for elem in bird1.up:
  118. # bird1.image = elem
  119. bird1.velocity = - 80
  120. clicked = 1
  121. else:
  122. if clicked == 1:
  123. bird1.velocity = 0
  124. bird1.image = bird1.copy
  125. clicked = 0
  126.  
  127. birds.update()
  128. pipes.update()
  129. dt = fpsclock.tick(fps)
  130. speed = 1 / float(dt)
  131. display.flip()
  132.  
  133. pygame.quit()
  134.  
  135.  
  136. if __name__ == "__main__":
  137. main()
Advertisement
Add Comment
Please, Sign In to add comment