Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, random
- WIDTH = 600
- HEIGHT = 800
- GRAVITY = 1
- JUMP_POWER = 20
- GAME_SPEED = 5
- class Pipe(pygame.sprite.Sprite):
- def __init__(self, category):
- super().__init__()
- self.height = random.randrange(HEIGHT/4, HEIGHT/2-50)
- self.image = pygame.Surface((90, self.height))
- self.image.fill((255,0,255))
- self.rect = self.image.get_rect()
- self.rect.left = WIDTH
- if category == 0:
- self.rect.top = 0
- else:
- self.rect.bottom = HEIGHT
- def update(self):
- self.rect.centerx -= GAME_SPEED
- class Bird(pygame.sprite.Sprite): # inheritance
- def __init__(self):
- super().__init__()
- self.image = pygame.Surface((35,35))
- self.image.fill((209, 0, 0))
- self.rect = self.image.get_rect()
- self.rect.center = (100, 50)
- self.yspeed = 0 # the speed with which the bird is going down
- def update(self):
- self.yspeed += GRAVITY
- self.rect.move_ip(0, self.yspeed)
- def jump(self):
- self.yspeed = -JUMP_POWER
- pygame.init()
- window = pygame.display.set_mode((WIDTH, HEIGHT))
- run = True
- elements = pygame.sprite.Group()
- pipes = pygame.sprite.Group()
- clock = pygame.time.Clock() # helps us control the frame rate
- event_make_pipe = pygame.USEREVENT + 1
- pygame.time.set_timer(event_make_pipe, 1500)
- player = Bird()
- elements.add(player)
- while run:
- clock.tick(60) # frame rate
- window.fill((0,0,0))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- run = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_SPACE:
- player.jump()
- if event.type == pygame.MOUSEBUTTONDOWN:
- player.jump()
- if event.type == event_make_pipe:
- p = Pipe(0)
- elements.add(p)
- pipes.add(p)
- p = Pipe(1)
- elements.add(p)
- pipes.add(p)
- if pygame.sprite.spritecollideany(player, pipes) or player.rect.bottom > HEIGHT or player.rect.top < 0:
- run = False
- elements.update()
- elements.draw(window)
- pygame.display.update()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement