Created
June 14, 2021 09:50
-
-
Save rupython/2b4fe6976019177d0f16afc0abdaeb55 to your computer and use it in GitHub Desktop.
From: Юродь
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 as pg | |
from random import randint, choice | |
from pgex.widgets import Text | |
from pgex.parameters import colors | |
SCREEN_WIDTH = 800 | |
SCREEN_HEIGHT = 600 | |
FPS = 60 | |
GHOSTS_AMOUNT = 3 | |
ghost_width = 333 | |
DIRECTIONS = ("left", "right") | |
pg.init() | |
screen = pg.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) | |
pg.display.set_caption("Ghosts Game") | |
pg.display.set_icon(pg.image.load("ghost_icon.png")) | |
background = pg.image.load("dark_forest.jpg") | |
game_over = False | |
game_scores = 0 | |
acceleration = 1 | |
acceleration_timer = 10 * FPS | |
knight = {} | |
knight["surf"] = pg.Surface((43, 64)) | |
knight["surf"].set_colorkey(colors["white"]) | |
knight["surf"].blit(pg.image.load("knight.png").convert(), (0, 0)) | |
knight["rect"] = knight["surf"].get_rect() | |
knight["rect"].x = 400 | |
knight["rect"].y = 300 | |
ghosts_images = [ | |
pg.image.load(r"ghosts/ghost_0.png").convert(), | |
pg.image.load(r"ghosts/ghost_1.png").convert(), | |
pg.image.load(r"ghosts/ghost_2.png").convert(), | |
] | |
ghosts = [] | |
new_ghost = {} | |
def draw_ghosts(): | |
global ghost, knight | |
global game_over | |
for ghost in ghosts: | |
draw_ghost(ghost) | |
if ghost["rect"].colliderect(knight["rect"]): | |
game_over = True | |
return | |
def draw_ghost(ghost): | |
global screen | |
if ghost["direction"] == "left": | |
ghost["rect"].x -= ghost["speed_x"] | |
ghost["rect"].y += ghost["speed_y"] | |
if ghost["rect"].right < 0: | |
update_ghost(ghost) | |
elif ghost["direction"] == "right": | |
ghost["rect"].x += ghost["speed_x"] | |
ghost["rect"].y += ghost["speed_y"] | |
if ghost["rect"].x > SCREEN_WIDTH: | |
update_ghost(ghost) | |
screen.blit(ghost["surf"], ghost["rect"]) | |
def update_ghost(ghost): | |
global ghosts_images | |
global acceleration | |
ghost["surf"].blit(choice(ghosts_images), (0, 0)) | |
ghost["speed_x"] = randint(3, 6) * acceleration | |
ghost["rect"].y = randint(0, SCREEN_HEIGHT - 128) | |
if ghost["rect"].y < SCREEN_HEIGHT / 2: | |
ghost["speed_y"] = randint(1, 3) * acceleration | |
else: | |
ghost["speed_y"] = -randint(1, 3) * acceleration | |
ghost["direction"] = choice(DIRECTIONS) | |
if ghost["direction"] == "left": | |
ghost["rect"].x = SCREEN_WIDTH | |
elif ghost["direction"] == "right": | |
ghost["rect"].right = 0 | |
def create_ghosts(): | |
global ghosts | |
for i in range(GHOSTS_AMOUNT): | |
new_ghost = create_ghost() | |
ghosts.append(new_ghost) | |
def create_ghost(): | |
global ghost_images | |
new_ghost["surf"] = pg.Surface((128, 128)) | |
new_ghost["surf"].set_colorkey(colors["black"]) | |
new_ghost["surf"].blit(choice(ghosts_images), (0,0)) | |
new_ghost["rect"] = new_ghost["surf"].get_rect() | |
new_ghost["rect"].y = randint(0, SCREEN_HEIGHT - 128) | |
new_ghost["direction"] = choice(DIRECTIONS) | |
new_ghost["speed_x"] = randint(3, 6) | |
if new_ghost["direction"] == "left": | |
new_ghost["rect"].x = SCREEN_WIDTH | |
elif new_ghost["direction"] == "right": | |
new_ghost["rect"].right = 0 | |
if new_ghost["rect"].y < SCREEN_HEIGHT / 2: | |
new_ghost["speed_y"] = randint(1, 3) | |
else: | |
new_ghost["speed_y"] = -randint(1, 3) | |
return new_ghost | |
def draw_knight(): | |
global screen | |
global knight | |
knight["rect"].center = pg.mouse.get_pos() | |
screen.blit(knight["surf"], knight["rect"]) | |
def game_cycle(): | |
global game_over, screen, game_scores | |
global background | |
global acceleration_timer, acceleration | |
scores_text = Text("Scores: ", str(game_scores), "font.ttf", 70, font_color=colors["orange"]) | |
acceleration_text = Text("x", str(acceleration), "font.ttf", 70, font_color=colors["orange"]) | |
clock = pg.time.Clock() | |
while not game_over: | |
for event in pg.event.get(eventtype=pg.QUIT): | |
game_over = True | |
return | |
screen.blit(background, (0, 0)) | |
draw_knight() | |
draw_ghosts() | |
game_scores += 1 | |
scores_text.text = "Scores: " + str(game_scores) | |
scores_text.draw(screen, (0, 530)) | |
acceleration_text.text = "x" + str(acceleration) | |
acceleration_text.draw(screen, (0, 0)) | |
acceleration_timer -= 1 | |
if acceleration_timer == 0: | |
acceleration_timer = 10 * FPS | |
acceleration += 0.5 | |
pg.display.update() | |
clock.tick(FPS) | |
def start_game(): | |
create_ghosts() | |
pg.mouse.set_visible(False) | |
game_cycle() | |
pg.quit() | |
start_game() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment