Compare commits
5 Commits
670508261b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| ce3befd167 | |||
| 612ad7c549 | |||
| c34b9d4c52 | |||
| 59a3d6d830 | |||
| 697d53f836 |
@ -6,10 +6,11 @@ class Ball(Turtle):
|
||||
super().__init__()
|
||||
self.shape("circle")
|
||||
self.color("white")
|
||||
self.speed("fastest")
|
||||
self.pu()
|
||||
self.move_speed = 0.1
|
||||
|
||||
self.angle = random.randint(0,45)
|
||||
self.angle = 50
|
||||
self.setheading(self.angle)
|
||||
|
||||
print(self.angle)
|
||||
@ -22,6 +23,6 @@ class Ball(Turtle):
|
||||
self.setheading(self.angle)
|
||||
|
||||
def bounce_paddle(self):
|
||||
self.angle = -self.angle
|
||||
self.angle = 180 - self.angle
|
||||
self.setheading(self.angle)
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
from turtle import Turtle, Screen
|
||||
from paddle import Paddle
|
||||
from ball import Ball
|
||||
from score import Score
|
||||
import time
|
||||
|
||||
class GameManager():
|
||||
BOARD_WIDTH=800
|
||||
@ -21,6 +23,9 @@ class GameManager():
|
||||
self.right_paddle = Paddle(350)
|
||||
self.left_paddle = Paddle(-370)
|
||||
|
||||
self.right_score = Score(30)
|
||||
self.left_score = Score(-30)
|
||||
|
||||
self.ball = Ball()
|
||||
|
||||
def draw_delimiter(self):
|
||||
@ -44,13 +49,31 @@ class GameManager():
|
||||
self.control_paddle()
|
||||
self.ball.move()
|
||||
|
||||
if abs(self.ball.xcor()) >= 400:
|
||||
if self.ball.xcor() > 400:
|
||||
self.game_on = False
|
||||
self.left_score.increment()
|
||||
self.left_score.update()
|
||||
|
||||
if self.ball.xcor() < -400:
|
||||
self.game_on = False
|
||||
self.right_score.increment()
|
||||
self.right_score.update()
|
||||
|
||||
if self.game_on == False:
|
||||
self.ball.clear()
|
||||
self.ball = Ball()
|
||||
self.right_paddle.reset_position(350)
|
||||
self.left_paddle.reset_position(-370)
|
||||
self.game_on = True
|
||||
|
||||
if abs(self.ball.ycor()) > 280:
|
||||
self.ball.bounce_wall()
|
||||
|
||||
print(self.ball.ycor())
|
||||
if (abs(self.ball.xcor() - self.right_paddle.xcor()) <= 20 and abs(self.ball.ycor() - self.right_paddle.ycor()) < 50) or (abs(self.ball.xcor() - self.left_paddle.xcor()) <= 20 and abs(self.ball.ycor() - self.left_paddle.ycor()) < 50):
|
||||
self.ball.bounce_paddle()
|
||||
self.ball.move_speed = self.ball.move_speed * 0.1
|
||||
|
||||
time.sleep(self.ball.move_speed)
|
||||
|
||||
def control_paddle(self):
|
||||
self.screen.listen()
|
||||
@ -1,9 +1,7 @@
|
||||
from game import GameManager
|
||||
from paddle import Paddle
|
||||
import time
|
||||
|
||||
game = GameManager()
|
||||
while game.game_on:
|
||||
while game.game_on == True:
|
||||
game.game_round()
|
||||
time.sleep(0.1)
|
||||
game.screen.exitonclick()
|
||||
@ -10,6 +10,9 @@ class Paddle(Turtle):
|
||||
self.pu()
|
||||
self.teleport(x=x_pos, y=0)
|
||||
|
||||
def reset_position(self, x_pos):
|
||||
self.teleport(x=x_pos, y=0)
|
||||
|
||||
def up(self):
|
||||
self.goto(x=self.xcor(), y=self.ycor()+20)
|
||||
|
||||
18
022/score.py
Normal file
18
022/score.py
Normal file
@ -0,0 +1,18 @@
|
||||
from turtle import Turtle
|
||||
|
||||
class Score(Turtle):
|
||||
def __init__(self, x_pos):
|
||||
super().__init__()
|
||||
self.score = 0
|
||||
self.color("white")
|
||||
self.hideturtle()
|
||||
self.pu()
|
||||
self.goto(x=x_pos, y=270)
|
||||
self.write(self.score, move=False, align='center', font=('Arial', 24, 'normal'))
|
||||
|
||||
def increment(self):
|
||||
self.score += 1
|
||||
|
||||
def update(self):
|
||||
self.clear()
|
||||
self.write(self.score, move=False, align='center', font=('Arial', 24, 'normal'))
|
||||
43
023/car_manager.py
Normal file
43
023/car_manager.py
Normal file
@ -0,0 +1,43 @@
|
||||
import random
|
||||
from turtle import Turtle
|
||||
|
||||
class CarManager:
|
||||
|
||||
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
|
||||
STARTING_MOVE_DISTANCE = 5
|
||||
MOVE_INCREMENT = 10
|
||||
CAR_GENERATION_PROBABILITY = 5
|
||||
|
||||
def __init__(self):
|
||||
self.cars = []
|
||||
self.add_car()
|
||||
|
||||
def execute_round(self, level):
|
||||
if self.should_generate_car_on_turn():
|
||||
self.add_car()
|
||||
|
||||
speed = 5 + (level - 1) * 10
|
||||
self.move(speed)
|
||||
|
||||
def should_generate_car_on_turn(self):
|
||||
return random.randint(1,self.CAR_GENERATION_PROBABILITY) % self.CAR_GENERATION_PROBABILITY == 0
|
||||
|
||||
def move(self, speed):
|
||||
for car in self.cars:
|
||||
car.goto(x=car.xcor()-speed, y=car.ycor())
|
||||
|
||||
def add_car(self):
|
||||
car = Turtle()
|
||||
car.color(random.choice(self.COLORS))
|
||||
car.shape("square")
|
||||
car.shapesize(stretch_wid=1, stretch_len=2)
|
||||
car.pu()
|
||||
car.goto(x=300, y=random.randint(-260, 280))
|
||||
|
||||
self.cars.append(car)
|
||||
|
||||
def check_collision(self, player):
|
||||
for car in self.cars:
|
||||
if abs(player.ycor() - car.ycor()) < 10 and abs(player.xcor() - car.xcor()) < 30:
|
||||
return True
|
||||
return False
|
||||
34
023/main.py
Normal file
34
023/main.py
Normal file
@ -0,0 +1,34 @@
|
||||
import time
|
||||
from turtle import Screen
|
||||
from player import Player
|
||||
from car_manager import CarManager
|
||||
from scoreboard import Scoreboard
|
||||
|
||||
screen = Screen()
|
||||
screen.setup(width=600, height=600)
|
||||
screen.tracer(0)
|
||||
screen.bgcolor("white")
|
||||
|
||||
game_is_on = True
|
||||
|
||||
player = Player()
|
||||
scoreboard = Scoreboard()
|
||||
cars = CarManager()
|
||||
|
||||
while game_is_on:
|
||||
screen.listen()
|
||||
screen.onkey(player.move_up, "Up")
|
||||
cars.execute_round(scoreboard.level)
|
||||
if cars.check_collision(player):
|
||||
game_is_on = False
|
||||
scoreboard.game_over()
|
||||
time.sleep(0.1)
|
||||
screen.update()
|
||||
|
||||
if player.has_won():
|
||||
player.reset_position()
|
||||
scoreboard.level += 1
|
||||
scoreboard.update_score(scoreboard.level)
|
||||
print("Finished")
|
||||
|
||||
screen.exitonclick()
|
||||
27
023/player.py
Normal file
27
023/player.py
Normal file
@ -0,0 +1,27 @@
|
||||
from turtle import Turtle
|
||||
|
||||
class Player(Turtle):
|
||||
STARTING_POSITION = (0, -280)
|
||||
MOVE_DISTANCE = 10
|
||||
FINISH_LINE_Y = 280
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.shape("turtle")
|
||||
self.color("black")
|
||||
self.pu()
|
||||
self.setheading(90)
|
||||
self.goto(self.STARTING_POSITION)
|
||||
|
||||
def move_up(self):
|
||||
self.goto(x=self.xcor(), y=self.ycor() + self.MOVE_DISTANCE)
|
||||
print(self.ycor())
|
||||
|
||||
def has_won(self):
|
||||
if self.ycor() >= self.FINISH_LINE_Y:
|
||||
return True
|
||||
return False
|
||||
|
||||
def reset_position(self):
|
||||
self.clear()
|
||||
self.goto(self.STARTING_POSITION)
|
||||
23
023/scoreboard.py
Normal file
23
023/scoreboard.py
Normal file
@ -0,0 +1,23 @@
|
||||
from turtle import Turtle
|
||||
|
||||
class Scoreboard(Turtle):
|
||||
|
||||
FONT = ("Courier", 24, "normal")
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.color("black")
|
||||
self.pu()
|
||||
self.hideturtle()
|
||||
self.goto(-280, 260)
|
||||
self.level = 1
|
||||
|
||||
self.update_score(self.level)
|
||||
|
||||
def update_score(self, level):
|
||||
self.clear()
|
||||
self.write(f"Level: {level}", False, align="left", font=self.FONT)
|
||||
|
||||
def game_over(self):
|
||||
self.goto(0,0)
|
||||
self.write("GAME OVER", False, align="center", font=self.FONT)
|
||||
7
024/input/letters/starting_letter.txt
Normal file
7
024/input/letters/starting_letter.txt
Normal file
@ -0,0 +1,7 @@
|
||||
Dear [name],
|
||||
|
||||
You are invited to my birthday this Saturday.
|
||||
|
||||
Hope you can make it!
|
||||
|
||||
Angela
|
||||
8
024/input/names/invited_names.txt
Normal file
8
024/input/names/invited_names.txt
Normal file
@ -0,0 +1,8 @@
|
||||
Aang
|
||||
Zuko
|
||||
Appa
|
||||
Katara
|
||||
Sokka
|
||||
Momo
|
||||
Uncle Iroh
|
||||
Toph
|
||||
20
024/main.py
Normal file
20
024/main.py
Normal file
@ -0,0 +1,20 @@
|
||||
#TODO: Create a letter using starting_letter.txt
|
||||
#for each name in invited_names.txt
|
||||
#Replace the [name] placeholder with the actual name.
|
||||
#Save the letters in the folder "ReadyToSend".
|
||||
|
||||
#Hint1: This method will help you: https://www.w3schools.com/python/ref_file_readlines.asp
|
||||
#Hint2: This method will also help you: https://www.w3schools.com/python/ref_string_replace.asp
|
||||
#Hint3: THis method will help you: https://www.w3schools.com/python/ref_string_strip.asp
|
||||
|
||||
|
||||
with open("input/names/invited_names.txt") as n:
|
||||
for name in n.readlines():
|
||||
name = name.strip()
|
||||
output_letter = ""
|
||||
with open("input/letters/starting_letter.txt") as l:
|
||||
for letter_line in l:
|
||||
output_letter += letter_line.replace("[name]", name)
|
||||
|
||||
with open(f"output/ready_to_send/to_{name}.txt", "w") as final:
|
||||
final.write(output_letter)
|
||||
7
024/output/ready_to_send/example.txt
Normal file
7
024/output/ready_to_send/example.txt
Normal file
@ -0,0 +1,7 @@
|
||||
Dear Aang,
|
||||
|
||||
You are invited to my birthday this Saturday.
|
||||
|
||||
Hope you can make it!
|
||||
|
||||
Angela
|
||||
7
024/output/ready_to_send/to_Aang.txt
Normal file
7
024/output/ready_to_send/to_Aang.txt
Normal file
@ -0,0 +1,7 @@
|
||||
Dear Aang,
|
||||
|
||||
You are invited to my birthday this Saturday.
|
||||
|
||||
Hope you can make it!
|
||||
|
||||
Angela
|
||||
7
024/output/ready_to_send/to_Appa.txt
Normal file
7
024/output/ready_to_send/to_Appa.txt
Normal file
@ -0,0 +1,7 @@
|
||||
Dear Appa,
|
||||
|
||||
You are invited to my birthday this Saturday.
|
||||
|
||||
Hope you can make it!
|
||||
|
||||
Angela
|
||||
7
024/output/ready_to_send/to_Katara.txt
Normal file
7
024/output/ready_to_send/to_Katara.txt
Normal file
@ -0,0 +1,7 @@
|
||||
Dear Katara,
|
||||
|
||||
You are invited to my birthday this Saturday.
|
||||
|
||||
Hope you can make it!
|
||||
|
||||
Angela
|
||||
7
024/output/ready_to_send/to_Momo.txt
Normal file
7
024/output/ready_to_send/to_Momo.txt
Normal file
@ -0,0 +1,7 @@
|
||||
Dear Momo,
|
||||
|
||||
You are invited to my birthday this Saturday.
|
||||
|
||||
Hope you can make it!
|
||||
|
||||
Angela
|
||||
7
024/output/ready_to_send/to_Sokka.txt
Normal file
7
024/output/ready_to_send/to_Sokka.txt
Normal file
@ -0,0 +1,7 @@
|
||||
Dear Sokka,
|
||||
|
||||
You are invited to my birthday this Saturday.
|
||||
|
||||
Hope you can make it!
|
||||
|
||||
Angela
|
||||
7
024/output/ready_to_send/to_Toph.txt
Normal file
7
024/output/ready_to_send/to_Toph.txt
Normal file
@ -0,0 +1,7 @@
|
||||
Dear Toph,
|
||||
|
||||
You are invited to my birthday this Saturday.
|
||||
|
||||
Hope you can make it!
|
||||
|
||||
Angela
|
||||
7
024/output/ready_to_send/to_Uncle Iroh.txt
Normal file
7
024/output/ready_to_send/to_Uncle Iroh.txt
Normal file
@ -0,0 +1,7 @@
|
||||
Dear Uncle Iroh,
|
||||
|
||||
You are invited to my birthday this Saturday.
|
||||
|
||||
Hope you can make it!
|
||||
|
||||
Angela
|
||||
7
024/output/ready_to_send/to_Zuko.txt
Normal file
7
024/output/ready_to_send/to_Zuko.txt
Normal file
@ -0,0 +1,7 @@
|
||||
Dear Zuko,
|
||||
|
||||
You are invited to my birthday this Saturday.
|
||||
|
||||
Hope you can make it!
|
||||
|
||||
Angela
|
||||
51
025/50_states.csv
Normal file
51
025/50_states.csv
Normal file
@ -0,0 +1,51 @@
|
||||
state,x,y
|
||||
Alabama,139,-77
|
||||
Alaska,-204,-170
|
||||
Arizona,-203,-40
|
||||
Arkansas,57,-53
|
||||
California,-297,13
|
||||
Colorado,-112,20
|
||||
Connecticut,297,96
|
||||
Delaware,275,42
|
||||
Florida,220,-145
|
||||
Georgia,182,-75
|
||||
Hawaii,-317,-143
|
||||
Idaho,-216,122
|
||||
Illinois,95,37
|
||||
Indiana,133,39
|
||||
Iowa,38,65
|
||||
Kansas,-17,5
|
||||
Kentucky,149,1
|
||||
Louisiana,59,-114
|
||||
Maine,319,164
|
||||
Maryland,288,27
|
||||
Massachusetts,312,112
|
||||
Michigan,148,101
|
||||
Minnesota,23,135
|
||||
Mississippi,94,-78
|
||||
Missouri,49,6
|
||||
Montana,-141,150
|
||||
Nebraska,-61,66
|
||||
Nevada,-257,56
|
||||
New Hampshire,302,127
|
||||
New Jersey,282,65
|
||||
New Mexico,-128,-43
|
||||
New York,236,104
|
||||
North Carolina,239,-22
|
||||
North Dakota,-44,158
|
||||
Ohio,176,52
|
||||
Oklahoma,-8,-41
|
||||
Oregon,-278,138
|
||||
Pennsylvania,238,72
|
||||
Rhode Island,318,94
|
||||
South Carolina,218,-51
|
||||
South Dakota,-44,109
|
||||
Tennessee,131,-34
|
||||
Texas,-38,-106
|
||||
Utah,-189,34
|
||||
Vermont,282,154
|
||||
Virginia,234,12
|
||||
Washington,-257,193
|
||||
West Virginia,200,20
|
||||
Wisconsin,83,113
|
||||
Wyoming,-134,90
|
||||
|
BIN
025/blank_states_img.gif
Normal file
BIN
025/blank_states_img.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
52
025/main.py
Normal file
52
025/main.py
Normal file
@ -0,0 +1,52 @@
|
||||
from turtle import Turtle, Screen
|
||||
import pandas as pd
|
||||
|
||||
FONT = ("Courier", 14, "normal")
|
||||
|
||||
screen = Screen()
|
||||
screen.setup(725,491)
|
||||
screen.bgpic('blank_states_img.gif')
|
||||
screen.tracer(0)
|
||||
|
||||
data = pd.read_csv("50_states.csv")
|
||||
# state, x, y
|
||||
|
||||
game_on = True
|
||||
states_remaining = len(data)
|
||||
found_states = []
|
||||
|
||||
while len(found_states) < 50:
|
||||
guess = str(screen.textinput(f"{states_remaining} states to find", "Find a state:"))
|
||||
if guess == "None":
|
||||
missing_states = [ s for s in data.states if s not in found_states ]
|
||||
output = pd.DataFrame(missing_states)
|
||||
output.to_csv("missing_states.csv")
|
||||
exit()
|
||||
|
||||
state = data[data.state == guess.title()]
|
||||
|
||||
# if the subset is not empty, it means the state is in the list
|
||||
if len(state) > 0:
|
||||
|
||||
|
||||
x_pos = state.x.item() #state.iloc[0]["x"]
|
||||
y_pos = state.y.item() #state.iloc[0]["y"]
|
||||
state_name = state.iloc[0]["state"]
|
||||
|
||||
if state_name in found_states:
|
||||
continue
|
||||
|
||||
states_remaining -= 1
|
||||
|
||||
found_states.append(state_name)
|
||||
text = Turtle()
|
||||
text.pu()
|
||||
text.color("black")
|
||||
text.hideturtle()
|
||||
text.goto(x_pos, y_pos)
|
||||
text.write(state_name, align="center", font=FONT)
|
||||
screen.update()
|
||||
|
||||
|
||||
|
||||
screen.mainloop()
|
||||
48
025/missing_states.csv
Normal file
48
025/missing_states.csv
Normal file
@ -0,0 +1,48 @@
|
||||
Alabama
|
||||
Alaska
|
||||
Arkansas
|
||||
California
|
||||
Colorado
|
||||
Connecticut
|
||||
Delaware
|
||||
Florida
|
||||
Georgia
|
||||
Hawaii
|
||||
Idaho
|
||||
Illinois
|
||||
Indiana
|
||||
Iowa
|
||||
Kansas
|
||||
Kentucky
|
||||
Louisiana
|
||||
Maine
|
||||
Maryland
|
||||
Massachusetts
|
||||
Michigan
|
||||
Minnesota
|
||||
Mississippi
|
||||
Missouri
|
||||
Montana
|
||||
Nebraska
|
||||
Nevada
|
||||
New Hampshire
|
||||
New Jersey
|
||||
New Mexico
|
||||
New York
|
||||
North Carolina
|
||||
North Dakota
|
||||
Oklahoma
|
||||
Oregon
|
||||
Pennsylvania
|
||||
Rhode Island
|
||||
South Carolina
|
||||
South Dakota
|
||||
Tennessee
|
||||
Texas
|
||||
Utah
|
||||
Vermont
|
||||
Virginia
|
||||
Washington
|
||||
West Virginia
|
||||
Wisconsin
|
||||
Wyoming
|
||||
|
3024
025/squirrels.csv
Normal file
3024
025/squirrels.csv
Normal file
File diff suppressed because it is too large
Load Diff
54
025/task.py
Normal file
54
025/task.py
Normal file
@ -0,0 +1,54 @@
|
||||
# data = []
|
||||
# with open("weather_data.csv") as f:
|
||||
# for line in f.readlines():
|
||||
# data.append(line.strip().split(','))
|
||||
|
||||
# print(data)
|
||||
|
||||
# ------------------------------------------------------------------------
|
||||
# import csv
|
||||
|
||||
# temperatures = []
|
||||
# with open("weather_data.csv") as f:
|
||||
# reader = csv.reader(f)
|
||||
# next(reader)
|
||||
|
||||
# for row in reader:
|
||||
# temperatures.append(int(row[1]))
|
||||
|
||||
# print(temperatures)
|
||||
|
||||
# ------------------------------------------------------------------------
|
||||
|
||||
# import pandas as pd
|
||||
|
||||
# data = pd.read_csv("weather_data.csv", delimiter=",")
|
||||
# print(data["temp"].mean())
|
||||
# print(data["temp"].max())
|
||||
|
||||
# output = data[data.temp == data.temp.max()]
|
||||
# print(output)
|
||||
|
||||
# ------------------------------------------------------------------------
|
||||
|
||||
import pandas as pd
|
||||
|
||||
data = pd.read_csv("squirrels.csv")
|
||||
|
||||
colors = data["Primary Fur Color"].unique()
|
||||
|
||||
output_colors = []
|
||||
output_count = []
|
||||
for color in colors:
|
||||
if type(color) != str:
|
||||
continue
|
||||
count = len(data[data["Primary Fur Color"] == color])
|
||||
|
||||
output_colors.append(color)
|
||||
output_count.append(count)
|
||||
|
||||
output = {"color": output_colors, "count": output_count}
|
||||
|
||||
df = pd.DataFrame(output)
|
||||
|
||||
print(df.to_csv(index=False))
|
||||
8
025/weather_data.csv
Normal file
8
025/weather_data.csv
Normal file
@ -0,0 +1,8 @@
|
||||
day,temp,condition
|
||||
Monday,12,Sunny
|
||||
Tuesday,14,Rain
|
||||
Wednesday,15,Rain
|
||||
Thursday,14,Cloudy
|
||||
Friday,21,Sunny
|
||||
Saturday,22,Sunny
|
||||
Sunday,24,Sunny
|
||||
|
15
026/main.py
Normal file
15
026/main.py
Normal file
@ -0,0 +1,15 @@
|
||||
import pandas as pd
|
||||
|
||||
# Keyword Method with iterrows()
|
||||
# {new_key:new_value for (index, row) in df.iterrows()}
|
||||
|
||||
#TODO 1. Create a dictionary in this format:
|
||||
# {"A": "Alfa", "B": "Bravo"}
|
||||
|
||||
data = pd.read_csv("nato_phonetic_alphabet.csv")
|
||||
dict = { row.letter:row.code for (index, row) in data.iterrows() }
|
||||
|
||||
#TODO 2. Create a list of the phonetic code words from a word that the user inputs.
|
||||
|
||||
input = list(input("Which word do you want to spell?\n"))
|
||||
print([ dict[letter.upper()] for letter in input])
|
||||
27
026/nato_phonetic_alphabet.csv
Normal file
27
026/nato_phonetic_alphabet.csv
Normal file
@ -0,0 +1,27 @@
|
||||
letter,code
|
||||
A,Alfa
|
||||
B,Bravo
|
||||
C,Charlie
|
||||
D,Delta
|
||||
E,Echo
|
||||
F,Foxtrot
|
||||
G,Golf
|
||||
H,Hotel
|
||||
I,India
|
||||
J,Juliet
|
||||
K,Kilo
|
||||
L,Lima
|
||||
M,Mike
|
||||
N,November
|
||||
O,Oscar
|
||||
P,Papa
|
||||
Q,Quebec
|
||||
R,Romeo
|
||||
S,Sierra
|
||||
T,Tango
|
||||
U,Uniform
|
||||
V,Victor
|
||||
W,Whiskey
|
||||
X,X-ray
|
||||
Y,Yankee
|
||||
Z,Zulu
|
||||
|
5
026/task.py
Normal file
5
026/task.py
Normal file
@ -0,0 +1,5 @@
|
||||
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
|
||||
new_numbers = [ n for n in numbers if n % 2 == 1 ]
|
||||
|
||||
print(new_numbers)
|
||||
Reference in New Issue
Block a user