Amazing-Python-Scripts
78 строк · 1.5 Кб
1# 2048.py
2
3# importing the logic.py file
4# where we have written all the
5# logic functions used.
6import logic
7
8# Driver code
9if __name__ == '__main__':
10
11# calling start_game function
12# to initialize the matrix
13mat = logic.start_game()
14
15while(True):
16
17# taking the user input
18# for next step
19x = input("Press the command : ")
20
21# we have to move up
22if(x == 'W' or x == 'w'):
23
24# call the move_up function
25mat, flag = logic.move_up(mat)
26
27# get the current state and print it
28status = logic.get_current_state(mat)
29print(status)
30
31# if game not over then continue
32# and add a new two
33if(status == 'GAME NOT OVER'):
34logic.add_new_2(mat)
35
36# else break the loop
37else:
38break
39
40# the above process will be followed
41# in case of each type of move
42# below
43
44# to move down
45elif(x == 'S' or x == 's'):
46mat, flag = logic.move_down(mat)
47status = logic.get_current_state(mat)
48print(status)
49if(status == 'GAME NOT OVER'):
50logic.add_new_2(mat)
51else:
52break
53
54# to move left
55elif(x == 'A' or x == 'a'):
56mat, flag = logic.move_left(mat)
57status = logic.get_current_state(mat)
58print(status)
59if(status == 'GAME NOT OVER'):
60logic.add_new_2(mat)
61else:
62break
63
64# to move right
65elif(x == 'D' or x == 'd'):
66mat, flag = logic.move_right(mat)
67status = logic.get_current_state(mat)
68print(status)
69if(status == 'GAME NOT OVER'):
70logic.add_new_2(mat)
71else:
72break
73else:
74print("Invalid Key Pressed")
75
76# print the matrix after each
77# move.
78print(mat)
79