Created
June 12, 2021 09:52
-
-
Save rupython/f374b3ee3dacb480f0f4be8a384be29c 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 pgex.parameters import colors | |
SCREEN_WIDTH = 800 | |
SCREEN_HEIGHT = 600 | |
FPS = 60 | |
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") | |
knight_image = pg.image.load("knight.png").convert() | |
game_over = False | |
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 | |
hero_speed = 5 | |
def draw_knight(): | |
global screen | |
global knight | |
screen.blit(knight["surf"], knight["rect"]) | |
keys = pg.key.get_pressed() | |
if keys[pg.K_LEFT]: | |
knight["rect"].x -= hero_speed | |
knight["rect"].blit(knight_image, (0,0)) | |
elif keys[pg.K_RIGHT]: | |
knight["rect"].x += hero_speed | |
knight["rect"].blit(knight_image, (0,0)) | |
elif keys[pg.K_DOWN]: | |
knight["rect"].y += hero_speed | |
knight["rect"].blit(knight_image, (0,0)) | |
elif keys[pg.K_UP]: | |
knight["rect"].y -= hero_speed | |
knight["rect"].blit(knight_image, (0,0)) | |
def game_cycle(): | |
global game_over, screen | |
global background | |
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() | |
pg.display.update() | |
clock.tick(FPS) | |
def start_game(): | |
game_cycle() | |
if keys[pg.K_ESCAPE]: | |
pg.quit() | |
return | |
pg.quit() | |
start_game() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment