diff --git a/README.md b/README.md index 051ef94a..3e2ccb5c 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/SNAKE_GAME/README.md b/SNAKE_GAME/README.md new file mode 100644 index 00000000..27068d35 --- /dev/null +++ b/SNAKE_GAME/README.md @@ -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: + + \ No newline at end of file diff --git a/SNAKE_GAME/snake-game.png b/SNAKE_GAME/snake-game.png new file mode 100644 index 00000000..e5a3ac0e Binary files /dev/null and b/SNAKE_GAME/snake-game.png differ diff --git a/SNAKE_GAME/snake.py b/SNAKE_GAME/snake.py new file mode 100644 index 00000000..ef0a91f3 --- /dev/null +++ b/SNAKE_GAME/snake.py @@ -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() \ No newline at end of file diff --git a/Turtle-Race-Game/README.md b/Turtle-Race-Game/README.md new file mode 100644 index 00000000..c0a9ac4b --- /dev/null +++ b/Turtle-Race-Game/README.md @@ -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. diff --git a/Turtle-Race-Game/main.py b/Turtle-Race-Game/main.py new file mode 100644 index 00000000..14720551 --- /dev/null +++ b/Turtle-Race-Game/main.py @@ -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() \ No newline at end of file diff --git a/Turtle-Race-Game/scores.txt b/Turtle-Race-Game/scores.txt new file mode 100644 index 00000000..e69de29b