Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import os
- import time
- import random
- import math
- from player import Player
- import re
- pygame.font.init()
- pygame.mixer.init()
- fps = 60
- background_rgb = (255, 255, 255)
- width, height = 1080, 720
- win = pygame.display.set_mode((width, height))
- clock = pygame.time.Clock()
- space_debounce = False
- gravity = 0.03
- flap_power = -2.5
- player = Player(width / 2 - 32, height / 2 - 64, 0, 0)
- starting_height = player.y_pos
- score = round(-(player.y_pos - starting_height) / win.get_height())
- colors = [
- name for name in pygame.color.THECOLORS
- # avoid hard to see colors
- if re.search('(red|green|blue)', name)
- ]
- def random_sprite(inside, min_size=(100, 100), max_size=(200, 200)):
- min_width, min_height = min_size
- max_width, max_height = max_size
- sprite = pygame.sprite.Sprite()
- size = (
- random.randint(min_width, max_width),
- random.randint(min_height, max_height)
- )
- position = (
- random.randint(0, inside.width - size[0]),
- random.randint(0, inside.height - size[1])
- )
- sprite.rect = pygame.Rect(position, size)
- color = random.choice(colors)
- sprite.image = pygame.Surface(sprite.rect.size)
- sprite.image.fill(color)
- return sprite
- frame = win.get_rect()
- camera = win.get_rect()
- space = frame.inflate(frame.width / 2, frame.height / 2)
- camera.center = 100, player.y_pos
- beans = [random_sprite(space) for _ in range(1)]
- def check_collisions(a_x, a_y, a_width, a_height, b_x, b_y, b_width, b_height):
- return (a_x + a_width > b_x) and (a_x < b_x + b_width) and (a_y + a_height > b_y) and (a_y < b_y + b_height)
- def draw():
- camera.center = player.x_pos, player.y_pos
- win.fill(background_rgb)
- for bean in beans:
- if check_collisions(player.x_pos - camera.x, player.y_pos - camera.y, player.current_img.get_width(), player.current_img.get_height(), bean.rect.x - camera.x, bean.rect.y - camera.y, bean.image.get_width(), bean.image.get_height()):
- print(player.x_pos, player.y_pos, bean.rect.x, bean.rect.y,)
- win.blit(bean.image, (bean.rect.x, bean.rect.y - camera.y))
- win.blit(player.current_img, (player.x_pos, player.y_pos))
- camera.center = player.x_pos, player.y_pos
- def update_positions():
- camera.center = player.x_pos, player.y_pos
- if player.x_pos >= width - 64:
- player.current_img = player.right_img
- player.x_vel = -player.x_vel
- elif player.x_pos <= 0:
- player.current_img = player.left_img
- player.x_vel = -player.x_vel
- player.y_vel += gravity
- player.x_pos += player.x_vel
- # player.y_pos += player.y_vel
- camera.center = player.x_pos, player.y_pos
- def check_keys():
- global space_debounce
- keys = pygame.key.get_pressed()
- if keys[pygame.K_UP]:
- player.y_pos += flap_power
- if keys[pygame.K_DOWN]:
- player.y_pos -= flap_power
- if keys[pygame.K_RIGHT]:
- player.x_pos -= flap_power
- if keys[pygame.K_LEFT]:
- player.x_pos += flap_power
- # if keys[pygame.K_SPACE] and not space_debounce:
- # player.y_vel = flap_power
- # space_debounce = True
- # if not keys[pygame.K_SPACE] and space_debounce:
- # space_debounce = False
- while True:
- clock.tick(fps)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- draw()
- update_positions()
- check_keys()
- pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment