Skip to content

python snake game #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Python-project-Scripts.
# Python-project-Scripts.

[![Open in Visual Studio Code](https://open.vscode.dev/badges/open-in-vscode.svg)](https://open.vscode.dev/larymak/Python-project-Scripts)
![Contributors](https://img.shields.io/github/contributors/larymak/Python-project-Scripts?style=plastic)
Expand Down
16 changes: 16 additions & 0 deletions SNAKE_GAME/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Snake Game

### This is a simple snack game in built using python. the snake start with a small length and keeps growing as it keeps feeding on the food. The game is over when it hits the end of the terminal box or eats itself.

## Requirements
- curses
- random

## How to run
- open you command line and navigate to the path of the file
- do a pip install curses && pip install random
- then type python snake.py to run

## The game in action is like this:

<img src="https://github.com/oyerohabib/Python-project-Scripts/blob/oyerohabib-2/SNAKE_GAME/snake-game.png">
Binary file added SNAKE_GAME/snake-game.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions SNAKE_GAME/snake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import random
import curses

def play_game():
s = curses.initscr()
curses.curs_set(0)
sh, sw = s.getmaxyx()
w = curses.newwin(sh, sw, 0, 0)
w.keypad(1)
w.timeout(100)

snk_x = sw/4
snk_y = sh/2
snake = [
[snk_y, snk_x],
[snk_y, snk_x-1],
[snk_y, snk_x-2]
]

food = [sh/2, sw/2]
w.addch(int(food[0]), int(food[1]), curses.ACS_PI)

key = curses.KEY_RIGHT

while True:
next_key = w.getch()
key = key if next_key == -1 else next_key

if snake[0][0] in [0, sh] or snake[0][1] in [0, sw] or snake[0] in snake[1:]:
curses.endwin()
quit()

new_head = [snake[0][0], snake[0][1]]

if key == curses.KEY_DOWN:
new_head[0] += 1
if key == curses.KEY_UP:
new_head[0] -= 1
if key == curses.KEY_LEFT:
new_head[1] -= 1
if key == curses.KEY_RIGHT:
new_head[1] += 1

snake.insert(0, new_head)

if snake[0] == food:
food = None
while food is None:
nf = [
random.randint(1, sh-1),
random.randint(1, sw-1)
]
food = nf if nf not in snake else None
w.addch(food[0], food[1], curses.ACS_PI)
else:
tail = snake.pop()
w.addch(int(tail[0]), int(tail[1]), ' ')

w.addch(int(snake[0][0]), int(snake[0][1]), curses.ACS_CKBOARD)

user_input = input("Do you want to play the snake game, type 'yes' or 'no': ").lower()

if user_input == "yes":
play_game()
else:
quit()
2 changes: 2 additions & 0 deletions Turtle-Race-Game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Turtle-Race
A randomly simulated turtle race! You can watch different color turtles race across the screen and keep track of the wins/losses of each turtle.
114 changes: 114 additions & 0 deletions Turtle-Race-Game/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import math
import random
import turtle
#import time

win_length = 500
win_height = 500

turtles = 8

turtle.screensize(win_length, win_height)


class racer(object):
def __init__(self, color, pos):
self.pos = pos
self.color = color
self.turt = turtle.Turtle()
self.turt.shape('turtle')
self.turt.color(color)
self.turt.penup()
self.turt.setpos(pos)
self.turt.setheading(90)

def move(self):
r = random.randrange(1, 20)
self.pos = (self.pos[0], self.pos[1] + r)
self.turt.pendown()
self.turt.forward(r)

def reset(self):
self.turt.penup()
self.turt.setpos(self.pos)


def setupFile(name, colors):
file = open(name, 'w')
for color in colors:
file.write(color + ' 0 \n')
file.close()


def startGame():
tList = []
turtle.clearscreen()
turtle.hideturtle()
colors = ["red", "green", "blue", 'yellow', 'pink', 'orange', 'purple', 'black', 'grey']
start = -(win_length/2) + 20
for t in range(turtles):
newPosX = start + t*(win_length)//turtles
tList.append(racer(colors[t],(newPosX, -230)))
tList[t].turt.showturtle()

run = True
while run:
for t in tList:
t.move()

maxColor = []
maxDis = 0
for t in tList:
if t.pos[1] > 230 and t.pos[1] > maxDis:
maxDis = t.pos[1]
maxColor = []
maxColor.append(t.color)
elif t.pos[1] > 230 and t.pos[1] == maxDis:
maxDis = t.pos[1]
maxColor.append(t.color)

if len(maxColor) > 0:
run = False
print('The winner is: ')
for win in maxColor:
print(win)

oldScore = []
file = open('scores.txt', 'r')
for line in file:
l = line.split()
color = l[0]
score = l[1]
oldScore.append([color, score])

file.close()

file = open('scores.txt', 'w')

for entry in oldScore:
for winner in maxColor:
if entry[0] == winner:
entry[1] = int(entry[1]) + 1

file.write(str(entry[0]) + ' ' + str(entry[1]) + '\n')


file.close()


start = input('Would you like to play, type "yes" or "no": ').lower()
if start == "yes":
print('----------GAME IN PROGRESS--------')
startGame()
else:
quit()

while True:
print('-----------------------------------')
start = input('Would you like to play again, type "yes" or "no": ').lower()
if start == "yes":
print('----------GAME IN PROGRESS--------')
startGame()
else:
print('----------THANK YOU FOR PLAYING--------')
quit()
Empty file added Turtle-Race-Game/scores.txt
Empty file.