Skip to content
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

Small code for Rock, Paper, Scissors #138

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 3 additions & 9 deletions c/Palindrome.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
A palindrome number is a number that is same after reverse.
For example 121, 34543, 343, 131, 48984 are the palindrome numbers.
*/
#include <stdio.h>
#include<stdio.h>
int main()
{
int n, m, rem, rev=0;
Expand All @@ -16,13 +16,7 @@ int main()
rev = rev * 10 +rem;
m = m / 10;
}
if(n == rev)
{
printf("%d is a Palindrome",n);
}
else
{
printf("%d is not a Palindrome",n);
}
n == rev ? printf("%d is a Palindrome",n) : printf("%d is not a Palindrome",n);

return 0;
}
17 changes: 11 additions & 6 deletions python/Celsius To Fahrenheit.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Python Program to convert temperature in celsius to fahrenheit
# Python Program to convert temperature in celsius(take input from user) to fahrenheit

# change this value for a different result
celsius = 37.5
# Input: temperature in Celsius
celsius = float(input("Enter temperature in Celsius: "))

# calculate fahrenheit
fahrenheit = (celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit))
# Function to convert Celsius to Fahrenheit
def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius * 9/5) + 32
return fahrenheit

# Convert and display the result
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius}°C is equal to {fahrenheit}°F")
195 changes: 18 additions & 177 deletions python/snake.py
Original file line number Diff line number Diff line change
@@ -1,183 +1,24 @@
# Simple Snake Game in Python 3 for Beginners
import turtle
import time
import random

delay = 0.1
def play():
options = ["rock", "paper", "scissors"]
computer_choice = random.choice(options)
user_choice = input("Enter rock, paper, or scissors: ").lower()

# Score
score = 0
high_score = 0
if user_choice not in options:
print("Invalid choice! Please choose rock, paper, or scissors.")
return play() # Restart if invalid input

# Set up the screen
wn = turtle.Screen()
wn.title("Snake Game by @TokyoEdTech")
wn.bgcolor("green")
wn.setup(width=600, height=600)
wn.tracer(0) # Turns off the screen updates
print(f"\nYou chose {user_choice}, and the computer chose {computer_choice}.")

# Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0,0)
head.direction = "stop"
if user_choice == computer_choice:
print("It's a tie!")
elif (user_choice == "rock" and computer_choice == "scissors") or \
(user_choice == "paper" and computer_choice == "rock") or \
(user_choice == "scissors" and computer_choice == "paper"):
print("You win!")
else:
print("Computer wins!")

# Snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0,100)

segments = []

# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0 High Score: 0", align="center", font=("Courier", 24, "normal"))

# Functions
def go_up():
if head.direction != "down":
head.direction = "up"

def go_down():
if head.direction != "up":
head.direction = "down"

def go_left():
if head.direction != "right":
head.direction = "left"

def go_right():
if head.direction != "left":
head.direction = "right"

def move():
if head.direction == "up":
y = head.ycor()
head.sety(y + 20)

if head.direction == "down":
y = head.ycor()
head.sety(y - 20)

if head.direction == "left":
x = head.xcor()
head.setx(x - 20)

if head.direction == "right":
x = head.xcor()
head.setx(x + 20)

# Keyboard bindings
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")

# Main game loop
while True:
wn.update()

# Check for a collision with the border
if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290:
time.sleep(1)
head.goto(0,0)
head.direction = "stop"

# Hide the segments
for segment in segments:
segment.goto(1000, 1000)

# Clear the segments list
segments.clear()

# Reset the score
score = 0

# Reset the delay
delay = 0.1

pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))


# Check for a collision with the food
if head.distance(food) < 20:
# Move the food to a random spot
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x,y)

# Add a segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("grey")
new_segment.penup()
segments.append(new_segment)

# Shorten the delay
delay -= 0.001

# Increase the score
score += 10

if score > high_score:
high_score = score

pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))

# Move the end segments first in reverse order
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)

# Move segment 0 to where the head is
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x,y)

move()

# Check for head collision with the body segments
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0,0)
head.direction = "stop"

# Hide the segments
for segment in segments:
segment.goto(1000, 1000)

# Clear the segments list
segments.clear()

# Reset the score
score = 0

# Reset the delay
delay = 0.1

# Update the score display
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))

time.sleep(delay)

wn.mainloop()
# Run the game
play()