Skip to content

Commit 68e7268

Browse files
committed
codebase
1 parent 15fd57c commit 68e7268

File tree

105 files changed

+2944
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+2944
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Rock-Paper-Scissors-in-Python (NON GUI Game)
2+
3+
In this project at OpenGenus, we explored the rock paper scissors game.
4+
5+
General rules for understanding the game:
6+
7+
1. If user1 picks Rock and user 2 picks Scissors, then user1 wins.
8+
2. If user1 picks Paper and user2 picks Rock, then user1 wins.
9+
3. If user1 picks Scissors and user2 picks Paper, then user1 wins.
10+
4. In all the other cases user2 wins.
11+
5. If both user1 and user2 pick a similar action then it will result in a draw.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from random import randint
2+
3+
print('Welcome to the ROCK, PAPER, SCISSORS game \n Choose a number from the below')
4+
5+
replayflag=True
6+
7+
while replayflag==True:
8+
numbercheckflag=False
9+
10+
while numbercheckflag==False:
11+
12+
try:
13+
user_no=int(input(" 0. rock \n 1. paper \n 2. scissors \n"))
14+
if(user_no<0 or user_no>2):
15+
print(' Pick again')
16+
else:
17+
numbercheckflag=True
18+
except:
19+
print('pick again')
20+
21+
22+
23+
options=['Rock', 'Paper', 'Scissors']
24+
25+
26+
user_choice=options[user_no]
27+
28+
29+
computer_no=randint(0,3)
30+
31+
32+
if ( user_no==computer_no):
33+
print(" IT'S A DRAW ")
34+
35+
elif ((user_no==1 and computer_no==3) or (user_no==2 and computer_no==1) or (user_no==3 and computer_no==2)):
36+
print(" USER WINS ")
37+
else:
38+
print(" COMPUTER WINS ")
39+
40+
41+
rep_choice=input(' DO you wanna play again?\n Press Y or N\n')
42+
replayflag= True if(rep_choice=='Y') else False
43+
44+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from enum import IntEnum
2+
from random import randint
3+
4+
class Action(IntEnum):
5+
Rock=1
6+
Paper=2
7+
Scissors=3
8+
9+
def get_action(number):
10+
11+
action=Action(number)
12+
return action.name
13+
14+
replayflag=True
15+
while replayflag==True:
16+
17+
user_no=int(input('Pick an action(number)\n 1.Rock \n 2.Paper \n 3.Scissors\n'))
18+
try:
19+
numcheck=False
20+
while numcheck==False:
21+
if(user_no<1 or user_no>3):
22+
print('pick again')
23+
else:
24+
numcheck=True
25+
except:
26+
print('pick again')
27+
28+
29+
computer_no=randint(1,3)
30+
31+
print(computer_no)
32+
33+
user_pick=get_action(user_no)
34+
computer_pick=get_action(computer_no)
35+
36+
37+
print(f"The user picked {user_pick} and the computer picked {computer_pick}")
38+
39+
if ( user_pick==computer_pick):
40+
print(" IT'S A DRAW ")
41+
42+
elif ((user_pick==1 and computer_pick==3) or (user_pick==2 and computer_pick==1) or (user_pick==3 and computer_pick==2)):
43+
print(" USER WINS ")
44+
else:
45+
print(" COMPUTER WINS ")
46+
47+
rep=input('Want to play again? Y/N')
48+
replayflag= True if rep=='Y' else False
49+
50+

codebase/P-10 2048 Game/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 2048-python
2+
3+
### Introduction
4+
This is 2048 game code in Python. Tkinter library is used for this code so that we can use the grid method for the display. This way, the game can be played similar to its original form. This game takes the input from the arrow keys of the keyboard and takes actions accordingly. Separate functions are created for these operations.
5+
6+
### Technologies
7+
Project created with:
8+
* PyCharm Community Edition 2022.3
9+
* Python 3.10.9
10+
* Tkinter Library(requires Python 3.7 or up)
11+
12+
### SetUp
13+
* Intall Python 3.7 or up
14+
* Check if Python and pip installed
15+
* Run to check: python --veresion & pip -V
16+
* Install Tkinter using pip
17+
* Run command: pip install tk
18+
19+
### Run Project
20+
Use the arrow keys to move Up, Down, Left, and Right. The game will go on until reached 2048 or all the tiles are filled up and no move is left.
21+
22+
### Output
23+
The output of the Gameboard, Start of the game, Winning Board, and Game Over Board.

codebase/P-10 2048 Game/main.py

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
from tkinter import *
2+
import random
3+
4+
class Game(Frame):
5+
def __init__(self):
6+
Frame.__init__(self)
7+
self.grid()
8+
self.master.title("2048")
9+
10+
self.main_grid = Frame(self, bg='lightgrey', bd=3, width=400,height=400)
11+
self.main_grid.grid()
12+
self.gameboard()
13+
self.start_game()
14+
15+
#binding moves
16+
self.master.bind("<Left>", self.left)
17+
self.master.bind("<Right>", self.right)
18+
self.master.bind("<Up>", self.up)
19+
self.master.bind("<Down>", self.down)
20+
21+
self.mainloop()
22+
23+
def gameboard(self):
24+
#making grid
25+
self.tiles = []
26+
for i in range(4):
27+
row = []
28+
for j in range(4):
29+
tile_frame = Frame(
30+
self.main_grid,
31+
bg='white',
32+
width='50',
33+
height='50'
34+
)
35+
tile_frame.grid(row=i, column=j, padx=3, pady=3)
36+
tile_number = Label(self.main_grid, bg='white')
37+
tile_number.grid(row=i, column=j)
38+
tile_data = {"frame": tile_frame, "number": tile_number}
39+
row.append(tile_data)
40+
self.tiles.append(row)
41+
42+
def start_game(self):
43+
self.board = [[0 for col in range(4)] for row in range(4)]
44+
45+
numNeeded = 2
46+
while numNeeded > 0:
47+
row = random.randint(0, 4 - 1)
48+
col = random.randint(0, 4 - 1)
49+
50+
if self.board[row][col] == 0:
51+
self.board[row][col] = 2
52+
numNeeded -= 1
53+
self.tiles[row][col]["frame"].configure(bg="orange")
54+
self.tiles[row][col]["number"].configure(
55+
bg="orange",
56+
fg="white",
57+
font=20,
58+
text="2"
59+
)
60+
61+
def moveToLeft(self):
62+
new_board = [[0 for col in range(4)] for row in range(4)]
63+
for i in range(4):
64+
fill_position = 0
65+
for j in range(4):
66+
if self.board[i][j] != 0:
67+
new_board[i][fill_position] = self.board[i][j]
68+
fill_position += 1
69+
self.board = new_board
70+
71+
72+
def merge(self):
73+
for i in range(4):
74+
for j in range(3):
75+
if self.board[i][j] != 0 and self.board[i][j] == self.board[i][j+1]:
76+
self.board[i][j] *= 2
77+
self.board[i][j+1] = 0
78+
# self.score += self.board[i][j]
79+
80+
def reverse(self):
81+
new_board = []
82+
for i in range(4):
83+
new_board.append([])
84+
for j in range(4):
85+
new_board[i].append(self.board[i][3-j])
86+
self.board = new_board
87+
88+
def transpose(self):
89+
new_board = [[0 for col in range(4)] for row in range(4)]
90+
for i in range(4):
91+
for j in range(4):
92+
new_board[i][j] = self.board[j][i]
93+
self.board = new_board
94+
95+
# add a 2 or 4 in any cell randomly
96+
def pickNewValue(self):
97+
row = col = 0
98+
while self.board[row][col] != 0:
99+
row = random.randint(0, 3)
100+
col = random.randint(0, 3)
101+
102+
if random.randint(1, 5) == 1:
103+
self.board[row][col] = 4
104+
else:
105+
self.board[row][col] = 2
106+
107+
#update board
108+
def updateGame(self):
109+
for i in range(4):
110+
for j in range(4):
111+
cell_value = self.board[i][j]
112+
if cell_value == 0:
113+
self.tiles[i][j]["frame"].configure(bg="white")
114+
self.tiles[i][j]["number"].configure(bg="white", text="")
115+
else:
116+
self.tiles[i][j]["frame"].configure(bg="orange")
117+
self.tiles[i][j]["number"].configure(
118+
bg="orange",
119+
fg="white",
120+
font="20",
121+
text=str(cell_value)
122+
)
123+
self.update_idletasks()
124+
125+
#left right up down operations
126+
def left(self, event):
127+
self.moveToLeft()
128+
self.merge()
129+
self.moveToLeft()
130+
self.pickNewValue()
131+
self.updateGame()
132+
self.final_result()
133+
134+
def right(self, event):
135+
self.reverse()
136+
self.moveToLeft()
137+
self.merge()
138+
self.moveToLeft()
139+
self.reverse()
140+
self.pickNewValue()
141+
self.updateGame()
142+
self.final_result()
143+
144+
def up(self, event):
145+
self.transpose()
146+
self.moveToLeft()
147+
self.merge()
148+
self.moveToLeft()
149+
self.transpose()
150+
self.pickNewValue()
151+
self.updateGame()
152+
self.final_result()
153+
154+
155+
def down(self, event):
156+
self.transpose()
157+
self.reverse()
158+
self.moveToLeft()
159+
self.merge()
160+
self.moveToLeft()
161+
self.reverse()
162+
self.transpose()
163+
self.pickNewValue()
164+
self.updateGame()
165+
self.final_result()
166+
167+
# #check for available moves
168+
def horizontal_move_exists(self):
169+
for i in range(4):
170+
for j in range(3):
171+
if self.board[i][j] == self.board[i][j+1]:
172+
return True
173+
return False
174+
175+
def vertical_move_exists(self):
176+
for i in range(3):
177+
for j in range(4):
178+
if self.board[i][j] == self.board[i+1][j]:
179+
return True
180+
return False
181+
182+
#check game over (win/lose)
183+
def final_result(self):
184+
if any(2048 in row for row in self.board):
185+
game_over_frame = Frame(self.main_grid, borderwidth=2)
186+
game_over_frame.place(relx=0.5, rely=0.5, anchor='center')
187+
Label(
188+
game_over_frame,
189+
text="You Win",
190+
bg="green",
191+
fg="white",
192+
font="20"
193+
).pack()
194+
195+
elif not any(0 in row for row in self.board) and not self.horizontal_move_exists() and not self.vertical_move_exists():
196+
game_over_frame = Frame(self.main_grid, borderwidth=2)
197+
game_over_frame.place(relx=0.5, rely=0.5, anchor='center')
198+
Label(
199+
game_over_frame,
200+
text="Game Over",
201+
bg="Red",
202+
fg="white",
203+
font="20"
204+
).pack()
205+
206+
def main():
207+
Game()
208+
209+
if __name__ == "__main__":
210+
main()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# flappy-bird-python
2+
3+
### Introduction
4+
This is the addictive Flappy Bird game code iThis game is developed using Pygame library. The images added to this game is easily available on the internet. The images include a bird, a background, a ground, a gameover, pipes and numbers for score.
5+
6+
### Technologies
7+
Used for this project:
8+
* PyCharm Community Edition 2022.3
9+
* Python 3.10.9
10+
* Pygame Library
11+
12+
### SetUp
13+
* Install Python
14+
* Install Pygame
15+
* Download images for the game
16+
17+
### Play
18+
This game is played using the UP or SPACE key of the keyoard. The key needs to be pressed continuously to keep the bird flapping and avoid the pipes. The scores will be added for avoiding collisions with the pipes.

0 commit comments

Comments
 (0)