Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pygame import *
- from pygame.sprite import *
- import random
- import copy
- import pyganim
- windowSize = (800, 480)
- gravity = 0.1
- newlist = []
- pipedistance = 80
- margin = 71
- pipescale = 0.5
- def rot_center(image, angle, scale):
- """rotate an image while keeping its center and size"""
- orig_rect = image.get_rect()
- rot_image = pygame.transform.rotozoom(image, angle, scale)
- rot_rect = orig_rect.copy()
- rot_rect.center = rot_image.get_rect().center
- rot_image = rot_image.subsurface(rot_rect).copy()
- return rot_image
- class Pipe(Sprite):
- def __init__(self):
- Sprite.__init__(self)
- self.image = image.load("pipe.png").convert_alpha()
- self.rect = self.image.get_rect()
- self.resize()
- def resize(self):
- y = random.randint(0, windowSize[1]-pipedistance)
- print(y)
- self.image = pygame.transform.rotozoom(self.image, 0, pipescale)
- self.rect = self.image.get_rect().move(windowSize[0]-margin, windowSize[1]-y)
- self.rect.size = (self.rect.size[0]-10, self.rect.size[1]-y)
- print("pipe1 rect: " + str(self.rect))
- # def update(self):
- # self.rect.move(-1, 0)
- class Bird(Sprite):
- def __init__(self):
- Sprite.__init__(self)
- self.image = pygame.transform.scale(image.load("bird.png").convert_alpha(), (50, 50))
- self.copy = pygame.transform.scale(image.load("bird.png").convert_alpha(), (50, 50))
- self.startposition = (windowSize[0] / 2 - (windowSize[0] / 4), windowSize[1] / 2)
- self.rect = self.image.get_rect().move(self.startposition)
- self.velocity = 0
- self.up = []
- self.down = []
- # Set up scaling animation
- def generateframes(self, nframes, direction, step):
- for i in range(0, nframes, step):
- if direction == 'up':
- self.up.append(rot_center(self.image, i, 1.0))
- elif direction == 'down':
- self.down.append(rot_center(self.image, - i, 1.0))
- def update(self):
- self.velocity += gravity
- self.rect = self.image.get_rect().move(self.rect.x, self.rect.y + self.velocity)
- # def createanimationlist(imglist, seconds):
- # for i in range(0, len(imglist)):
- # newlist.append((imglist[i], seconds))
- # return newlist
- def main():
- pygame.init()
- fps = 60 # frames per second setting
- skip = 3
- fpsclock = pygame.time.Clock()
- screen = display.set_mode(windowSize)
- background = pygame.image.load("background.png").convert_alpha()
- x = windowSize[0]
- x1 = 0
- clicked = 0
- display.set_caption("Flappy Bird")
- bird1 = Bird()
- birds = Group(bird1)
- bird1.generateframes(70, 'up', 1)
- pipe1 = Pipe()
- pipe2 = copy.deepcopy(pipe1)
- print(pipe2.rect.size)
- pipe2.image = pygame.transform.flip(pipe2.image, False, True)
- print("rect1size = " + str(pipe1.rect.size))
- print("rect2size = " + str(pipe2.rect.size))
- pipe2.rect.y = 0-pipe2.rect.size[1]
- print("pipe1 rect: " + str(pipe1.rect))
- print("pipe2 rect: " + str(pipe2.rect))
- pipes = Group(pipe1, pipe2)
- # animatelist = createanimationlist(bird1.up, 0.001)
- # animObj = pyganim.PygAnimation(animatelist, loop=False)
- while True:
- ev = event.poll()
- if ev.type == pygame.QUIT:
- break
- x -= skip
- x1 -= skip
- screen.blit(background, (x, 0))
- screen.blit(background, (x1, 0))
- if x < -windowSize[0]:
- x = windowSize[0]
- if x1 < -windowSize[0]:
- x1 = windowSize[0]
- birds.draw(screen)
- pipes.draw(screen)
- if ev.type == MOUSEBUTTONDOWN:
- bird1.image = bird1.copy
- # for elem in bird1.up:
- # bird1.image = elem
- bird1.velocity = - 80
- clicked = 1
- else:
- if clicked == 1:
- bird1.velocity = 0
- bird1.image = bird1.copy
- clicked = 0
- birds.update()
- pipes.update()
- dt = fpsclock.tick(fps)
- speed = 1 / float(dt)
- display.flip()
- pygame.quit()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment