Created
June 1, 2020 20:25
-
-
Save nandakoryaaa/78e39efe9dc53cbd4a591b4a2bc746ec to your computer and use it in GitHub Desktop.
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
from random import randint | |
import pygame | |
from pygame.locals import * | |
import time | |
W = 10 | |
PLAYER = 20 | |
ROBOT = 30 | |
DEAD_ROBOT = 35 | |
HAZARD = 40 | |
GAME_TITLE = 0 | |
GAME_PLAY = 1 | |
GAME_FINISH = 2 | |
field_title = [ | |
W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W, | |
W,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,W, | |
W,0,0,0,0,0,0,0,0,0,0,0,0,W,0,0,W,0,0,0,0,W, | |
W,0,0,0,W,W,W,W,W,W,0,0,W,W,W,W,W,W,0,0,0,W, | |
W,0,0,0,W,0,0,0,0,W,0,0,W,0,0,0,0,W,0,0,0,W, | |
W,0,0,0,W,W,W,W,W,W,0,0,W,0,0,0,0,W,0,0,0,W, | |
W,0,0,0,W,0,0,0,W,0,0,0,W,0,0,0,0,W,0,0,0,W, | |
W,0,0,0,W,0,0,0,0,W,0,0,W,W,W,W,W,W,0,0,0,W, | |
W,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,W, | |
W,0,0,0,W,W,W,W,W,0,0,0,W,W,W,W,W,W,0,0,0,W, | |
W,0,0,0,W,0,0,0,W,0,0,W,W,0,W,W,0,W,W,0,0,W, | |
W,0,0,0,W,W,W,W,W,W,0,W,W,W,W,W,W,W,W,0,0,W, | |
W,0,0,0,W,0,0,0,0,W,0,W,W,0,0,0,0,W,W,0,0,W, | |
W,0,0,0,W,W,W,W,W,W,0,0,W,W,W,W,W,W,0,0,0,W, | |
W,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,W, | |
W,0,0,0,W,W,W,W,W,W,W,0,W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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,W, | |
W,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,W, | |
W,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,W, | |
W,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,W, | |
W,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,W, | |
W,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,W, | |
W,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,W, | |
W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W,W | |
] | |
field_copy = [0] * len(field) | |
game_mode = GAME_TITLE | |
is_win = False | |
player_addr = 0 | |
def draw_rect(ctx, x, y, width, height, color): | |
rect = pygame.Rect(x, y, width, height) | |
ctx.fill(color, rect) | |
def add_objects(count, type): | |
for var in range(count): | |
x = randint(1, 20) | |
y = randint(1, 20) | |
field[y * 22 + x] = type | |
def draw_field(ctx): | |
if game_mode == GAME_TITLE: | |
my_field = field_title | |
elif game_mode == GAME_FINISH: | |
if is_win: | |
my_field = field_win | |
else: | |
my_field = field_lose | |
else: | |
my_field = field | |
draw_rect(ctx, 0, 0, 400, 400, (255,255,255)) | |
for addr in range(len(my_field)): | |
if my_field[addr] != 0: | |
rect_y = 24 + (addr // 22) * 16 | |
rect_x = 24 + (addr % 22) * 16 | |
if my_field[addr] == PLAYER: | |
color = (255,180,88) | |
elif my_field[addr] == ROBOT: | |
color = (100,118,135) | |
elif my_field[addr] == HAZARD: | |
color = (250,104,0) | |
else: | |
color = (0,0,0) | |
draw_rect(ctx, rect_x, rect_y, 15, 15, color) | |
pygame.display.update() | |
def init(): | |
global player_addr | |
global game_mode | |
global is_win | |
for addr in range(22 * 22): | |
if field[addr] != W: | |
field[addr] = 0 | |
add_objects(3, ROBOT) | |
add_objects(5, HAZARD) | |
player_addr = 22 * randint(8, 14) + randint(8, 14) | |
field[player_addr] = PLAYER | |
game_mode = GAME_PLAY | |
is_win = False | |
def get_target_addr(addr): | |
global player_addr | |
new_addr = addr | |
diff = player_addr // 22 - addr // 22 | |
if diff > 0: | |
new_addr += 22 | |
elif diff < 0: | |
new_addr -= 22 | |
diff = player_addr % 22 - addr % 22 | |
if diff < 0: | |
new_addr -= 1 | |
elif diff > 0: | |
new_addr += 1 | |
return new_addr | |
def move_robots(): | |
global game_mode | |
global is_win | |
for addr in range(len(field)): | |
field_copy[addr] = field[addr] | |
if field[addr] == ROBOT: | |
field[addr] = 0 | |
for addr in range (22, len(field) - 22): | |
if field_copy[addr] != ROBOT: | |
continue | |
new_addr = get_target_addr(addr) | |
if field[new_addr] == ROBOT or field[new_addr] == DEAD_ROBOT: | |
field[new_addr] = DEAD_ROBOT | |
else: | |
if field[new_addr] == PLAYER: | |
game_mode = GAME_FINISH | |
if field[new_addr] != HAZARD: | |
field[new_addr] = ROBOT | |
robot_count = 0 | |
for addr in range(22, len(field) - 22): | |
if field[addr] == DEAD_ROBOT: | |
field[addr] = 0 | |
elif field[addr] == ROBOT: | |
robot_count += 1 | |
if robot_count == 0: | |
is_win = True | |
game_mode = GAME_FINISH | |
def move_player(move): | |
global player_addr | |
global game_mode | |
if field[player_addr + move] != W: | |
field[player_addr] = 0 | |
player_addr += move | |
if field[player_addr] != 0: | |
game_mode = GAME_FINISH | |
else: | |
field[player_addr] = PLAYER | |
def process_move(key): | |
if key == pygame.K_KP1: | |
return 22 - 1 | |
elif key == pygame.K_KP2: | |
return 22 | |
elif key == pygame.K_KP3: | |
return 22 + 1 | |
elif key == pygame.K_KP4: | |
return -1 | |
elif key == pygame.K_KP6: | |
return 1 | |
elif key == pygame.K_KP7: | |
return -22 - 1 | |
elif key == pygame.K_KP8: | |
return -22 | |
elif key == pygame.K_KP9: | |
return -22 + 1 | |
return 0 | |
def process_game(event): | |
move = process_move(event.key) | |
if move == 0: | |
return | |
move_player(move) | |
if game_mode == GAME_FINISH: | |
return | |
move_robots() | |
def process_title(event): | |
init() | |
def process_finish(event): | |
init() | |
def process_input(event): | |
if game_mode == GAME_TITLE: | |
process_title(event) | |
elif game_mode == GAME_PLAY: | |
process_game(event) | |
elif game_mode == GAME_FINISH: | |
process_finish(event) | |
draw_field(ctx) | |
pygame.init() | |
ctx = pygame.display.set_mode((400, 400)) | |
draw_field(ctx) | |
is_playing = True | |
while is_playing: | |
event = pygame.event.wait() | |
if (event.type == pygame.QUIT): | |
break | |
if event.type == pygame.KEYDOWN: | |
process_input(event) | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment