The Flag
The Flag
py
WINDOW_WIDTH = 1500
WINDOW_HEIGHT = WINDOW_WIDTH // 2
SQUARE_SIDE = WINDOW_HEIGHT // 25
FONT = "Calibri"
FONT_SIZE = 30
FLAG_CORD_X = 46 * SQUARE_SIDE
FlAG_CORD_Y = 21 * SQUARE_SIDE
AMOUNT_OF_GRASS = 20
GRASS_IMG = r"C:\Users\jbt\Downloads\grass.png"
MINE_IMG = r"C:\Users\jbt\Downloads\mine.png"
INJURY_IMG = r"C:\Users\jbt\Downloads\injury.png"
FLAG_IMG = r"C:\Users\jbt\Downloads\flag.png"
EXPLOSION_IMG = r"C:\Users\jbt\Downloads\explotion.png"
SNAKE_IMG = r"C:\Users\jbt\Downloads\snake.png"
SOLDIER_NIGHT_IMG = r"C:\Users\jbt\Downloads\soldier_nigth.png"
SOLDIER_HEIGHT = 4 * SQUARE_SIDE
SOLDIER_WIDTH = 4 * SQUARE_SIDE
SOLDIER_BODY_WIDTH = 2 * SQUARE_SIDE
SOLDIER_BODY_POS_IN_IMG = (0, 1)
FLAG_HEIGHT = 4 * SQUARE_SIDE
FLAG_WIDTH = 4 * SQUARE_SIDE
GRASS_SIDE = 80
MINE_SIZE = [SQUARE_SIDE * 3, SQUARE_SIDE]
NUM_COL = 50
NUM_ROW = 25
NUM_OF_MINES = 20
def handle_user_events(list_player_location):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and list_player_location[0] != 0:
list_player_location[0] = list_player_location[0] -1
elif event.key == pygame.K_DOWN and list_player_location[0] !=
len(mine_matrix):
list_player_location[0] = list_player_location[0] + 1
elif event.key == pygame.K_LEFT and list_player_location[1] !=
0:
list_player_location[1] = list_player_location[1] - 1
elif event.key == pygame.K_RIGHT and list_player_location[1] !=
len(mine_matrix[0]):
list_player_location[1] = list_player_location[1] + 1
if event.key == pygame.K_ENTER:
pass
return list_player_location
def soldier_touched_mine(legs_indexes):
for i in range(len(legs_indexes)):
check_touching_mine = is_location_mine(legs_indexes[i])
if check_touching_mine == True:
return True
return False
def is_location_mine(legs_indexes):
if mine_matrix[legs_indexes[0]][legs_indexes[1]] == 1:
return True
else:
return False
for p in range(len(legs_indexes)):
check_touching_flag1 = is_location_flag(legs_indexes[p])
if check_touching_flag1 == True:
return True
return False
def is_location_flag(location):
if mine_matrix[location[0]][location[1]] == 2:
return True
return False
if event.key == pygame.K_1:
pass
if event.key == pygame.K_2:
pass
if event.key == pygame.K_3:
pass
if event.key == pygame.K_4:
pass
if event.key == pygame.K_5:
pass
if event.key == pygame.K_6:
pass
if event.key == pygame.K_7:
pass
if event.key == pygame.K_8:
pass
if event.key == pygame.K_9:
pass
def convert_data_lst(data_lst):
for i in range(len(data_lst)):
while ", " in data_lst[i] or "[" in data_lst[i] or "]" in
data_lst[i]:
data_lst[i] = data_lst[i].replace(", ", "")
data_lst[i] = data_lst[i].replace("[", "")
data_lst[i] = data_lst[i].replace("]", "")
data_lst[i] = list(data_lst)
for i in range(len(data_lst)):
for j in range(len(data_lst[i])):
data_lst[i][j] = int(data_lst[i][j])
return data_lst
https://stackoverflow.com/questions/40649634/determine-length-of-keypress-in-python
https://yashardata.co.il/introduction-to-python-data-analysis-and-panda-library/
https://www.pythontutorial.net/python-basics/python-write-csv-file/
https://www.learndatasci.com/tutorials/python-pandas-tutorial-complete-introduction-for-
beginners/
https://www.geeksforgeeks.org/introduction-to-pandas-in-python/
https://www.geeksforgeeks.org/make-a-pandas-dataframe-with-two-dimensional-list-python/
https://stackoverflow.com/questions/40649634/determine-length-of-keypress-in-python
https://reshetech.co.il/machine-learning-tutorials/essentials-of-pandas
import Screen
import consts
import Soldier
import MineField
import pygame
import time
import Database
MINE_MATRIX = 0
GRASS_MATRIX = 1
SOLDIER_POSITION = 2
mine_matrix = MineField.mine_matrix
soldier_position = Soldier.start_soldier_position
grass_matrix = Screen.draw_start_green_screen(soldier_position)
def main(data_lst):
touched_mine = False
touched_flag = False
print(type(data_lst[0]), type(data_lst[1]), type(data_lst[2]))
print(data_lst)
t = 0
finish = False
while not finish:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and data_lst[SOLDIER_POSITION]
[0] != 0:
data_lst[SOLDIER_POSITION][0] =
data_lst[SOLDIER_POSITION][0] - 1
elif event.key == pygame.K_DOWN and
data_lst[SOLDIER_POSITION][0] != len(
data_lst[MINE_MATRIX]) - consts.SOLDIER_HEIGHT //
consts.SQUARE_SIDE:
data_lst[SOLDIER_POSITION][0] =
data_lst[SOLDIER_POSITION][0] + 1
elif event.key == pygame.K_LEFT and
data_lst[SOLDIER_POSITION][1] != 0:
data_lst[SOLDIER_POSITION][1] =
data_lst[SOLDIER_POSITION][1] - 1
elif event.key == pygame.K_RIGHT and
data_lst[SOLDIER_POSITION][1] != len(
data_lst[MINE_MATRIX][0]) - consts.SOLDIER_WIDTH //
consts.SQUARE_SIDE:
data_lst[SOLDIER_POSITION][1] =
data_lst[SOLDIER_POSITION][1] + 1
elif event.key == pygame.K_RETURN:
Screen.draw_night_screen(data_lst[MINE_MATRIX],
data_lst[SOLDIER_POSITION])
pygame.time.delay(1000)
elif event.key == pygame.K_1 or event.key == pygame.K_2 or
event.key == pygame.K_3 \
or event.key == pygame.K_4 or event.key ==
pygame.K_5 or event.key == pygame.K_6 \
or event.key == pygame.K_7 or event.key ==
pygame.K_8 or event.key == pygame.K_9:
t = time.time()
Screen.draw_green_screen(data_lst[SOLDIER_POSITION],
data_lst[GRASS_MATRIX])
touched_mine =
Soldier.soldier_touched_mine(Soldier.get_legs_indexes(data_lst[SOLDIER_POSI
TION]),
mine_matrix)
touched_flag =
Soldier.soldier_touched_flag(data_lst[SOLDIER_POSITION],
Soldier.get_legs_indexes(data_lst[SOLDIER_POSITION]),
mine_matrix)
if event.type == pygame.KEYUP:
if event.key == pygame.K_1 or event.key == pygame.K_2 or
event.key == pygame.K_3 \
or event.key == pygame.K_4 or event.key ==
pygame.K_5 or event.key == pygame.K_6 \
or event.key == pygame.K_7 or event.key ==
pygame.K_8 or event.key == pygame.K_9:
t = time.time() - t
t = str(t)
t = t[:3]
if float(t) >= 1:
data_lst = Database.find_game_in_csv(event.key -
pygame.K_0)
print(data_lst)
main(data_lst)
finish = True
else:
Database.add_game_to_csv(data_lst[MINE_MATRIX],
data_lst[GRASS_MATRIX],
data_lst[SOLDIER_POSITION],
event.key - pygame.K_0)
if touched_mine:
Screen.draw_losing(data_lst[SOLDIER_POSITION],
data_lst[GRASS_MATRIX])
pygame.time.delay(3000)
finish = True
if touched_flag:
Screen.draw_win_text()
pygame.time.delay(3000)
finish = True
main(data_lst)
import pandas
import os.path
is_file_exists = os.path.exists(csv_file_name)
def create_games_csv():
data_rows_lst = []
for i in range(9):
data_rows_lst.append([])
for j in range(len(columns)):
data_rows_lst[i].append([0])
if is_file_exists:
games = pandas.read_csv(csv_file_name)
else:
games = create_games_csv()
def find_game_in_csv(key):
lst = games.iloc[key - 1].tolist()
print(lst[0])
return lst
print(games)