Created
September 8, 2021 14:47
-
-
Save ShambhaviPataskar/6b7c2e876fac76df32f250ecde720429 to your computer and use it in GitHub Desktop.
I have created a block game!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import random | |
import sys | |
pygame.init() | |
text = "Game Over" | |
HEIGHT = 600 | |
WIDTH = 800 | |
P_C = (255,255,0) | |
E_C = (0,0,238) | |
BACKGROUND_COLOR = (0,0,0) | |
player_size = 50 | |
player_pos = [WIDTH/2, HEIGHT-2*player_size] | |
enemy_size = 50 | |
enemy_pos = [random.randint(0,WIDTH-enemy_size), 0] | |
enemy_list = [enemy_pos] | |
SPEED = 10 | |
screen = pygame.display.set_mode((WIDTH, HEIGHT)) | |
game_over = False | |
score = 0 | |
clock = pygame.time.Clock() | |
# Game Title | |
pygame.display.set_caption("Block Dodge") | |
pygame.display.update() | |
clock = pygame.time.Clock() | |
font = pygame.font.SysFont (None, 55) | |
def drop_enemies(enemy_list): | |
delay = random.random() | |
if len(enemy_list) < 10 and delay < 0.1: | |
x_pos = random.randint(0, WIDTH-enemy_size) | |
y_pos = 0 | |
enemy_list.append([x_pos, y_pos]) | |
def draw_enemies(enemy_list): | |
for enemy_pos in enemy_list: | |
pygame.draw.rect(screen, E_C, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size)) | |
def update_enemy_positions(enemy_list, score): | |
for idx, enemy_pos in enumerate(enemy_list): | |
if enemy_pos[1] >= 0 and enemy_pos[1] < HEIGHT: | |
enemy_pos[1] += SPEED | |
else: | |
enemy_list.pop(idx) | |
score += 1 | |
def collision_check(enemy_list, player_pos): | |
for enemy_pos in enemy_list: | |
if detect_collision(enemy_pos, player_pos): | |
return True | |
return False | |
def detect_collision(player_pos, enemy_pos): | |
p_x = player_pos[0] | |
p_y = player_pos[1] | |
e_x = enemy_pos[0] | |
e_y = enemy_pos[1] | |
if (e_x >= p_x and e_x < (p_x + player_size)) or (p_x >= e_x and p_x < (e_x+enemy_size)): | |
if (e_y >= p_y and e_y < (p_y + player_size)) or (p_y >= e_y and p_y < (e_y + enemy_size)): | |
return True | |
return False | |
while not game_over: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
sys.exit() | |
if event.type == pygame.KEYDOWN: | |
x = player_pos[0] | |
y = player_pos[1] | |
if event.key == pygame.K_LEFT: | |
x -= player_size | |
elif event.key == pygame.K_RIGHT: | |
x += player_size | |
player_pos = [x,y] | |
screen.fill(BACKGROUND_COLOR) | |
if detect_collision(player_pos, enemy_pos): | |
game_over = True | |
drop_enemies(enemy_list) | |
update_enemy_positions(enemy_list, score) | |
if collision_check(enemy_list, player_pos): | |
game_over = True | |
#print("Game Over") | |
print(text) | |
draw_enemies(enemy_list) | |
pygame.draw.rect(screen, P_C, (player_pos[0], player_pos[1], player_size, player_size)) | |
clock.tick(30) | |
pygame.display.update() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment