initial commit
This commit is contained in:
commit
39f56a3b10
83 files changed
+3583
No files matched your search
Generated
+5
@@ -0,0 +1,5 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
Generated
+1
@@ -0,0 +1 @@
|
|||||||
|
Prog_A
|
||||||
Generated
+10
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="jdk" jdkName="Python 3.12 (PythonProject)" jdkType="Python SDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
<component name="ProjectDictionaryState">
|
||||||
|
<dictionary name="project">
|
||||||
|
<words>
|
||||||
|
<w>smorkelo</w>
|
||||||
|
</words>
|
||||||
|
</dictionary>
|
||||||
|
</component>
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
||||||
Generated
+10
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Black">
|
||||||
|
<option name="sdkName" value="Python 3.12 (PythonProject)" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (PythonProject)" project-jdk-type="Python SDK" />
|
||||||
|
<component name="PythonCompatibilityInspectionAdvertiser">
|
||||||
|
<option name="version" value="3" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/Prog_A.iml" filepath="$PROJECT_DIR$/.idea/Prog_A.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
from my_package.Particle_class import Particle
|
||||||
|
|
||||||
|
|
||||||
|
class Spark(Particle):
|
||||||
|
def __init__(self,x,y,color,fade_speed=4):
|
||||||
|
super().__init__(x,y,color,fade_speed)
|
||||||
|
self.y = y
|
||||||
|
self.rad = CIRCLE_RADIUS
|
||||||
|
self.image = pygame.Surface((self.rad * 2, self.rad * 2), pygame.SRCALPHA)
|
||||||
|
pygame.draw.circle(self.image, self.color, (self.rad, self.rad), self.rad)
|
||||||
|
|
||||||
|
def appear(self, screen):
|
||||||
|
if self.alpha > 0:
|
||||||
|
self.image.set_alpha(self.alpha)
|
||||||
|
screen.blit(self.image, (self.pos[0] - self.rad, self.pos[1] - self.rad))
|
||||||
|
|
||||||
|
|
||||||
|
particles = []
|
||||||
|
width = 600
|
||||||
|
height = 600
|
||||||
|
SIZE = (width, height)
|
||||||
|
WHITE = (255, 255, 255)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
BLUE = (0, 0, 255)
|
||||||
|
ORANGE = (255, 128, 0)
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
CIRCLE_RADIUS = 3
|
||||||
|
f_x = [-0.1,0,0.1]
|
||||||
|
f_y = [-0.2,-0.1]
|
||||||
|
COLOR = (ORANGE, RED)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
screen = init_game()
|
||||||
|
game_loop(screen)
|
||||||
|
exit_game()
|
||||||
|
|
||||||
|
def init_game():
|
||||||
|
global clock
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
pygame.init()
|
||||||
|
screen = pygame.display.set_mode(SIZE)
|
||||||
|
return screen
|
||||||
|
|
||||||
|
def game_loop(screen):
|
||||||
|
while True:
|
||||||
|
if not event_handling():
|
||||||
|
break
|
||||||
|
if not update_game():
|
||||||
|
break
|
||||||
|
draw_screen(screen)
|
||||||
|
|
||||||
|
|
||||||
|
def event_handling():
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_game():
|
||||||
|
for _ in range(2):
|
||||||
|
particles.append(Spark(300,300,random.choice(COLOR)))
|
||||||
|
|
||||||
|
for particle_object in particles:
|
||||||
|
particle_object.apply_force((random.choice(f_x), -0.1))
|
||||||
|
|
||||||
|
if particle_object.y <= 0 or particle_object.alpha <= 0:
|
||||||
|
|
||||||
|
print(particle_object)
|
||||||
|
particles.remove(particle_object)
|
||||||
|
|
||||||
|
particle_object.fade()
|
||||||
|
particle_object.update()
|
||||||
|
|
||||||
|
clock.tick(60)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_screen(screen):
|
||||||
|
screen.fill(BLACK)
|
||||||
|
for particle_object in particles:
|
||||||
|
particle_object.set_surface(screen)
|
||||||
|
particle_object.appear(screen)
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def exit_game():
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
from my_package.Particle_class import Particle
|
||||||
|
|
||||||
|
|
||||||
|
class Spark(Particle):
|
||||||
|
def __init__(self, x, y, color, fade_speed=4):
|
||||||
|
super().__init__(x, y, color, fade_speed)
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.rad = CIRCLE_RADIUS
|
||||||
|
self.image = pygame.Surface((self.rad * 2, self.rad * 2), pygame.SRCALPHA)
|
||||||
|
pygame.draw.circle(self.image, self.color, (self.rad, self.rad), self.rad)
|
||||||
|
|
||||||
|
def appear(self, screen):
|
||||||
|
if self.alpha > 0:
|
||||||
|
self.image.set_alpha(self.alpha)
|
||||||
|
screen.blit(self.image, (self.pos[0] - self.rad, self.pos[1] - self.rad))
|
||||||
|
|
||||||
|
particles = []
|
||||||
|
width = 600
|
||||||
|
height = 600
|
||||||
|
SIZE = (width, height)
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
CIRCLE_RADIUS = 1
|
||||||
|
COLORS = [(255,0,0), (0,0,255), (255,0,255), (0,255,255), (255,0,255)]
|
||||||
|
FRICTION = -0.5
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
screen = init_game()
|
||||||
|
game_loop(screen)
|
||||||
|
exit_game()
|
||||||
|
|
||||||
|
def init_game():
|
||||||
|
global clock
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
pygame.init()
|
||||||
|
screen = pygame.display.set_mode(SIZE)
|
||||||
|
return screen
|
||||||
|
|
||||||
|
def game_loop(screen):
|
||||||
|
while True:
|
||||||
|
if not event_handling():
|
||||||
|
break
|
||||||
|
if not update_game():
|
||||||
|
break
|
||||||
|
draw_screen(screen)
|
||||||
|
|
||||||
|
|
||||||
|
def event_handling():
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
x = pygame.mouse.get_pos()[0]
|
||||||
|
y = pygame.mouse.get_pos()[1]
|
||||||
|
for _ in range(3000):
|
||||||
|
particles.append(Spark(x, y, random.choice(COLORS)))
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_game():
|
||||||
|
for particle_object in particles:
|
||||||
|
|
||||||
|
particle_object.apply_force((random.randint(-2,2),random.randint(-2,2)))
|
||||||
|
|
||||||
|
if particle_object.y <= 0 or particle_object.x <= 0 or particle_object.y == height or particle_object.x == width or particle_object.alpha <= 0:
|
||||||
|
particle_object.set_surface(None)
|
||||||
|
print(particle_object)
|
||||||
|
particles.remove(particle_object)
|
||||||
|
|
||||||
|
particle_object.fade()
|
||||||
|
particle_object.update()
|
||||||
|
|
||||||
|
clock.tick(60)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_screen(screen):
|
||||||
|
screen.fill(BLACK)
|
||||||
|
for particle_object in particles:
|
||||||
|
particle_object.set_surface(screen)
|
||||||
|
particle_object.appear(screen)
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def exit_game():
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
from my_package.Particle_class import Particle
|
||||||
|
|
||||||
|
|
||||||
|
class Spark(Particle):
|
||||||
|
def __init__(self, x, y, color=(255,255,255), fade_speed=20):
|
||||||
|
super().__init__(x, y, color, fade_speed)
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.rad = CIRCLE_RADIUS
|
||||||
|
self.image = pygame.Surface((self.rad * 2, self.rad * 2), pygame.SRCALPHA)
|
||||||
|
pygame.draw.circle(self.image, self.color, (self.rad, self.rad), self.rad)
|
||||||
|
|
||||||
|
def appear(self, screen):
|
||||||
|
if self.alpha > 0:
|
||||||
|
self.image.set_alpha(self.alpha)
|
||||||
|
screen.blit(self.image, (self.pos[0] - self.rad, self.pos[1] - self.rad))
|
||||||
|
|
||||||
|
particles = []
|
||||||
|
width = 600
|
||||||
|
height = 600
|
||||||
|
SIZE = (width, height)
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
CIRCLE_RADIUS = 2
|
||||||
|
f_x = [-0.1,0,0.1]
|
||||||
|
|
||||||
|
def main():
|
||||||
|
screen = init_game()
|
||||||
|
game_loop(screen)
|
||||||
|
exit_game()
|
||||||
|
|
||||||
|
def init_game():
|
||||||
|
global clock
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
pygame.init()
|
||||||
|
screen = pygame.display.set_mode(SIZE)
|
||||||
|
return screen
|
||||||
|
|
||||||
|
def game_loop(screen):
|
||||||
|
while True:
|
||||||
|
if not event_handling():
|
||||||
|
break
|
||||||
|
if not update_game():
|
||||||
|
break
|
||||||
|
draw_screen(screen)
|
||||||
|
|
||||||
|
|
||||||
|
def event_handling():
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_game():
|
||||||
|
x = pygame.mouse.get_pos()[0]
|
||||||
|
y = pygame.mouse.get_pos()[1]
|
||||||
|
for _ in range(10):
|
||||||
|
particles.append(Spark(x,y))
|
||||||
|
|
||||||
|
for particle_object in particles:
|
||||||
|
particle_object.apply_force((random.randint(-2,2),random.randint(-2,2)))
|
||||||
|
|
||||||
|
if particle_object.y <= 0 or particle_object.x <= 0 or particle_object.y == height or particle_object.x == width or particle_object.alpha <= 0:
|
||||||
|
particle_object.set_surface(None)
|
||||||
|
print(particle_object)
|
||||||
|
particles.remove(particle_object)
|
||||||
|
|
||||||
|
particle_object.fade()
|
||||||
|
particle_object.update()
|
||||||
|
|
||||||
|
clock.tick(60)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_screen(screen):
|
||||||
|
screen.fill(BLACK)
|
||||||
|
for particle_object in particles:
|
||||||
|
particle_object.set_surface(screen)
|
||||||
|
particle_object.appear(screen)
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def exit_game():
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
import random
|
||||||
|
import time
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
|
||||||
|
class Star:
|
||||||
|
def __init__(self,visible,pos):
|
||||||
|
self.visible = visible
|
||||||
|
self.pos = pos
|
||||||
|
self.size = random.randint(1,3)
|
||||||
|
def set_visible(self):
|
||||||
|
self.visible = True
|
||||||
|
def star_appear(self,screen):
|
||||||
|
pygame.draw.circle(screen, WHITE, self.pos, self.size)
|
||||||
|
|
||||||
|
stars = []
|
||||||
|
width = 640
|
||||||
|
height = 600
|
||||||
|
SIZE = (width, height)
|
||||||
|
WHITE = (255, 255, 255)
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
screen = init_game()
|
||||||
|
game_loop(screen)
|
||||||
|
exit_game()
|
||||||
|
|
||||||
|
def init_game():
|
||||||
|
pygame.init()
|
||||||
|
screen = pygame.display.set_mode(SIZE)
|
||||||
|
return screen
|
||||||
|
|
||||||
|
def game_loop(screen):
|
||||||
|
while True:
|
||||||
|
if not event_handling():
|
||||||
|
break
|
||||||
|
if not update_game(screen):
|
||||||
|
break
|
||||||
|
event = event_handling()
|
||||||
|
update_game(screen)
|
||||||
|
draw_screen(screen)
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
def event_handling():
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_game(screen):
|
||||||
|
for _ in range (2):
|
||||||
|
night_sky(screen)
|
||||||
|
random.choice(stars).set_visible()
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_screen(screen):
|
||||||
|
screen.fill(BLACK)
|
||||||
|
x = 0
|
||||||
|
for _ in stars:
|
||||||
|
if stars[x].visible:
|
||||||
|
stars[x].star_appear(screen)
|
||||||
|
x+=1
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def night_sky(screen):
|
||||||
|
pos = (random.randint(0, SIZE[0]), random.randint(0, SIZE[1]))
|
||||||
|
stars.append(Star(False, pos))
|
||||||
|
|
||||||
|
def exit_game():
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import tkinter as tk
|
||||||
|
|
||||||
|
from my_package.App_window import AppWindow
|
||||||
|
|
||||||
|
|
||||||
|
class TrainingApp(AppWindow):
|
||||||
|
def __init__(self,width,height):
|
||||||
|
super().__init__(width,height)
|
||||||
|
self.listbox = None
|
||||||
|
self.entry = None
|
||||||
|
self.button = None
|
||||||
|
|
||||||
|
def on_click(self):
|
||||||
|
self.listbox.insert(tk.END,self.entry.get())
|
||||||
|
self.entry.delete(0,tk.END)
|
||||||
|
|
||||||
|
def init_widgets(self):
|
||||||
|
self.listbox = tk.Listbox(self.root)
|
||||||
|
self.listbox.pack()
|
||||||
|
self.entry = tk.Entry(self.root)
|
||||||
|
self.entry.pack()
|
||||||
|
self.button = tk.Button(self.root, text="save", command=self.on_click)
|
||||||
|
self.button.pack()
|
||||||
|
|
||||||
|
t = TrainingApp(500,500)
|
||||||
|
t.run()
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
from Prog_1.Pygame_games.first_games import draw_game, update_game
|
||||||
|
|
||||||
|
width = 800
|
||||||
|
height = 600
|
||||||
|
SIZE = (width, height)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
screen = init_game()
|
||||||
|
game_loop(screen)
|
||||||
|
exit_game()
|
||||||
|
|
||||||
|
def init_game():
|
||||||
|
pygame.init()
|
||||||
|
screen = pygame.display.set_mode(SIZE)
|
||||||
|
return screen
|
||||||
|
|
||||||
|
def game_loop(screen):
|
||||||
|
while True:
|
||||||
|
if event_handling() == False:
|
||||||
|
break
|
||||||
|
if update_game() == False:
|
||||||
|
break
|
||||||
|
event = event_handling()
|
||||||
|
update_game()
|
||||||
|
draw_game(screen)
|
||||||
|
|
||||||
|
def event_handling():
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_game():
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_screen(screen):
|
||||||
|
screen.fill("BLACK")
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def exit_game():
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
WIDTH = 800
|
||||||
|
HEIGHT= 600
|
||||||
|
SIZE = (WIDTH, HEIGHT)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
CIRCLE_RADIUS = 10
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
FPS = 60
|
||||||
|
|
||||||
|
def main():
|
||||||
|
screen = init_game()
|
||||||
|
game_loop(screen)
|
||||||
|
exit_game()
|
||||||
|
|
||||||
|
def init_game():
|
||||||
|
global position
|
||||||
|
global clock
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
position = (WIDTH // 2, HEIGHT // 2)
|
||||||
|
pygame.init()
|
||||||
|
return pygame.display.set_mode(SIZE)
|
||||||
|
|
||||||
|
def game_loop(screen):
|
||||||
|
while True:
|
||||||
|
if not event_handling():
|
||||||
|
break
|
||||||
|
if not update_game():
|
||||||
|
break
|
||||||
|
event = event_handling()
|
||||||
|
draw_game(screen)
|
||||||
|
clock.tick(FPS)
|
||||||
|
|
||||||
|
def event_handling():
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_game():
|
||||||
|
global new_pos
|
||||||
|
mouse_pos = pygame.mouse.get_pos()
|
||||||
|
delta_x = mouse_pos[0] - position[0]
|
||||||
|
delta_y = mouse_pos[1] - position[1]
|
||||||
|
acceleration = delta_x / WIDTH, delta_y / HEIGHT
|
||||||
|
new_pos = (mouse_pos[0] + acceleration[0], mouse_pos[1] + acceleration[1])
|
||||||
|
clock.tick(FPS)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_game(screen):
|
||||||
|
screen.fill(BLACK)
|
||||||
|
pygame.draw.circle(screen, RED, new_pos, CIRCLE_RADIUS)
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def exit_game():
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
WIDTH = 800
|
||||||
|
HEIGHT= 600
|
||||||
|
SIZE = (WIDTH, HEIGHT)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
CIRCLE_RADIUS = 10
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
FPS = 120
|
||||||
|
|
||||||
|
def main():
|
||||||
|
screen = init_game()
|
||||||
|
game_loop(screen)
|
||||||
|
exit_game()
|
||||||
|
|
||||||
|
def init_game():
|
||||||
|
global position
|
||||||
|
global clock
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
position = (WIDTH // 2, HEIGHT // 2)
|
||||||
|
pygame.init()
|
||||||
|
return pygame.display.set_mode(SIZE)
|
||||||
|
|
||||||
|
def game_loop(screen):
|
||||||
|
while True:
|
||||||
|
if not event_handling():
|
||||||
|
break
|
||||||
|
if not update_game():
|
||||||
|
break
|
||||||
|
event = event_handling()
|
||||||
|
draw_game(screen)
|
||||||
|
clock.tick(FPS)
|
||||||
|
|
||||||
|
def event_handling():
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_game():
|
||||||
|
global position
|
||||||
|
keys_pressed = pygame.key.get_pressed()
|
||||||
|
if keys_pressed[pygame.K_UP]:
|
||||||
|
position = (position[0], position[1] - 1)
|
||||||
|
if keys_pressed[pygame.K_DOWN]:
|
||||||
|
position = (position[0], position[1] + 1)
|
||||||
|
if keys_pressed[pygame.K_LEFT]:
|
||||||
|
position = (position[0] - 1, position[1])
|
||||||
|
if keys_pressed[pygame.K_RIGHT]:
|
||||||
|
position = (position[0] + 1, position[1])
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_game(screen):
|
||||||
|
screen.fill(BLACK)
|
||||||
|
pygame.draw.circle(screen, RED, position, CIRCLE_RADIUS)
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def exit_game():
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
|
||||||
|
class Cell:
|
||||||
|
def __init__(self,pos,size):
|
||||||
|
self.pos = pos
|
||||||
|
self.size = size
|
||||||
|
|
||||||
|
def grow(self):
|
||||||
|
self.size = self.size + 1
|
||||||
|
|
||||||
|
def movement(self):
|
||||||
|
keys_pressed = pygame.key.get_pressed()
|
||||||
|
if keys_pressed[pygame.K_UP]:
|
||||||
|
pos = (cell.pos[0], cell.pos[1] - 1)
|
||||||
|
self.pos = pos
|
||||||
|
if keys_pressed[pygame.K_DOWN]:
|
||||||
|
pos = (cell.pos[0], cell.pos[1] + 1)
|
||||||
|
self.pos = pos
|
||||||
|
if keys_pressed[pygame.K_LEFT]:
|
||||||
|
pos = (cell.pos[0] - 1, cell.pos[1])
|
||||||
|
self.pos = pos
|
||||||
|
if keys_pressed[pygame.K_RIGHT]:
|
||||||
|
pos = (cell.pos[0] + 1, cell.pos[1])
|
||||||
|
self.pos = pos
|
||||||
|
|
||||||
|
def appearance(self,screen):
|
||||||
|
pygame.draw.circle(screen, RED, self.pos, self.size)
|
||||||
|
|
||||||
|
|
||||||
|
class Nutrient:
|
||||||
|
def __init__(self,visible,pos,size):
|
||||||
|
self.visible = visible
|
||||||
|
self.pos = pos
|
||||||
|
self.size = size
|
||||||
|
|
||||||
|
def appear(self,screen):
|
||||||
|
pygame.draw.circle(screen, BLUE, self.pos, CIRCLE_RADIUS)
|
||||||
|
|
||||||
|
def consumed(self,pos):
|
||||||
|
del self
|
||||||
|
del nutrients[pos]
|
||||||
|
cell.grow()
|
||||||
|
|
||||||
|
def check_collision(self):
|
||||||
|
if ((cell.pos[0] - self.pos[0]) ** 2) + ((cell.pos[1] - self.pos[1]) ** 2) <= (cell.size + self.size) ** 2:
|
||||||
|
self.consumed(self.pos)
|
||||||
|
|
||||||
|
nutrients = {}
|
||||||
|
width = 640
|
||||||
|
height = 600
|
||||||
|
SIZE = (width, height)
|
||||||
|
WHITE = (255, 255, 255)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
BLUE = (0, 0, 255)
|
||||||
|
CIRCLE_RADIUS = 2
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
screen = init_game()
|
||||||
|
game_loop(screen)
|
||||||
|
exit_game()
|
||||||
|
|
||||||
|
def init_game():
|
||||||
|
global clock
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
pygame.init()
|
||||||
|
screen = pygame.display.set_mode(SIZE)
|
||||||
|
return screen
|
||||||
|
|
||||||
|
def game_loop(screen):
|
||||||
|
global cell
|
||||||
|
cell = Cell((300,300),5)
|
||||||
|
spawn_nutrition()
|
||||||
|
while True:
|
||||||
|
if not event_handling():
|
||||||
|
break
|
||||||
|
if not update_game():
|
||||||
|
break
|
||||||
|
event = event_handling()
|
||||||
|
update_game()
|
||||||
|
draw_screen(screen)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def event_handling():
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_game():
|
||||||
|
n_copy = nutrients.copy()
|
||||||
|
for x,y in n_copy:
|
||||||
|
nutrients[(x,y)].check_collision()
|
||||||
|
del n_copy
|
||||||
|
cell.movement()
|
||||||
|
clock.tick(60)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_screen(screen):
|
||||||
|
screen.fill(WHITE)
|
||||||
|
x = 0
|
||||||
|
cell.appearance(screen)
|
||||||
|
for x,y in nutrients:
|
||||||
|
if nutrients[x,y].visible:
|
||||||
|
nutrients[x,y].appear(screen)
|
||||||
|
x+=1
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def spawn_nutrition():
|
||||||
|
for _ in range(100):
|
||||||
|
pos = (random.randint(0, SIZE[0]), random.randint(0, SIZE[1]))
|
||||||
|
nutrients[pos]=(Nutrient(True, pos,2))
|
||||||
|
|
||||||
|
def exit_game():
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
BLOCKSIZE = 20
|
||||||
|
width = 800
|
||||||
|
height = 600
|
||||||
|
SIZE = (width, height)
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
YELLOW = (255, 255, 0)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
|
||||||
|
class Block:
|
||||||
|
def __init__(self, grid_pos, visibility):
|
||||||
|
self.grid_pos = grid_pos
|
||||||
|
self.visibility = visibility
|
||||||
|
def set_visible(self):
|
||||||
|
self.visibility = True
|
||||||
|
def unset_visible(self):
|
||||||
|
self.visibility = False
|
||||||
|
def draw(self,screen):
|
||||||
|
block_shape = (self.grid_pos[0], self.grid_pos[1], BLOCKSIZE, BLOCKSIZE)
|
||||||
|
pygame.draw.rect(screen, YELLOW, block_shape)
|
||||||
|
pygame.draw.rect(screen, RED, block_shape, 1)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
global blocks
|
||||||
|
blocks = {}
|
||||||
|
screen = init_game()
|
||||||
|
create_grid(screen)
|
||||||
|
game_loop(screen)
|
||||||
|
exit_game()
|
||||||
|
|
||||||
|
def init_game():
|
||||||
|
pygame.init()
|
||||||
|
screen = pygame.display.set_mode(SIZE)
|
||||||
|
return screen
|
||||||
|
|
||||||
|
def game_loop(screen):
|
||||||
|
while True:
|
||||||
|
if not event_handling():
|
||||||
|
break
|
||||||
|
if not update_game():
|
||||||
|
break
|
||||||
|
event = event_handling()
|
||||||
|
update_game()
|
||||||
|
draw_game(screen)
|
||||||
|
|
||||||
|
def event_handling():
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
if event.type == pygame.MOUSEBUTTONUP:
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
x = pos[0] // BLOCKSIZE * BLOCKSIZE
|
||||||
|
y = pos[1] // BLOCKSIZE * BLOCKSIZE
|
||||||
|
blocks[(x,y)].set_visible()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_LSHIFT:
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
x = pos[0] // BLOCKSIZE * BLOCKSIZE
|
||||||
|
y = pos[1] // BLOCKSIZE * BLOCKSIZE
|
||||||
|
blocks[(x,y)].unset_visible()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def update_game():
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_game(screen):
|
||||||
|
screen.fill(BLACK)
|
||||||
|
for x,y in blocks:
|
||||||
|
if blocks[(x,y)].visibility:
|
||||||
|
blocks[(x,y)].draw(screen)
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def create_grid(screen):
|
||||||
|
y = 0
|
||||||
|
for _ in range(SIZE[1]//BLOCKSIZE*BLOCKSIZE):
|
||||||
|
x = 0
|
||||||
|
for _ in range(SIZE[0]//BLOCKSIZE*BLOCKSIZE):
|
||||||
|
blocks[(x,y)]= (Block ((x, y), False))
|
||||||
|
x += BLOCKSIZE
|
||||||
|
y += BLOCKSIZE
|
||||||
|
|
||||||
|
def exit_game():
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
|
||||||
|
def init_game():
|
||||||
|
pygame.init()
|
||||||
|
screen = pygame.display.set_mode()
|
||||||
|
pygame.display.set_caption("Innit Game")
|
||||||
|
return screen
|
||||||
|
|
||||||
|
def game_loop(screen):
|
||||||
|
while True:
|
||||||
|
if not event_handling():
|
||||||
|
break
|
||||||
|
if not update_game():
|
||||||
|
break
|
||||||
|
draw_game(screen)
|
||||||
|
|
||||||
|
def exit_game():
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
def event_handling():
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_game():
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_game(screen):
|
||||||
|
screen.fill("BLACK")
|
||||||
|
pygame.display.flip()
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
|
||||||
|
class Particle:
|
||||||
|
def __init__(self,x,y):
|
||||||
|
self.pos = (x,y)
|
||||||
|
self.velocity = (0,0)
|
||||||
|
self.accel = (0,0)
|
||||||
|
self.surface = None
|
||||||
|
|
||||||
|
def set_surface(self,surface):
|
||||||
|
self.surface = surface
|
||||||
|
|
||||||
|
def appear(self,screen):
|
||||||
|
if self.surface is not None:
|
||||||
|
pygame.draw.circle(screen, BLUE, self.pos, CIRCLE_RADIUS)
|
||||||
|
blit_pos = (self.pos[0]-self.surface.get_width()/2, self.pos[1]-self.surface.get_height()/2)
|
||||||
|
screen.blit(self.surface, blit_pos)
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self.velocity = (self.velocity[0]+self.accel[0], self.velocity[1]+self.accel[1])
|
||||||
|
self.pos = (self.pos[0] + self.velocity[0], self.pos[1] + self.velocity[1])
|
||||||
|
self.accel = (0,0)
|
||||||
|
|
||||||
|
def apply_force(self,force):
|
||||||
|
self.accel = (self.accel[0]+force[0],self.accel[1]+force[1])
|
||||||
|
|
||||||
|
projectiles = {}
|
||||||
|
width = 640
|
||||||
|
height = 600
|
||||||
|
SIZE = (width, height)
|
||||||
|
FRICTION = 0.3
|
||||||
|
WHITE = (255, 255, 255)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
BLUE = (0, 0, 255)
|
||||||
|
CIRCLE_RADIUS = 2
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
|
||||||
|
def init_circle():
|
||||||
|
global particle
|
||||||
|
particle = Particle(width // 2, height // 2)
|
||||||
|
surface = pygame.Surface((CIRCLE_RADIUS * 2, CIRCLE_RADIUS * 2))
|
||||||
|
pygame.draw.circle(surface, RED, (CIRCLE_RADIUS, CIRCLE_RADIUS), CIRCLE_RADIUS)
|
||||||
|
particle.set_surface(surface.convert_alpha())
|
||||||
|
|
||||||
|
def main():
|
||||||
|
screen = init_game()
|
||||||
|
game_loop(screen)
|
||||||
|
exit_game()
|
||||||
|
|
||||||
|
def init_game():
|
||||||
|
global clock
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
pygame.init()
|
||||||
|
screen = pygame.display.set_mode(SIZE)
|
||||||
|
init_circle()
|
||||||
|
return screen
|
||||||
|
|
||||||
|
def game_loop(screen):
|
||||||
|
while True:
|
||||||
|
if not event_handling():
|
||||||
|
break
|
||||||
|
if not update_game():
|
||||||
|
break
|
||||||
|
event_handling()
|
||||||
|
update_game()
|
||||||
|
draw_screen(screen)
|
||||||
|
|
||||||
|
|
||||||
|
def event_handling():
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
keys_pressed = pygame.key.get_pressed()
|
||||||
|
if keys_pressed[pygame.K_UP]:
|
||||||
|
particle.apply_force((0,-1))
|
||||||
|
if keys_pressed[pygame.K_DOWN]:
|
||||||
|
particle.apply_force((0,1))
|
||||||
|
if keys_pressed[pygame.K_LEFT]:
|
||||||
|
particle.apply_force((-1,0))
|
||||||
|
if keys_pressed[pygame.K_RIGHT]:
|
||||||
|
particle.apply_force((1,0))
|
||||||
|
particle.apply_force((particle.velocity[0]*-FRICTION,particle.velocity[1]*-FRICTION))
|
||||||
|
particle.update()
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_game():
|
||||||
|
clock.tick(60)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_screen(screen):
|
||||||
|
screen.fill(BLACK)
|
||||||
|
particle.appear(screen)
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def exit_game():
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
|
||||||
|
class Walker:
|
||||||
|
def __init__(self,pos,size):
|
||||||
|
self.pos = pos
|
||||||
|
self.size = size
|
||||||
|
self.walk_direction = (0,0)
|
||||||
|
|
||||||
|
def grow(self):
|
||||||
|
self.size = self.size + 1
|
||||||
|
|
||||||
|
def controls(self):
|
||||||
|
keys_pressed = pygame.key.get_pressed()
|
||||||
|
if keys_pressed[pygame.K_UP]:
|
||||||
|
pos = (walker.pos[0], walker.pos[1] - 1)
|
||||||
|
self.pos = pos
|
||||||
|
self.walk_direction = (self.walk_direction[0],self.walk_direction[1]-1)
|
||||||
|
if keys_pressed[pygame.K_DOWN]:
|
||||||
|
pos = (walker.pos[0], walker.pos[1] + 1)
|
||||||
|
self.pos = pos
|
||||||
|
self.walk_direction = (self.walk_direction[0], self.walk_direction[1] - 1)
|
||||||
|
if keys_pressed[pygame.K_LEFT]:
|
||||||
|
pos = (walker.pos[0] - 1, walker.pos[1])
|
||||||
|
self.pos = pos
|
||||||
|
self.walk_direction = (self.walk_direction[0], self.walk_direction[1] - 1)
|
||||||
|
if keys_pressed[pygame.K_RIGHT]:
|
||||||
|
pos = (walker.pos[0] + 1, walker.pos[1])
|
||||||
|
self.pos = pos
|
||||||
|
self.walk_direction = (self.walk_direction[0], self.walk_direction[1] - 1)
|
||||||
|
if keys_pressed[pygame.K_SPACE]:
|
||||||
|
x=0
|
||||||
|
projectiles[x] = Particle(walker.pos[0],walker.pos[1])
|
||||||
|
projectiles[x].apply_force(self.walk_direction)
|
||||||
|
x+=1
|
||||||
|
|
||||||
|
def appearance(self,screen):
|
||||||
|
pygame.draw.circle(screen, RED, self.pos, self.size)
|
||||||
|
|
||||||
|
class Particle:
|
||||||
|
def __init__(self,x,y):
|
||||||
|
self.pos = (x,y)
|
||||||
|
self.velocity = (0,0)
|
||||||
|
self.accel = (0,0)
|
||||||
|
self.surface = None
|
||||||
|
|
||||||
|
def set_surface(self,surface):
|
||||||
|
self.surface = surface
|
||||||
|
|
||||||
|
def appear(self,screen):
|
||||||
|
if self.surface is not None:
|
||||||
|
pygame.draw.circle(screen, BLUE, self.pos, CIRCLE_RADIUS)
|
||||||
|
blit_pos = (self.pos[0]-self.surface.get_width()/2, self.pos[1]-self.surface.get_height()/2)
|
||||||
|
screen.blit(self.surface, blit_pos)
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self.velocity = (self.velocity[0]+self.accel[0], self.velocity[1]+self.accel[1])
|
||||||
|
self.pos = (self.pos[0] + self.velocity[0], self.pos[1] + self.velocity[1])
|
||||||
|
self.accel = (0,0)
|
||||||
|
|
||||||
|
def apply_force(self,force):
|
||||||
|
self.accel = (self.accel[0]+force[0],self.accel[1]+force[1])
|
||||||
|
|
||||||
|
projectiles = {}
|
||||||
|
width = 640
|
||||||
|
height = 600
|
||||||
|
SIZE = (width, height)
|
||||||
|
FRICTION = 0.3
|
||||||
|
WHITE = (255, 255, 255)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
BLUE = (0, 0, 255)
|
||||||
|
CIRCLE_RADIUS = 2
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
|
||||||
|
def init_circle():
|
||||||
|
global particle
|
||||||
|
particle = Particle(width // 2, height // 2)
|
||||||
|
surface = pygame.Surface((CIRCLE_RADIUS * 2, CIRCLE_RADIUS * 2))
|
||||||
|
pygame.draw.circle(surface, RED, (CIRCLE_RADIUS, CIRCLE_RADIUS), CIRCLE_RADIUS)
|
||||||
|
particle.set_surface(surface.convert_alpha())
|
||||||
|
|
||||||
|
def main():
|
||||||
|
screen = init_game()
|
||||||
|
game_loop(screen)
|
||||||
|
exit_game()
|
||||||
|
|
||||||
|
def init_game():
|
||||||
|
global clock
|
||||||
|
global walker
|
||||||
|
walker = Walker((300,300),10)
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
pygame.init()
|
||||||
|
screen = pygame.display.set_mode(SIZE)
|
||||||
|
init_circle()
|
||||||
|
return screen
|
||||||
|
|
||||||
|
def game_loop(screen):
|
||||||
|
while True:
|
||||||
|
if not event_handling():
|
||||||
|
break
|
||||||
|
if not update_game():
|
||||||
|
break
|
||||||
|
event_handling()
|
||||||
|
update_game()
|
||||||
|
draw_screen(screen)
|
||||||
|
|
||||||
|
def event_handling():
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_game():
|
||||||
|
walker.controls()
|
||||||
|
for x in projectiles:
|
||||||
|
projectiles[x].update()
|
||||||
|
clock.tick(60)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_screen(screen):
|
||||||
|
screen.fill(BLACK)
|
||||||
|
walker.appearance(screen)
|
||||||
|
for x in projectiles:
|
||||||
|
projectiles[x].appear(screen)
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def exit_game():
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
BLOCKSIZE = 200
|
||||||
|
width = 600
|
||||||
|
height = 600
|
||||||
|
SIZE = (width, height)
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
WHITE = (255, 255, 255)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
|
||||||
|
class Block:
|
||||||
|
def __init__(self, grid_pos, visibility):
|
||||||
|
self.grid_pos = grid_pos
|
||||||
|
self.visibility = visibility
|
||||||
|
def draw(self,screen):
|
||||||
|
block_shape = (self.grid_pos[0], self.grid_pos[1], BLOCKSIZE, BLOCKSIZE)
|
||||||
|
pygame.draw.rect(screen, BLACK, block_shape)
|
||||||
|
pygame.draw.rect(screen, RED, block_shape, 1)
|
||||||
|
|
||||||
|
class Circle:
|
||||||
|
def __init__(self, grid_pos, visibility):
|
||||||
|
self.grid_pos = grid_pos
|
||||||
|
self.visibility = visibility
|
||||||
|
def set_visible(self):
|
||||||
|
self.visibility = True
|
||||||
|
def draw(self,screen):
|
||||||
|
radius = BLOCKSIZE // 2
|
||||||
|
center = (self.grid_pos[0]-radius, self.grid_pos[1]-radius)
|
||||||
|
pygame.draw.circle(screen, WHITE, center, radius)
|
||||||
|
pygame.draw.circle(screen, BLACK, center, radius-10)
|
||||||
|
|
||||||
|
class Cross:
|
||||||
|
def __init__(self, grid_pos, visibility):
|
||||||
|
self.grid_pos = grid_pos
|
||||||
|
self.visibility = visibility
|
||||||
|
def set_visible(self):
|
||||||
|
self.visibility = True
|
||||||
|
def draw(self,screen):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
screen = init_game()
|
||||||
|
create_grid(screen)
|
||||||
|
game_loop(screen)
|
||||||
|
exit_game()
|
||||||
|
|
||||||
|
def init_game():
|
||||||
|
global circles
|
||||||
|
global blocks
|
||||||
|
global crosses
|
||||||
|
crosses ={}
|
||||||
|
circles = {}
|
||||||
|
blocks = {}
|
||||||
|
pygame.init()
|
||||||
|
screen = pygame.display.set_mode(SIZE)
|
||||||
|
return screen
|
||||||
|
|
||||||
|
def game_loop(screen):
|
||||||
|
while True:
|
||||||
|
if not event_handling():
|
||||||
|
break
|
||||||
|
if not update_game():
|
||||||
|
break
|
||||||
|
event = event_handling()
|
||||||
|
update_game()
|
||||||
|
draw_game(screen)
|
||||||
|
|
||||||
|
def event_handling():
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
x = pos[0] // BLOCKSIZE * BLOCKSIZE
|
||||||
|
y = pos[1] // BLOCKSIZE * BLOCKSIZE
|
||||||
|
circles[(x, y)].set_visible()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def update_game():
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_game(screen):
|
||||||
|
screen.fill(BLACK)
|
||||||
|
|
||||||
|
for x,y in blocks:
|
||||||
|
if blocks[(x,y)].visibility:
|
||||||
|
blocks[(x,y)].draw(screen)
|
||||||
|
if circles[(x,y)].visibility:
|
||||||
|
circles[(x,y)].draw(screen)
|
||||||
|
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def create_grid(screen):
|
||||||
|
y = 0
|
||||||
|
for _ in range(3):
|
||||||
|
x = 0
|
||||||
|
for _ in range(3):
|
||||||
|
blocks[(x,y)]= (Block ((x, y), True))
|
||||||
|
circles[(x, y)] = (Circle((x, y), False))
|
||||||
|
x += BLOCKSIZE
|
||||||
|
y += BLOCKSIZE
|
||||||
|
|
||||||
|
def exit_game():
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
def calendar_check(i):
|
||||||
|
if i % 2 == 0:
|
||||||
|
print("Even")
|
||||||
|
else:
|
||||||
|
print("Odd")
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
def countdown(lim):
|
||||||
|
i = lim
|
||||||
|
for x in range(lim+1):
|
||||||
|
print(i)
|
||||||
|
i = i-1
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
countdown(10)
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
def fahrenheit_to_celsius(f):
|
||||||
|
c = (5.0 / 9.0) * (f - 32)
|
||||||
|
print(f"current temperature: {c:.1f}")
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
def gamble():
|
||||||
|
counter = 0
|
||||||
|
print("in this game you have to guess a random number between 1 and 100")
|
||||||
|
target = int(random.randint(1,100))
|
||||||
|
while True:
|
||||||
|
guess = int(input("Take a guess! : "))
|
||||||
|
if target == guess:
|
||||||
|
print("You won!")
|
||||||
|
print("guesses taken: ",counter)
|
||||||
|
break
|
||||||
|
if target > guess:
|
||||||
|
print("too low")
|
||||||
|
if target < guess:
|
||||||
|
print("too high")
|
||||||
|
counter = counter+1
|
||||||
|
|
||||||
|
gamble()
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
|
||||||
|
|
||||||
|
def sequence_length():
|
||||||
|
sequence = input("Enter a sequence: ")
|
||||||
|
counter = 0
|
||||||
|
for sequence in sequence:
|
||||||
|
counter = counter+1
|
||||||
|
print("sequence length: ",counter)
|
||||||
|
|
||||||
|
def most_common_letter():
|
||||||
|
sequence = input("Enter a word: ")
|
||||||
|
letter = max(set(sequence), key=sequence.count)
|
||||||
|
print(f"The most common letter is '{letter}' and can be found {sequence.count(letter)} times")
|
||||||
|
|
||||||
|
def guarded_sum_loop():
|
||||||
|
print("Add numbers. Inputting '0' gives you the result")
|
||||||
|
result = 0
|
||||||
|
while True:
|
||||||
|
num = float(input("Enter a number: "))
|
||||||
|
if num == 0:
|
||||||
|
break
|
||||||
|
result = result + num
|
||||||
|
if result < 0:
|
||||||
|
print("NEGATIVE BALANCE")
|
||||||
|
break
|
||||||
|
print("Current balance: ", result)
|
||||||
|
|
||||||
|
def quadratic_calc():
|
||||||
|
MAX_NUM = 10
|
||||||
|
i = 0
|
||||||
|
result = 0
|
||||||
|
while i < MAX_NUM:
|
||||||
|
result = result + (i * i)
|
||||||
|
i = i + 1
|
||||||
|
print("quadratic sum between 0 and 10: ",result)
|
||||||
|
|
||||||
|
def time_passed():
|
||||||
|
sec = int(input("how many seconds passed? "))
|
||||||
|
min = sec // 60
|
||||||
|
sec = sec % 60
|
||||||
|
h = min // 60
|
||||||
|
min = min % 60
|
||||||
|
print(f"{h:02}:{min:02}:{sec:02}")
|
||||||
|
|
||||||
|
def list_sum():
|
||||||
|
numbers = [1,2,3,4,5,6,7]
|
||||||
|
result = 0
|
||||||
|
for x in numbers:
|
||||||
|
result = result + x
|
||||||
|
print(result)
|
||||||
|
|
||||||
|
def letter_counter():
|
||||||
|
text = input("Enter a text: ")
|
||||||
|
tar = input("Enter a letter: ")
|
||||||
|
i = 0
|
||||||
|
counter = 0
|
||||||
|
for text in text:
|
||||||
|
if text[i] == tar:
|
||||||
|
counter = counter + 1
|
||||||
|
print(f"This text contains the letter '{tar}' {counter} times")
|
||||||
|
|
||||||
|
def two_sum(lst, target):
|
||||||
|
o = len(lst)-1
|
||||||
|
counter = 0
|
||||||
|
for x in lst:
|
||||||
|
i = 0
|
||||||
|
while i < o:
|
||||||
|
checker = lst[i] + lst[o]
|
||||||
|
if target == checker:
|
||||||
|
print(f"combination found: {lst[i]} + {lst[o]} = {checker}")
|
||||||
|
#return lst[i], lst[o]
|
||||||
|
counter += 1
|
||||||
|
i = i + 1
|
||||||
|
o = o - 1
|
||||||
|
if counter == 0:
|
||||||
|
print("No combinations found")
|
||||||
|
#return None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
two_sum([1,2,3,4,5,6,7,8,9,10,-1,-2],5)
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
def money_calc(money):
|
||||||
|
euros = money // 100
|
||||||
|
cents = money % 100
|
||||||
|
print(f"{euros}.{cents:02}")
|
||||||
|
|
||||||
|
def more_money(cents):
|
||||||
|
eur1 = cents // 100
|
||||||
|
cents = cents % 100
|
||||||
|
eur2 = eur1 // 1000
|
||||||
|
eur1 = eur1 % 1000
|
||||||
|
eur3 = eur2 // 1000
|
||||||
|
eur2 = eur2 % 1000
|
||||||
|
if eur3 > 0:
|
||||||
|
print(f'{eur3}.{eur2:03}.{eur1:03},{cents:02}')
|
||||||
|
elif eur2 > 0:
|
||||||
|
print( f'{eur2}.{eur1:03},{cents:02}')
|
||||||
|
else:
|
||||||
|
print(f'{eur1},{cents:02}')
|
||||||
|
|
||||||
|
more_money(3600780000)
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
def m_narrator():
|
||||||
|
print("After days of drifting in darkness, you finally reach your destination - the moon")
|
||||||
|
time.sleep(5)
|
||||||
|
print(
|
||||||
|
"The onboard computer is about to finish the last few calculations to land you on the surface safely, as it suddenly stops")
|
||||||
|
print()
|
||||||
|
time.sleep(7)
|
||||||
|
print("ERROR: UNEXPECTED VARIABLES DETECTED")
|
||||||
|
time.sleep(2)
|
||||||
|
print("PROCEED WITH MANUAL LANDING")
|
||||||
|
print()
|
||||||
|
time.sleep(2)
|
||||||
|
print("Lucky that the sensors are still functioning")
|
||||||
|
time.sleep(2)
|
||||||
|
print("________________________")
|
||||||
|
print("ALTITUDE: 50.000m")
|
||||||
|
print("DESCENT SPEED: -1000m/s")
|
||||||
|
print("FUEL: 10.000l")
|
||||||
|
print("________________________")
|
||||||
|
time.sleep(2)
|
||||||
|
print("MOON ACCELERATION: -1,63m/s²")
|
||||||
|
print("ENGINE ACCELERATION: 12m/s²")
|
||||||
|
print("________________________")
|
||||||
|
time.sleep(3)
|
||||||
|
print()
|
||||||
|
print("As the gravity pulls you to your demise, the time to decide comes")
|
||||||
|
print()
|
||||||
|
|
||||||
|
def moon_landing():
|
||||||
|
#m_narrator()
|
||||||
|
fuel = 10000
|
||||||
|
altitude = 50000
|
||||||
|
speed = -1000
|
||||||
|
m_ac = -1.63
|
||||||
|
e_ac = 12
|
||||||
|
t = 1
|
||||||
|
|
||||||
|
while True:
|
||||||
|
if altitude <= 0:
|
||||||
|
if speed > -10:
|
||||||
|
print("You landed successfully")
|
||||||
|
else :
|
||||||
|
print("As your ship descended violently, you regretted not paying attention in physics class")
|
||||||
|
break
|
||||||
|
if input(f"Will you spend {100}l fuel to decelerate? (y/n): ") == "y":
|
||||||
|
print()
|
||||||
|
print("You turn the engines on for a while")
|
||||||
|
fuel = fuel - (100)
|
||||||
|
speed = speed+(e_ac*1)
|
||||||
|
altitude = altitude + speed
|
||||||
|
else :
|
||||||
|
print("you do nothing")
|
||||||
|
speed = speed+(m_ac*1)
|
||||||
|
altitude = altitude + speed
|
||||||
|
|
||||||
|
print("________________________")
|
||||||
|
print("ALTITUDE: ",altitude)
|
||||||
|
print("DESCENT SPEED: ",speed)
|
||||||
|
print("FUEL: ", fuel)
|
||||||
|
print("________________________")
|
||||||
|
print()
|
||||||
|
|
||||||
|
m_narrator()
|
||||||
|
moon_landing()
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"]
|
||||||
|
suits = ["of Clubs", "of Diamonds", "of Hearts", "of Spades"]
|
||||||
|
player_hand = []
|
||||||
|
dealer_hand = []
|
||||||
|
deck = []
|
||||||
|
|
||||||
|
class Player:
|
||||||
|
def draw(self):
|
||||||
|
card = random.choice(deck)
|
||||||
|
deck.remove(card)
|
||||||
|
player_hand.append(card)
|
||||||
|
return card
|
||||||
|
|
||||||
|
def hand_value(self):
|
||||||
|
total = 0
|
||||||
|
for _ in player_hand:
|
||||||
|
total += get_value(_)
|
||||||
|
return total
|
||||||
|
|
||||||
|
class Dealer:
|
||||||
|
def draw(self):
|
||||||
|
card = random.choice(deck)
|
||||||
|
deck.remove(card)
|
||||||
|
dealer_hand.append(card)
|
||||||
|
return card
|
||||||
|
|
||||||
|
def hand_value(self):
|
||||||
|
total = 0
|
||||||
|
for _ in dealer_hand:
|
||||||
|
total += get_value(_)
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
def create_deck():
|
||||||
|
counter = 0
|
||||||
|
x = 0
|
||||||
|
for _ in range(len(suits)):
|
||||||
|
y = 0
|
||||||
|
for _ in range(len(ranks)):
|
||||||
|
deck.append(f"{ranks[y]} {suits[x]}")
|
||||||
|
y += 1
|
||||||
|
counter += 1
|
||||||
|
x += 1
|
||||||
|
return deck
|
||||||
|
|
||||||
|
def get_value(card):
|
||||||
|
if "2" in card:
|
||||||
|
return 2
|
||||||
|
if "3" in card:
|
||||||
|
return 3
|
||||||
|
if "4" in card:
|
||||||
|
return 4
|
||||||
|
if "5" in card:
|
||||||
|
return 5
|
||||||
|
if "6" in card:
|
||||||
|
return 6
|
||||||
|
if "7" in card:
|
||||||
|
return 7
|
||||||
|
if "8" in card:
|
||||||
|
return 8
|
||||||
|
if "9" in card:
|
||||||
|
return 9
|
||||||
|
if "10" in card:
|
||||||
|
return 10
|
||||||
|
if "Jack" in card or "Queen" in card or "King" in card:
|
||||||
|
return 10
|
||||||
|
if "Ace" in card:
|
||||||
|
return 11
|
||||||
|
return None
|
||||||
|
|
||||||
|
def main():
|
||||||
|
bank = 1000
|
||||||
|
while True:
|
||||||
|
global player
|
||||||
|
global dealer
|
||||||
|
player = Player()
|
||||||
|
dealer = Dealer()
|
||||||
|
create_deck()
|
||||||
|
p_stand = False
|
||||||
|
d_stand = False
|
||||||
|
for _ in range(2):
|
||||||
|
player.draw()
|
||||||
|
dealer.draw()
|
||||||
|
print(f"your current balance: {bank}")
|
||||||
|
bet = int(input("Enter your bet: "))
|
||||||
|
bank -= bet
|
||||||
|
|
||||||
|
while True:
|
||||||
|
print(f"Your hand: {player_hand} value: {player.hand_value()}")
|
||||||
|
print()
|
||||||
|
print(f"dealer card: {dealer_hand} value: {dealer.hand_value()}")
|
||||||
|
print()
|
||||||
|
|
||||||
|
if not p_stand:
|
||||||
|
if input("hit or stand (h/s): ") == "h":
|
||||||
|
print(f"you draw a card: {player.draw()}")
|
||||||
|
print()
|
||||||
|
else:
|
||||||
|
p_stand = True
|
||||||
|
|
||||||
|
if not d_stand:
|
||||||
|
if dealer.hand_value() < 17:
|
||||||
|
dealer.draw()
|
||||||
|
print(f"dealer draws a card: {dealer.draw()}")
|
||||||
|
print()
|
||||||
|
else:
|
||||||
|
d_stand = True
|
||||||
|
|
||||||
|
if player.hand_value() > 21:
|
||||||
|
print("you loose")
|
||||||
|
player_hand.clear()
|
||||||
|
dealer_hand.clear()
|
||||||
|
break
|
||||||
|
if player.hand_value() == 21:
|
||||||
|
print(f"you win ({bet * 2})")
|
||||||
|
bank = bank + bet * 2
|
||||||
|
player_hand.clear()
|
||||||
|
dealer_hand.clear()
|
||||||
|
deck.clear()
|
||||||
|
break
|
||||||
|
|
||||||
|
if bank == 0:
|
||||||
|
print("YOU HAVE NO MONEY")
|
||||||
|
break
|
||||||
|
if input("Do you want to play again? (y/n): ") == "n":
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
ranks = [2,3,4,5,6,7,8,9,10,"Jack","Queen","King","Ace"]
|
||||||
|
suits = ["of Clubs", "of Diamonds", "of Hearts", "of Spades"]
|
||||||
|
deck = []
|
||||||
|
counter = 0
|
||||||
|
y = 0
|
||||||
|
x = 0
|
||||||
|
for _ in range (len(suits)):
|
||||||
|
y = 0
|
||||||
|
for _ in range (len(ranks)):
|
||||||
|
deck.append(f"{ranks[y]} {suits[x]}")
|
||||||
|
y += 1
|
||||||
|
counter +=1
|
||||||
|
x +=1
|
||||||
|
|
||||||
|
print(deck[random.randint(0,len(deck)-1)])
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
class Student:
|
||||||
|
def __init__(self,name,s_id):
|
||||||
|
self.name = name
|
||||||
|
self.s_id = s_id
|
||||||
|
def __eq__(self,other):
|
||||||
|
if self.name == other.name and self.s_id == other.s_id:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
s1=Student("A",1)
|
||||||
|
s2=Student("A",1)
|
||||||
|
if s1 == s2:
|
||||||
|
print("equal")
|
||||||
|
else:
|
||||||
|
print("not equal")
|
||||||
Whitespace-only changes.
@@ -0,0 +1,41 @@
|
|||||||
|
|
||||||
|
word = {}
|
||||||
|
|
||||||
|
def train():
|
||||||
|
with (open("vocab.txt", "r+")) as vocab:
|
||||||
|
for line in vocab:
|
||||||
|
word[line] = vocab.readline()
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
if input(f"what is the translation of '{word[0]}'? ") in word[1]:
|
||||||
|
print("correct!")
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
print("incorrect, try again")
|
||||||
|
|
||||||
|
except KeyError:
|
||||||
|
print("no such word in the library!")
|
||||||
|
print()
|
||||||
|
|
||||||
|
def save_vocab():
|
||||||
|
ger = input("enter a german word: ")
|
||||||
|
en = input("enter an english translation: ")
|
||||||
|
|
||||||
|
with (open("vocab.txt", "w")) as vocab:
|
||||||
|
vocab.write(f"{ger}, {en}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while True:
|
||||||
|
print("1. Add a new word")
|
||||||
|
print("2. Train")
|
||||||
|
print("3. Quit")
|
||||||
|
|
||||||
|
user_inp = input()
|
||||||
|
if user_inp == "1":
|
||||||
|
save_vocab()
|
||||||
|
if user_inp == "2":
|
||||||
|
train()
|
||||||
|
if user_inp == "3":
|
||||||
|
break
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
|
||||||
|
class Animal:
|
||||||
|
def __init__(self,name,species,age):
|
||||||
|
self.name = name
|
||||||
|
self.species = species
|
||||||
|
self.age = age
|
||||||
|
def make_sound(self):
|
||||||
|
print()
|
||||||
|
|
||||||
|
class Bird(Animal):
|
||||||
|
def __str__(self):
|
||||||
|
return f"({self.species},{self.age})"
|
||||||
|
|
||||||
|
def make_sound(self):
|
||||||
|
print("chirp chirp")
|
||||||
|
|
||||||
|
class Mammal(Animal):
|
||||||
|
def __str__(self):
|
||||||
|
return f"({self.species},{self.age})"
|
||||||
|
def make_sound(self):
|
||||||
|
print("roar")
|
||||||
|
|
||||||
|
def main():
|
||||||
|
animals = {}
|
||||||
|
while True:
|
||||||
|
print("1. Add new mammal")
|
||||||
|
print("2. Add new bird")
|
||||||
|
print("3. List all animals")
|
||||||
|
print("4. Animal concert")
|
||||||
|
print("5. exit")
|
||||||
|
|
||||||
|
user_input = int(input("(1/2/3/4)?: "))
|
||||||
|
if user_input == 1:
|
||||||
|
age = input("Age: ")
|
||||||
|
species = input("Species: ")
|
||||||
|
name = input("Name: ")
|
||||||
|
animals[name] = str(Bird(name, species, age))
|
||||||
|
print()
|
||||||
|
|
||||||
|
if user_input == 2:
|
||||||
|
age = input("Age: ")
|
||||||
|
species = input("Species: ")
|
||||||
|
name = input("Name: ")
|
||||||
|
animals[name] = str(Mammal(name, species, age))
|
||||||
|
print()
|
||||||
|
|
||||||
|
if user_input == 3:
|
||||||
|
print(animals)
|
||||||
|
print()
|
||||||
|
|
||||||
|
if user_input == 4:
|
||||||
|
for animal_object in animals.values():
|
||||||
|
animal_object.make_sound()
|
||||||
|
|
||||||
|
if user_input == 5:
|
||||||
|
break
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,183 @@
|
|||||||
|
from turtle import Turtle
|
||||||
|
|
||||||
|
|
||||||
|
def broken_line(length, pause, lines):
|
||||||
|
for x in range(lines):
|
||||||
|
sophia.forward(length)
|
||||||
|
sophia.penup()
|
||||||
|
sophia.forward(pause)
|
||||||
|
sophia.pendown()
|
||||||
|
|
||||||
|
def circles(y):
|
||||||
|
for x in range(150):
|
||||||
|
sophia.circle(y)
|
||||||
|
y = y+5
|
||||||
|
sophia.right(10)
|
||||||
|
|
||||||
|
def draw_square(t,length,width,color):
|
||||||
|
t.color(color)
|
||||||
|
for x in range(2):
|
||||||
|
t.forward(width)
|
||||||
|
t.left(90)
|
||||||
|
t.forward(length)
|
||||||
|
t.left(90)
|
||||||
|
|
||||||
|
def filled_square(t,length,width,color):
|
||||||
|
t.fillcolor(color)
|
||||||
|
t.color(color)
|
||||||
|
t.begin_fill()
|
||||||
|
for x in range(2):
|
||||||
|
t.forward(width)
|
||||||
|
t.left(90)
|
||||||
|
t.forward(length)
|
||||||
|
t.left(90)
|
||||||
|
t.end_fill()
|
||||||
|
|
||||||
|
def filled_circle(t,y,color):
|
||||||
|
t.fillcolor(color)
|
||||||
|
t.color(color)
|
||||||
|
t.begin_fill()
|
||||||
|
t.circle(y)
|
||||||
|
t.end_fill()
|
||||||
|
|
||||||
|
def draw_mandala(t,y,color):
|
||||||
|
for x in range(18):
|
||||||
|
draw_square(t,y,color)
|
||||||
|
t.left(20)
|
||||||
|
|
||||||
|
def draw_triangle(t,y):
|
||||||
|
for x in range(3):
|
||||||
|
t.left(120)
|
||||||
|
t.forward(y)
|
||||||
|
|
||||||
|
def draw_target(t,y):
|
||||||
|
r = y
|
||||||
|
for x in range(2):
|
||||||
|
filled_circle(sophia, y,"red")
|
||||||
|
y = y - 30
|
||||||
|
t.left(90)
|
||||||
|
t.forward(r-y)
|
||||||
|
r = r - 30
|
||||||
|
print("r=",r)
|
||||||
|
print("y=",y)
|
||||||
|
t.left(270)
|
||||||
|
filled_circle(sophia, y,"white")
|
||||||
|
y = y - 30
|
||||||
|
t.left(90)
|
||||||
|
t.forward(r-y)
|
||||||
|
r = r - 30
|
||||||
|
print("r=",r)
|
||||||
|
print("y=",y)
|
||||||
|
t.left(270)
|
||||||
|
filled_circle(sophia, y,"red")
|
||||||
|
|
||||||
|
def ampel(t,y):
|
||||||
|
t.backward(70/2)
|
||||||
|
length = y
|
||||||
|
filled_square(t,length,70,"black")
|
||||||
|
t.penup()
|
||||||
|
t.forward(70/2)
|
||||||
|
t.left(90)
|
||||||
|
t.forward(6)
|
||||||
|
t.right(90)
|
||||||
|
filled_circle(t, 25, "green")
|
||||||
|
t.left(90)
|
||||||
|
t.forward(y / 3)
|
||||||
|
t.right(90)
|
||||||
|
filled_circle(t, 25, "yellow")
|
||||||
|
t.left(90)
|
||||||
|
t.forward(y / 3)
|
||||||
|
t.right(90)
|
||||||
|
filled_circle(t, 25, "red")
|
||||||
|
|
||||||
|
def skyscraper(t):
|
||||||
|
t.penup()
|
||||||
|
for x in range(5):
|
||||||
|
t.backward(100)
|
||||||
|
t.left(90)
|
||||||
|
t.forward(30)
|
||||||
|
t.right(90)
|
||||||
|
filled_square(t,42,100,"black")
|
||||||
|
t.forward(10)
|
||||||
|
t.left(90)
|
||||||
|
t.forward(10)
|
||||||
|
t.right(90)
|
||||||
|
for x in range(3):
|
||||||
|
filled_square(t,30,20,"yellow")
|
||||||
|
t.forward(30)
|
||||||
|
t.backward(100)
|
||||||
|
t.left(90)
|
||||||
|
t.forward(30)
|
||||||
|
t.right(90)
|
||||||
|
filled_square(t,8,100,"black")
|
||||||
|
|
||||||
|
def draw_flag(t,color1,color2,color3):
|
||||||
|
t.screen.bgcolor("black")
|
||||||
|
filled_square(t,120,60, color1)
|
||||||
|
t.forward(60)
|
||||||
|
filled_square(t, 120, 60, color2)
|
||||||
|
t.forward(60)
|
||||||
|
filled_square(t, 120, 60, color3)
|
||||||
|
|
||||||
|
def draw_segment(t,r,a,color):
|
||||||
|
t.color(color)
|
||||||
|
t.fillcolor(color)
|
||||||
|
t.begin_fill()
|
||||||
|
t.circle(r,a)
|
||||||
|
t.left(90)
|
||||||
|
t.forward(r)
|
||||||
|
t.end_fill()
|
||||||
|
t.backward(r)
|
||||||
|
t.right(90)
|
||||||
|
|
||||||
|
def pie_chart(t,r):
|
||||||
|
a = 240
|
||||||
|
draw_segment(t,r,a,"red")
|
||||||
|
a = 90
|
||||||
|
draw_segment(t, r, a, "blue")
|
||||||
|
a = 30
|
||||||
|
draw_segment(t, r, a, "green")
|
||||||
|
|
||||||
|
def yin(r):
|
||||||
|
sophia.begin_fill()
|
||||||
|
sophia.circle(r/2, -180)
|
||||||
|
sophia.end_fill()
|
||||||
|
sophia.left(90)
|
||||||
|
sophia.forward(r / 2.5)
|
||||||
|
sophia.right(90)
|
||||||
|
filled_circle(sophia, r / 10, "black")
|
||||||
|
|
||||||
|
def yang(r):
|
||||||
|
zoe.penup()
|
||||||
|
zoe.circle(r, -180)
|
||||||
|
zoe.pendown()
|
||||||
|
zoe.begin_fill()
|
||||||
|
zoe.circle(r / 2, -180)
|
||||||
|
zoe.end_fill()
|
||||||
|
zoe.left(90)
|
||||||
|
zoe.forward(r/2.5)
|
||||||
|
zoe.right(90)
|
||||||
|
filled_circle(zoe,r/10,"white")
|
||||||
|
|
||||||
|
def yin_yang(r):
|
||||||
|
sophia.screen.bgcolor("grey")
|
||||||
|
sophia.color("white")
|
||||||
|
sophia.fillcolor("white")
|
||||||
|
zoe.fillcolor("black")
|
||||||
|
zoe.color("black")
|
||||||
|
sophia.begin_fill()
|
||||||
|
sophia.circle(r, -180)
|
||||||
|
sophia.end_fill()
|
||||||
|
zoe.begin_fill()
|
||||||
|
zoe.circle(r, 180)
|
||||||
|
zoe.end_fill()
|
||||||
|
yang(r)
|
||||||
|
yin(r)
|
||||||
|
|
||||||
|
Turtle.target = draw_target
|
||||||
|
sophia = Turtle()
|
||||||
|
sophia.hideturtle()
|
||||||
|
sophia.speed(0)
|
||||||
|
zoe = Turtle()
|
||||||
|
zoe.hideturtle()
|
||||||
|
zoe.speed(0)
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
from turtle import Turtle
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
sophia = Turtle()
|
||||||
|
|
||||||
|
draw_circles(sophia, 100)
|
||||||
|
sophia.screen.mainloop()
|
||||||
|
|
||||||
|
def draw_circles(sophia, radius):
|
||||||
|
for _ in range(3):
|
||||||
|
sophia.circle(radius)
|
||||||
|
sophia.penup()
|
||||||
|
sophia.forward(radius)
|
||||||
|
sophia.pendown()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
import tkinter as tk
|
||||||
|
|
||||||
|
from my_package.App_window import AppWindow
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid_isbn(entry):
|
||||||
|
clean_isbn = entry.replace("-", "").replace(" ", "").upper()
|
||||||
|
|
||||||
|
if len(clean_isbn) != 10:
|
||||||
|
return False
|
||||||
|
|
||||||
|
total = 0
|
||||||
|
try:
|
||||||
|
for i in range(10):
|
||||||
|
char = clean_isbn[i]
|
||||||
|
if i == 9 and char == 'X':
|
||||||
|
value = 10
|
||||||
|
elif char.isdigit():
|
||||||
|
value = int(char)
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
total += value * (10 - i)
|
||||||
|
|
||||||
|
return total % 11 == 0
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid_iban(entry):
|
||||||
|
clean_iban = entry.replace(" ", "").upper()
|
||||||
|
|
||||||
|
rearranged = clean_iban[4:] + clean_iban[:4]
|
||||||
|
|
||||||
|
numeric_iban = ""
|
||||||
|
for char in rearranged:
|
||||||
|
if char.isdigit():
|
||||||
|
numeric_iban += char
|
||||||
|
else:
|
||||||
|
numeric_iban += str(ord(char) - 55)
|
||||||
|
|
||||||
|
return int(numeric_iban) % 97 == 1
|
||||||
|
|
||||||
|
|
||||||
|
class Checker(AppWindow):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(300,300)
|
||||||
|
self.button_1 = tk.Button(self.root, text="check", command= self.check_isbn)
|
||||||
|
self.button_2 = tk.Button(self.root,text="check", command= self.check_iban)
|
||||||
|
self.isbn_label = tk.Label(self.root, text="")
|
||||||
|
self.iban_label = tk.Label(self.root, text="")
|
||||||
|
self.entry_1 = tk.Entry(self.root, width=20)
|
||||||
|
self.entry_2 = tk.Entry(self.root, width=20)
|
||||||
|
|
||||||
|
|
||||||
|
def init_widgets(self):
|
||||||
|
tk.Label(self.root, text="ISBN: ").grid(row=0, column=0)
|
||||||
|
self.entry_1.grid(row=0, column=1)
|
||||||
|
self.button_1.grid(row=1, column=0)
|
||||||
|
self.isbn_label.grid(row=1, column=1)
|
||||||
|
|
||||||
|
tk.Label(self.root, text="IBAN: ").grid(row=2, column=0)
|
||||||
|
self.entry_2.grid(row=2, column=1)
|
||||||
|
self.button_2.grid(row=3, column=0)
|
||||||
|
self.iban_label.grid(row=3, column=1)
|
||||||
|
|
||||||
|
def check_isbn(self):
|
||||||
|
entry = self.entry_1.get()
|
||||||
|
self.entry_1.delete(0, tk.END)
|
||||||
|
if is_valid_isbn(entry):
|
||||||
|
self.isbn_label.configure(text="Valid ISBN")
|
||||||
|
else:
|
||||||
|
self.isbn_label.configure(text="Invalid ISBN")
|
||||||
|
|
||||||
|
def check_iban(self):
|
||||||
|
entry = self.entry_2.get()
|
||||||
|
self.entry_2.delete(0, tk.END)
|
||||||
|
if is_valid_iban(entry):
|
||||||
|
self.iban_label.configure(text="Valid IBAN")
|
||||||
|
else:
|
||||||
|
self.iban_label.configure(text="Invalid IBAN")
|
||||||
|
|
||||||
|
def console_checker():
|
||||||
|
print("Welcome to IBAN / ISBN Checker")
|
||||||
|
while True:
|
||||||
|
usr_input = input("Please enter your Number: ")
|
||||||
|
print("1. Check ISBN")
|
||||||
|
print("2. Check IBAN")
|
||||||
|
print("3. Exit")
|
||||||
|
usr_choice = input("1 / 2 / 3 ?: ")
|
||||||
|
|
||||||
|
if usr_choice == "1":
|
||||||
|
if is_valid_isbn(usr_input):
|
||||||
|
print("Valid ISBN")
|
||||||
|
else:
|
||||||
|
print("Invalid ISBN")
|
||||||
|
if usr_choice == "2":
|
||||||
|
if is_valid_iban(usr_input):
|
||||||
|
print("Valid IBAN")
|
||||||
|
else:
|
||||||
|
print("Invalid IBAN")
|
||||||
|
if usr_choice == "3":
|
||||||
|
break
|
||||||
|
|
||||||
|
def main():
|
||||||
|
app = Checker()
|
||||||
|
app.run()
|
||||||
|
|
||||||
|
console_checker()
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
import tkinter as tk
|
||||||
|
|
||||||
|
from my_package.App_window import AppWindow
|
||||||
|
|
||||||
|
|
||||||
|
class VowelDelete (AppWindow):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(300,300)
|
||||||
|
self.vowels = ['a', 'e', 'i', 'o', 'u']
|
||||||
|
self.button = tk.Button(self.root, text="decompose", command=self.on_click)
|
||||||
|
self.label = tk.Label(self.root, text="enter text: ")
|
||||||
|
self.entry = tk.Entry(self.root, width=20)
|
||||||
|
self.list = tk.Listbox(self.root)
|
||||||
|
|
||||||
|
def init_widgets(self):
|
||||||
|
self.label.grid(row=0, column=0)
|
||||||
|
self.entry.grid(row=0, column=1)
|
||||||
|
self.button.grid(row=1, column=0)
|
||||||
|
self.list.grid(row=1, column=1)
|
||||||
|
|
||||||
|
def on_click(self):
|
||||||
|
entry = self.entry.get()
|
||||||
|
self.entry.delete(tk.END)
|
||||||
|
entry_split = list(entry)
|
||||||
|
|
||||||
|
c_entry_split = entry_split.copy()
|
||||||
|
for vowel in c_entry_split:
|
||||||
|
if vowel in self.vowels:
|
||||||
|
entry_split.remove(vowel)
|
||||||
|
result = ''.join(entry_split)
|
||||||
|
|
||||||
|
self.list.insert(tk.END, str(result))
|
||||||
|
|
||||||
|
def main():
|
||||||
|
app = VowelDelete()
|
||||||
|
app.run()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
class GameServerQueue:
|
||||||
|
def __init__(self, num_player_team: int, queue=None):
|
||||||
|
if queue is None:
|
||||||
|
self.queue = []
|
||||||
|
self._num_player_team = num_player_team
|
||||||
|
|
||||||
|
|
||||||
|
def enqueue(self, player_name):
|
||||||
|
self.queue.append(player_name)
|
||||||
|
|
||||||
|
def dequeue(self):
|
||||||
|
try:
|
||||||
|
dequed_player = self.queue.pop(0)
|
||||||
|
return dequed_player
|
||||||
|
except IndexError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def fill_queue_with_names(self):
|
||||||
|
player_names = ["xx_sniperLegend_xx","smorkelo","John Warframe","uriel","helldiver david"]
|
||||||
|
for i in range(len(player_names)):
|
||||||
|
self.enqueue(player_names[i])
|
||||||
|
|
||||||
|
def build_teams(self):
|
||||||
|
teams = []
|
||||||
|
while len(self.queue) >= 2:
|
||||||
|
teams.append((self.dequeue(), self.dequeue()))
|
||||||
|
return teams
|
||||||
|
|
||||||
|
def main():
|
||||||
|
game = GameServerQueue(num_player_team=3)
|
||||||
|
game.fill_queue_with_names()
|
||||||
|
game.enqueue("the_tested_one")
|
||||||
|
game.enqueue("the_tested_two")
|
||||||
|
|
||||||
|
print(game.queue)
|
||||||
|
print(game.build_teams())
|
||||||
|
print(game.queue)
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
Nuremberg > Berlin
|
||||||
|
Nuremberg > Frankfurt
|
||||||
|
Nuremberg > Munich
|
||||||
|
Munich > Nuremberg
|
||||||
|
Hamburg > Berlin
|
||||||
|
Berlin > Hamburg
|
||||||
|
Berlin > Nuremberg
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
class Trains:
|
||||||
|
def __init__(self):
|
||||||
|
self.cities = []
|
||||||
|
self.connections: dict[str, list] = dict()
|
||||||
|
self.f_length = 0
|
||||||
|
self.setup()
|
||||||
|
|
||||||
|
def add_connection(self, city, connection):
|
||||||
|
self.connections[city] = connection
|
||||||
|
|
||||||
|
def setup(self):
|
||||||
|
with open("t_connections.txt", "r") as f:
|
||||||
|
for line in f:
|
||||||
|
self.f_length += 1
|
||||||
|
|
||||||
|
f.seek(0)
|
||||||
|
for _ in range(self.f_length):
|
||||||
|
line = f.readline().strip()
|
||||||
|
split_line = line.split()
|
||||||
|
|
||||||
|
if split_line[0] not in self.cities: self.cities.append(split_line[0])
|
||||||
|
if split_line[2] not in self.cities: self.cities.append(split_line[2])
|
||||||
|
|
||||||
|
for _ in range(len(self.cities)):
|
||||||
|
self.add_connection(self.cities[_], [])
|
||||||
|
|
||||||
|
f.seek(0)
|
||||||
|
for _ in range(self.f_length):
|
||||||
|
line = f.readline().strip()
|
||||||
|
split_line = line.split()
|
||||||
|
if split_line[2] not in self.connections[split_line[0]]:
|
||||||
|
self.connections[split_line[0]].append(split_line[2])
|
||||||
|
|
||||||
|
def get_directions(self,origin):
|
||||||
|
try:
|
||||||
|
return self.connections[origin]
|
||||||
|
except KeyError:
|
||||||
|
return "Origin not Found"
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
trains = Trains()
|
||||||
|
print(trains.cities)
|
||||||
|
print(trains.connections)
|
||||||
|
|
||||||
|
print("Welcome to Route-Manager")
|
||||||
|
print(f"Known origins {trains.cities}")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
print("1. Proceed to route search")
|
||||||
|
print("2. quit")
|
||||||
|
usr_input = input("1 / 2 ?: ")
|
||||||
|
|
||||||
|
if usr_input == "1":
|
||||||
|
usr_origin = input("Set your origin: ")
|
||||||
|
|
||||||
|
if usr_origin in trains.cities:
|
||||||
|
directions = trains.get_directions(usr_origin)
|
||||||
|
l = len(directions)
|
||||||
|
|
||||||
|
if l != 0:
|
||||||
|
for _ in range(l):
|
||||||
|
print(f"{usr_origin} -> {directions[_]}")
|
||||||
|
|
||||||
|
else:
|
||||||
|
print(f"No connections from {usr_origin}")
|
||||||
|
|
||||||
|
else:
|
||||||
|
print("Origin not Found")
|
||||||
|
|
||||||
|
if usr_input == "2":
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
def permute(picked: str, remaining: str):
|
||||||
|
if remaining == "" :
|
||||||
|
print(picked + remaining + ", ", end="")
|
||||||
|
|
||||||
|
else:
|
||||||
|
for i in range(len(remaining)):
|
||||||
|
pick = remaining[i]
|
||||||
|
front = remaining[:i]
|
||||||
|
back = remaining[i+1:]
|
||||||
|
permute(picked + pick, front + back)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
start = time.time()
|
||||||
|
permute("","abcdefghijk")
|
||||||
|
end = time.time()
|
||||||
|
print(end - start)
|
||||||
|
#"abcdefghijk" takes 85 sec
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import math
|
||||||
|
import random
|
||||||
|
from turtle import Turtle
|
||||||
|
|
||||||
|
|
||||||
|
def draw_turtle_line(turtle, start: (float,float), end: (float, float)):
|
||||||
|
turtle.penup()
|
||||||
|
turtle.goto(start[0], start[1])
|
||||||
|
turtle.pendown()
|
||||||
|
turtle.goto(end[0], end[1])
|
||||||
|
|
||||||
|
def draw_branch(turtle, point: (float, float), angle: float, length: float):
|
||||||
|
if length > 10:
|
||||||
|
x1 = round(point[0] - math.sin(angle) * length)
|
||||||
|
y1 = round(point[1] + math.cos(angle) * length)
|
||||||
|
draw_turtle_line(turtle, point, (x1, y1))
|
||||||
|
|
||||||
|
bend_angle = math.radians(random.uniform(-10, 10))
|
||||||
|
branch_angle = math.radians(random.uniform(-40, 40))
|
||||||
|
draw_branch(turtle,(x1, y1), bend_angle - branch_angle, length *
|
||||||
|
random.uniform(0.6, 0.8))
|
||||||
|
draw_branch(turtle,(x1, y1), bend_angle + branch_angle, length *
|
||||||
|
random.uniform(0.6, 0.8))
|
||||||
|
|
||||||
|
sophia = Turtle()
|
||||||
|
draw_branch(sophia, (0, 0), 0.0 ,100)
|
||||||
|
|
||||||
|
sophia.screen.mainloop()
|
||||||
|
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
list_of_names = ['Alexander', 'Maximilianosohn', 'Peter', 'Jan',
|
||||||
|
'Paul', 'Sabine', 'Franzi von Ulmen']
|
||||||
|
|
||||||
|
def bublesort(lst):
|
||||||
|
for element in range(len(lst) - 1):
|
||||||
|
if element != len(lst):
|
||||||
|
if lst[element] > lst[element+1]:
|
||||||
|
lst[element], lst[element+1] = lst[element+1], lst[element]
|
||||||
|
print(f"'{lst[element]}' is greater then '{lst[element + 1]}', their postions are now swaped")
|
||||||
|
print(f"current state of the list: {lst}")
|
||||||
|
print()
|
||||||
|
time.sleep(3)
|
||||||
|
if lst != sorted(lst):
|
||||||
|
bublesort(lst)
|
||||||
|
print("bubble sort calls itself")
|
||||||
|
print()
|
||||||
|
time.sleep(2)
|
||||||
|
return lst
|
||||||
|
|
||||||
|
print("Bubble sort is a simple sorting algorithm, that recursively iterates over the entire list")
|
||||||
|
print()
|
||||||
|
time.sleep(4)
|
||||||
|
print("Each letter in the alphabet has its own value, the later they appear in the alphabet, the higher their value")
|
||||||
|
print()
|
||||||
|
time.sleep(4)
|
||||||
|
print("If a value is greater then the next value in the list, their positions will be swapped")
|
||||||
|
print()
|
||||||
|
time.sleep(4)
|
||||||
|
print("This Process is repeated until all elements in the list are sorted.")
|
||||||
|
print()
|
||||||
|
time.sleep(4)
|
||||||
|
print("The following messages will show the process in detail")
|
||||||
|
print()
|
||||||
|
time.sleep(3)
|
||||||
|
print(bublesort(list_of_names))
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
from my_package.Game_class import Game
|
||||||
|
from my_package.Spark_class import Spark
|
||||||
|
|
||||||
|
|
||||||
|
class PiDraw(Game):
|
||||||
|
def __init__(self, inner_c=None, outer_c=None):
|
||||||
|
self.inner_c = inner_c if inner_c is not None else []
|
||||||
|
self.outer_c = outer_c if outer_c is not None else []
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
def update_game(self):
|
||||||
|
for _ in range(2):
|
||||||
|
x = random.randint(0,500)
|
||||||
|
y = random.randint(0,500)
|
||||||
|
|
||||||
|
if (x**2 + y**2) < 500**2: self.inner_c.append(Spark(x, y, (0,255,0), 1))
|
||||||
|
else: self.outer_c.append(Spark(x, y, (255,0,0), 1))
|
||||||
|
|
||||||
|
try:
|
||||||
|
pi = 4.0 * len(self.inner_c) / (len(self.inner_c) + len(self.outer_c))
|
||||||
|
print(pi)
|
||||||
|
except ZeroDivisionError: print("ZeroDivisionError")
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_game(self):
|
||||||
|
for particle in self.inner_c + self.outer_c:
|
||||||
|
particle.set_surface(self.set_screen())
|
||||||
|
particle.appear(self.set_screen())
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
game = PiDraw()
|
||||||
|
game.run()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
def gcd():
|
||||||
|
i1 = int(input("enter first number : "))
|
||||||
|
i2 = int(input("enter second number : "))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
i1 // i2
|
||||||
|
r = i1 % i2
|
||||||
|
|
||||||
|
if r == 0:
|
||||||
|
print(f"GCD= {i2}")
|
||||||
|
break
|
||||||
|
|
||||||
|
else:
|
||||||
|
i1 = i2
|
||||||
|
i2 = r
|
||||||
|
|
||||||
|
gcd()
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
import tkinter as tk
|
||||||
|
|
||||||
|
from my_package.App_window import AppWindow
|
||||||
|
|
||||||
|
label_names = ["a:","b:","m:","seed:"]
|
||||||
|
entries = []
|
||||||
|
labels = []
|
||||||
|
|
||||||
|
class LehmerAlg (AppWindow):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(300,500)
|
||||||
|
self.button = tk.Button(self.root, text="calculate", command=self.on_click)
|
||||||
|
self.c_button = tk.Button(self.root, text="close", command=self.close)
|
||||||
|
self.list = tk.Listbox(self.root)
|
||||||
|
|
||||||
|
def init_widgets(self):
|
||||||
|
r = 0
|
||||||
|
for _ in range(4):
|
||||||
|
entry = tk.Entry(self.root, width=20)
|
||||||
|
entry.grid(row=r, column=1)
|
||||||
|
entries.append(entry)
|
||||||
|
label = tk.Label(self.root, text=f"{label_names[r]}")
|
||||||
|
label.grid(row=r, column=0)
|
||||||
|
labels.append(label)
|
||||||
|
r +=1
|
||||||
|
self.button.grid(column=2, row=0)
|
||||||
|
self.c_button.grid(column=2, row=1)
|
||||||
|
self.list.grid(column=1, row=5)
|
||||||
|
|
||||||
|
def on_click(self):
|
||||||
|
val = 0
|
||||||
|
for _ in range(4):
|
||||||
|
label_names[val] = entries[val].get()
|
||||||
|
entries[val].delete(0, tk.END)
|
||||||
|
val +=1
|
||||||
|
|
||||||
|
result = (int(label_names[0])*int(label_names[3])+int(label_names[1])) %int(label_names[2])
|
||||||
|
#v_k = (a * v_k + b) % m
|
||||||
|
self.list.insert(tk.END, result)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.root.destroy()
|
||||||
|
|
||||||
|
def main ():
|
||||||
|
app = LehmerAlg()
|
||||||
|
app.run()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
r = 50
|
||||||
|
for _ in range (r):
|
||||||
|
print("i like programming")
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
class Node:
|
||||||
|
def __init__(self, data):
|
||||||
|
self.data = data
|
||||||
|
self.visited = False
|
||||||
|
self.left = None
|
||||||
|
self.right = None
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.data
|
||||||
|
def __repr__(self):
|
||||||
|
return self.__str__()
|
||||||
|
|
||||||
|
def traverse (node, stack = None):
|
||||||
|
if stack is None:
|
||||||
|
stack = []
|
||||||
|
|
||||||
|
if node.left and node.left.visited == False:
|
||||||
|
stack.append(node.left)
|
||||||
|
print(node.left.data)
|
||||||
|
node.left.visited = True
|
||||||
|
traverse(node.left)
|
||||||
|
|
||||||
|
if node.right and node.right.visited == False:
|
||||||
|
stack.append(node.right)
|
||||||
|
print(node.right.data)
|
||||||
|
node.right.visited = True
|
||||||
|
traverse(node.right)
|
||||||
|
|
||||||
|
else :
|
||||||
|
if len(stack) > 0:
|
||||||
|
traverse(stack[len(stack)-1])
|
||||||
|
stack.pop()
|
||||||
|
|
||||||
|
root = Node(6)
|
||||||
|
node_two = Node(2)
|
||||||
|
node_seven = Node(7)
|
||||||
|
node_one = Node(1)
|
||||||
|
node_four = Node(4)
|
||||||
|
node_three = Node(3)
|
||||||
|
node_five = Node(5)
|
||||||
|
node_six = Node(6)
|
||||||
|
node_nine = Node(9)
|
||||||
|
|
||||||
|
|
||||||
|
root.left = node_two
|
||||||
|
root.right = node_seven
|
||||||
|
|
||||||
|
node_two.left = node_one
|
||||||
|
node_one.right = node_four
|
||||||
|
|
||||||
|
node_four.right = node_three
|
||||||
|
node_three.right = node_five
|
||||||
|
|
||||||
|
node_seven.left = node_six
|
||||||
|
node_six.right = node_nine
|
||||||
|
|
||||||
|
traverse(root)
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
class Node:
|
||||||
|
def __init__(self, data):
|
||||||
|
self.data = data
|
||||||
|
self.visited = False
|
||||||
|
self.children = []
|
||||||
|
|
||||||
|
def is_internal_external(self):
|
||||||
|
if self.children:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_children(self):
|
||||||
|
return self.children
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.data
|
||||||
|
def __repr__(self):
|
||||||
|
return self.__str__()
|
||||||
|
|
||||||
|
def traverse (node, stack = None):
|
||||||
|
if stack is None:
|
||||||
|
stack = []
|
||||||
|
|
||||||
|
if node.children is not None:
|
||||||
|
for child in node.children:
|
||||||
|
if not child.visited:
|
||||||
|
child.visited = True
|
||||||
|
stack.append(child)
|
||||||
|
print(child.data)
|
||||||
|
traverse(child)
|
||||||
|
else:
|
||||||
|
if len(stack) > 0:
|
||||||
|
traverse(stack[len(stack) - 1])
|
||||||
|
stack.pop()
|
||||||
|
|
||||||
|
root = Node(6)
|
||||||
|
node_two = Node(2)
|
||||||
|
node_seven = Node(7)
|
||||||
|
node_one = Node(1)
|
||||||
|
node_four = Node(4)
|
||||||
|
node_three = Node(3)
|
||||||
|
node_five = Node(5)
|
||||||
|
node_six = Node(6)
|
||||||
|
node_nine = Node(9)
|
||||||
|
|
||||||
|
root.children = [node_two, node_seven, node_one]
|
||||||
|
node_two.children = [node_four, node_three]
|
||||||
|
node_seven.children = [node_five, node_six]
|
||||||
|
|
||||||
|
traverse(root)
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import numpy as np
|
||||||
|
|
||||||
|
bins = [" 0-9: ","10-19: ","20-29: ","30-39: ","40-49: ","50-59: ","60-69: ","70-79: ","80-89: ","90-99: "]
|
||||||
|
|
||||||
|
def get_distribution():
|
||||||
|
# Generate normal distribution (mean_value, standard_deviation, size)
|
||||||
|
exam_points = np.random.normal(60, 20, 67)
|
||||||
|
# round to integer datatype
|
||||||
|
exam_points = np.rint(exam_points).astype(int)
|
||||||
|
# clip values between 15 and 95
|
||||||
|
exam_points = np.clip(exam_points, 15, 95)
|
||||||
|
|
||||||
|
for points in exam_points:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return exam_points
|
||||||
|
|
||||||
|
def generate_arr():
|
||||||
|
arr = np.ndarray((10,10), dtype=object)
|
||||||
|
|
||||||
|
for pos in range (10):
|
||||||
|
arr[(pos,0)] = bins[pos]
|
||||||
|
|
||||||
|
print(arr)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
generate_arr()
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
class Student:
|
||||||
|
def __init__(self, number, name, email):
|
||||||
|
self.number = number
|
||||||
|
self.name = name
|
||||||
|
self.email = email
|
||||||
|
self.MAX_HASH =100000
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"Student: {self.name} ({self.email}, {self.number})"
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
return (id(self.number) + id(self.name) + id(self.email)) % self.MAX_HASH
|
||||||
|
|
||||||
|
|
||||||
|
class HashTable:
|
||||||
|
def __init__(self, elements):
|
||||||
|
self.bucket_size = len(elements) * 3
|
||||||
|
self.buckets = [[] for i in range(self.bucket_size)]
|
||||||
|
self._assign_buckets(elements)
|
||||||
|
self.insertion_order = []
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
ret_list = []
|
||||||
|
for index, obj_id in self.insertion_order:
|
||||||
|
for obj in self.buckets[index]:
|
||||||
|
if id(obj) == obj_id:
|
||||||
|
ret_list.append(obj)
|
||||||
|
return iter(ret_list)
|
||||||
|
|
||||||
|
def _assign_buckets(self, elements):
|
||||||
|
for obj in elements:
|
||||||
|
hashed_value = hash(obj)
|
||||||
|
index = hashed_value % self.bucket_size
|
||||||
|
self.buckets[index].append(obj)
|
||||||
|
self.insertion_order.insert((index, id(obj)))
|
||||||
|
|
||||||
|
def get_value(self,input_key):
|
||||||
|
return self.buckets[input_key]
|
||||||
|
|
||||||
|
def main():
|
||||||
|
arthur = Student("097", "Arthur", "artur@blade.hex")
|
||||||
|
eleanor = Student("098", "Eleanor", "eleano@plague.hex")
|
||||||
|
aoi = Student("099", "Aoi", "aoi@magnet.hex")
|
||||||
|
hash_table = HashTable([arthur,eleanor,aoi])
|
||||||
|
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
class Student:
|
||||||
|
|
||||||
|
def __init__(self, number, name, email):
|
||||||
|
self.number = number
|
||||||
|
self.name = name
|
||||||
|
self.email = email
|
||||||
|
self.MAX_HASH =100000
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"Student: {self.name} ({self.email}, {self.number})"
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
return (id(self.number) + id(self.name) + id(self.email)) % self.MAX_HASH
|
||||||
|
|
||||||
|
def main():
|
||||||
|
student = Student("0975", "Arthur", "arthur@blade.hex")
|
||||||
|
print(hash(student))
|
||||||
|
for _ in range(10):
|
||||||
|
main()
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
|
||||||
|
|
||||||
|
class RouteManager:
|
||||||
|
def __init__(self,file="t_connections.txt"):
|
||||||
|
self.cities = []
|
||||||
|
self.connections: dict[str, list] = dict()
|
||||||
|
self.journey = []
|
||||||
|
self.f_length = 0
|
||||||
|
self.setup(file)
|
||||||
|
|
||||||
|
def add_connection(self, city, connection):
|
||||||
|
self.connections[city] = connection
|
||||||
|
|
||||||
|
|
||||||
|
def setup(self,file):
|
||||||
|
with open(file, "r") as f:
|
||||||
|
for line in f:
|
||||||
|
self.f_length += 1
|
||||||
|
|
||||||
|
f.seek(0)
|
||||||
|
for _ in range(self.f_length):
|
||||||
|
line = f.readline().strip()
|
||||||
|
split_line = line.split()
|
||||||
|
|
||||||
|
|
||||||
|
if split_line[0] not in self.cities:
|
||||||
|
self.cities.append(split_line[0])
|
||||||
|
|
||||||
|
|
||||||
|
if split_line[2] not in self.cities:
|
||||||
|
self.cities.append(split_line[2])
|
||||||
|
|
||||||
|
for _ in range(len(self.cities)):
|
||||||
|
self.add_connection(self.cities[_], [])
|
||||||
|
|
||||||
|
f.seek(0)
|
||||||
|
for _ in range(self.f_length):
|
||||||
|
line = f.readline().strip()
|
||||||
|
split_line = line.split()
|
||||||
|
if split_line[2] not in self.connections[split_line[0]]:
|
||||||
|
self.connections[split_line[0]].append(split_line[2])
|
||||||
|
|
||||||
|
def get_directions(self,origin,):
|
||||||
|
try:
|
||||||
|
return self.connections[origin]
|
||||||
|
except KeyError:
|
||||||
|
return "Origin not Found"
|
||||||
|
|
||||||
|
def create_route(self,origin):
|
||||||
|
if origin in self.cities:
|
||||||
|
directions = self.get_directions(origin)
|
||||||
|
l = len(directions)
|
||||||
|
|
||||||
|
if l != 0:
|
||||||
|
for _ in range(l):
|
||||||
|
print(f"{origin} -> {directions[_]}")
|
||||||
|
print()
|
||||||
|
else:
|
||||||
|
print(f"No connections from {origin}")
|
||||||
|
else:
|
||||||
|
print("Origin not Found")
|
||||||
|
|
||||||
|
def add_to_journey(self,origin,destination):
|
||||||
|
if not self.journey:
|
||||||
|
self.journey.append(origin)
|
||||||
|
self.journey.append(destination)
|
||||||
|
else: self.journey.append(destination)
|
||||||
|
|
||||||
|
def save_journey(self,journey_name):
|
||||||
|
with open(f"{journey_name}.txt", "a") as f:
|
||||||
|
f.writelines(f" > {self.journey[_]}" for _ in range (len(self.journey)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
rm = RouteManager()
|
||||||
|
print("Welcome to Route-Manager")
|
||||||
|
print(f"Known origins {rm.cities}")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
print("1. New journey")
|
||||||
|
print("2. Load journey")
|
||||||
|
print("3. quit")
|
||||||
|
usr_input = input("1 / 2 / 3?: ")
|
||||||
|
|
||||||
|
if usr_input == "1":
|
||||||
|
while True:
|
||||||
|
usr_origin = input("Set your origin: ")
|
||||||
|
print()
|
||||||
|
rm.create_route(usr_origin)
|
||||||
|
usr_destination = input("Type in your destination: ")
|
||||||
|
rm.add_to_journey(usr_origin, usr_destination)
|
||||||
|
print()
|
||||||
|
|
||||||
|
_usr_input = input("Continue Journey? (y/n): ")
|
||||||
|
|
||||||
|
if _usr_input == "n":
|
||||||
|
usr_save = input("Save journey? (y/n): ")
|
||||||
|
|
||||||
|
if usr_save == "y":
|
||||||
|
file_name = input("Enter file name: ")
|
||||||
|
rm.save_journey(file_name)
|
||||||
|
break
|
||||||
|
|
||||||
|
if usr_input == "2":
|
||||||
|
pass
|
||||||
|
|
||||||
|
if usr_input == "3":
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
Nuremberg > Berlin
|
||||||
|
Nuremberg > Frankfurt
|
||||||
|
Nuremberg > Munich
|
||||||
|
Munich > Nuremberg
|
||||||
|
Hamburg > Berlin
|
||||||
|
Berlin > Hamburg
|
||||||
|
Berlin > Nuremberg
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
> Nuremberg
|
||||||
|
> Frankfurt
|
||||||
|
> Nuremberg
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
class RouteManager:
|
||||||
|
def __init__(self,file="t_connections.txt"):
|
||||||
|
self.cities = []
|
||||||
|
self.connections: dict[str, list] = dict()
|
||||||
|
self.journey = []
|
||||||
|
self.f_length = 0
|
||||||
|
self.setup(file)
|
||||||
|
|
||||||
|
def add_connection(self, city, connection):
|
||||||
|
self.connections[city] = connection
|
||||||
|
|
||||||
|
def setup(self,file):
|
||||||
|
with open(file, "r") as f:
|
||||||
|
for line in f:
|
||||||
|
self.f_length += 1
|
||||||
|
|
||||||
|
f.seek(0)
|
||||||
|
for _ in range(self.f_length):
|
||||||
|
line = f.readline().strip()
|
||||||
|
split_line = line.split()
|
||||||
|
|
||||||
|
if split_line[0] not in self.cities: self.cities.append(split_line[0])
|
||||||
|
if split_line[2] not in self.cities: self.cities.append(split_line[2])
|
||||||
|
|
||||||
|
for _ in range(len(self.cities)):
|
||||||
|
self.add_connection(self.cities[_], [])
|
||||||
|
|
||||||
|
f.seek(0)
|
||||||
|
for _ in range(self.f_length):
|
||||||
|
line = f.readline().strip()
|
||||||
|
split_line = line.split()
|
||||||
|
if split_line[2] not in self.connections[split_line[0]]:
|
||||||
|
self.connections[split_line[0]].append(split_line[2])
|
||||||
|
|
||||||
|
def get_directions(self,origin,):
|
||||||
|
try:
|
||||||
|
return self.connections[origin]
|
||||||
|
except KeyError:
|
||||||
|
return "Origin not Found"
|
||||||
|
|
||||||
|
def create_route(self,origin):
|
||||||
|
if origin in self.cities:
|
||||||
|
directions = self.get_directions(origin)
|
||||||
|
l = len(directions)
|
||||||
|
|
||||||
|
if l != 0:
|
||||||
|
for _ in range(l):
|
||||||
|
print(f"{origin} -> {directions[_]}")
|
||||||
|
print()
|
||||||
|
else:
|
||||||
|
print(f"No connections from {origin}")
|
||||||
|
else:
|
||||||
|
print("Origin not Found")
|
||||||
|
|
||||||
|
def add_to_journey(self,origin,destination):
|
||||||
|
if not self.journey:
|
||||||
|
self.journey.append(origin)
|
||||||
|
self.journey.append(destination)
|
||||||
|
else: self.journey.append(destination)
|
||||||
|
|
||||||
|
def save_journey(self,journey_name):
|
||||||
|
with open(f"{journey_name}.txt", "a") as f:
|
||||||
|
f.writelines(f" > {self.journey[_]}" for _ in range (len(self.journey)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
rm = RouteManager()
|
||||||
|
print("Welcome to Route-Manager")
|
||||||
|
print(f"Known origins {rm.cities}")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
print("1. New journey")
|
||||||
|
print("2. Load journey")
|
||||||
|
print("3. quit")
|
||||||
|
usr_input = input("1 / 2 / 3?: ")
|
||||||
|
|
||||||
|
if usr_input == "1":
|
||||||
|
while True:
|
||||||
|
usr_origin = input("Set your origin: ")
|
||||||
|
print()
|
||||||
|
rm.create_route(usr_origin)
|
||||||
|
usr_destination = input("Type in your destination: ")
|
||||||
|
rm.add_to_journey(usr_origin, usr_destination)
|
||||||
|
print()
|
||||||
|
|
||||||
|
_usr_input = input("Continue Journey? (y/n): ")
|
||||||
|
|
||||||
|
if _usr_input == "n":
|
||||||
|
usr_save = input("Save journey? (y/n): ")
|
||||||
|
|
||||||
|
if usr_save == "y":
|
||||||
|
file_name = input("Enter file name: ")
|
||||||
|
rm.save_journey(file_name)
|
||||||
|
break
|
||||||
|
|
||||||
|
if usr_input == "2":
|
||||||
|
pass
|
||||||
|
|
||||||
|
if usr_input == "3":
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
def palindrome_check(word: str, front: int = 0):
|
||||||
|
|
||||||
|
back = len(word)-1 - front
|
||||||
|
print(word[front], word[back])
|
||||||
|
|
||||||
|
if front >= back:
|
||||||
|
print("Palindrome")
|
||||||
|
|
||||||
|
elif word[front] == word[back]:
|
||||||
|
front += 1
|
||||||
|
palindrome_check(word, front)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
word = input("Enter a word: ")
|
||||||
|
word = word.casefold()
|
||||||
|
word = word.replace(" ", "")
|
||||||
|
|
||||||
|
palindrome_check(word)
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
from my_package.Game_class import Game
|
||||||
|
|
||||||
|
|
||||||
|
class TriangleTriangle(Game):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.WHITE = (255, 255, 255)
|
||||||
|
self.surface = pygame.Surface((100, 100))
|
||||||
|
|
||||||
|
def draw_triangle(self, screen, x: float, y: float, w: float, h:float):
|
||||||
|
p1 = (x, y)
|
||||||
|
p2 = (x + w, y)
|
||||||
|
p3 = (x + w / 2, y + h)
|
||||||
|
pygame.draw.polygon(screen, self.WHITE, [p1, p2, p3], 1)
|
||||||
|
|
||||||
|
def draw_sierpinski(self, screen, x: float, y: float, w: float, h:float):
|
||||||
|
self.draw_triangle(screen, x, y, w, h)
|
||||||
|
|
||||||
|
# TODO: base case
|
||||||
|
# recursive case
|
||||||
|
h2 = h / 2
|
||||||
|
w2 = w / 2
|
||||||
|
self.draw_sierpinski(screen, x, y, w2, h2)
|
||||||
|
self.draw_sierpinski(screen, x + w2 / 2, y + h2, w2, h2)
|
||||||
|
self.draw_sierpinski(screen, x + w2, y, w2, h2)
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
def _min(lst: list, previous = None):
|
||||||
|
if previous is None:
|
||||||
|
previous = lst[0]
|
||||||
|
for item in lst:
|
||||||
|
previous = min(previous, item)
|
||||||
|
return previous
|
||||||
|
|
||||||
|
def _max(lst: list, previous = None):
|
||||||
|
if previous is None:
|
||||||
|
previous = lst[0]
|
||||||
|
for item in lst:
|
||||||
|
previous = max(previous, item)
|
||||||
|
return previous
|
||||||
|
|
||||||
|
def _avg(lst: list, total = 0):
|
||||||
|
for item in lst:
|
||||||
|
total += item
|
||||||
|
return total / len(lst)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
i_lst = [5, 55, 2, 7, 45, 3, 1, 8, 23, 12]
|
||||||
|
start = time.time()
|
||||||
|
|
||||||
|
for _ in range(100000):
|
||||||
|
_min(i_lst)
|
||||||
|
|
||||||
|
end = time.time()
|
||||||
|
print(end - start)
|
||||||
|
print(_max(i_lst))
|
||||||
|
print(_avg(i_lst))
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
def calc_prime(n: int, d = 2):
|
||||||
|
if n % d == 0:
|
||||||
|
return d
|
||||||
|
else:
|
||||||
|
d += 1
|
||||||
|
calc_prime(n, d)
|
||||||
|
return False
|
||||||
|
|
||||||
|
def power_calc(n: int):
|
||||||
|
p = calc_prime(n)
|
||||||
|
#print(f"{p}^{n/p}")
|
||||||
|
print(f"{n}/{p}")
|
||||||
|
|
||||||
|
power_calc(81)
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
def fibitts(s_population: list, months: int, previous = None):
|
||||||
|
#every 2 months: 1 pair -> 2 pairs
|
||||||
|
#newborn pair can produce a new pair only after 2 months
|
||||||
|
|
||||||
|
if previous is None:
|
||||||
|
previous = s_population[0]
|
||||||
|
|
||||||
|
for month in range(months+1):
|
||||||
|
if month == 0:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
s_population.append(s_population[month]+previous)
|
||||||
|
print(f"Month {month}: {s_population[month]}")
|
||||||
|
previous = s_population[month]
|
||||||
|
|
||||||
|
fibitts([1,1], 24)
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
new_lst = ["Alex", "Moin", "ka", "world", "Zh", "lakj"]
|
||||||
|
s_lst = sorted(new_lst)
|
||||||
|
|
||||||
|
def bnr_search(lst, target):
|
||||||
|
low = 0
|
||||||
|
high = len(lst) - 1
|
||||||
|
|
||||||
|
while low <= high:
|
||||||
|
mid = (low + high) // 2
|
||||||
|
|
||||||
|
if lst[mid] == target:
|
||||||
|
print("found target")
|
||||||
|
return mid
|
||||||
|
elif lst[mid] < target:
|
||||||
|
low = mid + 1
|
||||||
|
else:
|
||||||
|
high = mid - 1
|
||||||
|
|
||||||
|
return None, print("not found")
|
||||||
|
|
||||||
|
|
||||||
|
bnr_search(s_lst, "Alex")
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
new_lst = ["Hallo", "Alex", "Moin", "ka", "world", "Zh"]
|
||||||
|
|
||||||
|
def lin_search(lst,target):
|
||||||
|
for element in lst:
|
||||||
|
if element == target:
|
||||||
|
print(f"found '{element}' in {lst}")
|
||||||
|
print("finished search")
|
||||||
|
|
||||||
|
lin_search(new_lst, "Alex")
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
list_of_ints = [5, 55, 2, 7, 45, 3, 1, 8, 23, 12]
|
||||||
|
list_of_ints.sort(reverse=False)
|
||||||
|
print(list_of_ints)
|
||||||
|
l = ['Hallo', 'Alex', 'Moin', 'ka', 'world', 'Zh', 'lakj']
|
||||||
|
new_sorted_list = sorted(l, key= len)
|
||||||
|
print(new_sorted_list)
|
||||||
|
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
import random
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
def merge_sort(arr: list[int]) -> list[int]:
|
||||||
|
if len(arr) <= 1:
|
||||||
|
return arr
|
||||||
|
|
||||||
|
mid = len(arr) // 2
|
||||||
|
left_half = arr[:mid]
|
||||||
|
right_half = arr[mid:]
|
||||||
|
|
||||||
|
left_half = merge_sort(left_half)
|
||||||
|
right_half = merge_sort(right_half)
|
||||||
|
|
||||||
|
return merge(left_half, right_half)
|
||||||
|
|
||||||
|
def merge(left: list[int], right: list[int], merged=None) -> list[int]:
|
||||||
|
if merged is None:
|
||||||
|
merged = []
|
||||||
|
l_index = 0
|
||||||
|
r_index = 0
|
||||||
|
|
||||||
|
while l_index < len(left) and r_index < len(right):
|
||||||
|
if left[l_index] <= right[r_index]:
|
||||||
|
merged.append(left[l_index])
|
||||||
|
l_index += 1
|
||||||
|
else:
|
||||||
|
merged.append(right[r_index])
|
||||||
|
r_index += 1
|
||||||
|
|
||||||
|
merged += left[l_index:]
|
||||||
|
merged += right[r_index:]
|
||||||
|
return merged
|
||||||
|
|
||||||
|
NUM_ELEMENTS = 1_000_000
|
||||||
|
l = [random.randint(0, 2*NUM_ELEMENTS) for _ in range(NUM_ELEMENTS)]
|
||||||
|
|
||||||
|
start = time.time()
|
||||||
|
print(merge_sort(l))
|
||||||
|
end = time.time()
|
||||||
|
print(f"time elapsed: {end - start} sec")
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import random
|
||||||
|
import time
|
||||||
|
|
||||||
|
NUM_ELEMENTS = 1_000_000
|
||||||
|
l = [random.randint(0, 2*NUM_ELEMENTS) for _ in range(NUM_ELEMENTS)]
|
||||||
|
|
||||||
|
start = time.time()
|
||||||
|
sorted(l)
|
||||||
|
end = time.time()
|
||||||
|
print(end - start)
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
class Node:
|
||||||
|
def __init__(self, value):
|
||||||
|
self.value = value
|
||||||
|
def __str__(self):
|
||||||
|
return self.value
|
||||||
|
def __repr__(self):
|
||||||
|
return self.__str__()
|
||||||
|
|
||||||
|
class Graph:
|
||||||
|
def __init__(self):
|
||||||
|
self.nodes = []
|
||||||
|
self.connections = {}
|
||||||
|
self.new_node("root")
|
||||||
|
|
||||||
|
def new_node(self,value, connections= None):
|
||||||
|
value = Node(value)
|
||||||
|
self.connections[value] = connections
|
||||||
|
self.nodes.append(value)
|
||||||
|
|
||||||
|
def add_connection(self, node_name, connections):
|
||||||
|
self.connections[node_name] = connections
|
||||||
|
|
||||||
|
|
||||||
|
def create_graph():
|
||||||
|
g = Graph()
|
||||||
|
g.new_node("1")
|
||||||
|
g.add_connection("1", ["root","2","3"])
|
||||||
|
g.new_node("2")
|
||||||
|
g.add_connection("2", ["root","3","4"])
|
||||||
|
g.new_node("3")
|
||||||
|
g.new_node("4")
|
||||||
|
g.add_connection("3", ["5"])
|
||||||
|
g.new_node("5")
|
||||||
|
|
||||||
|
create_graph()
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
from collections import deque
|
||||||
|
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import networkx as nx
|
||||||
|
|
||||||
|
# ==========================================
|
||||||
|
# 1. THE SEARCH ALGORITHMS (Add new ones here)
|
||||||
|
# ==========================================
|
||||||
|
|
||||||
|
|
||||||
|
def bfs_algorithm(graph, start_node):
|
||||||
|
"""
|
||||||
|
Breadth-First Search
|
||||||
|
"""
|
||||||
|
visited = set()
|
||||||
|
queue = deque([start_node])
|
||||||
|
|
||||||
|
while queue:
|
||||||
|
current = queue.popleft()
|
||||||
|
|
||||||
|
if current not in visited:
|
||||||
|
visited.add(current)
|
||||||
|
|
||||||
|
for neighbor in graph.neighbors(current):
|
||||||
|
if neighbor not in visited:
|
||||||
|
queue.append(neighbor)
|
||||||
|
|
||||||
|
# Send current engine state back to the visualizer
|
||||||
|
yield current, visited
|
||||||
|
|
||||||
|
|
||||||
|
def recursive_dfs_algorithm(graph, start_node):
|
||||||
|
"""
|
||||||
|
Depth-First Search
|
||||||
|
"""
|
||||||
|
visited = set()
|
||||||
|
|
||||||
|
# 1. Define the internal recursive function
|
||||||
|
def dfs_helper(node):
|
||||||
|
if node not in visited:
|
||||||
|
visited.add(node)
|
||||||
|
|
||||||
|
# Send current frame state back up the chain to the visualizer
|
||||||
|
yield node, visited
|
||||||
|
|
||||||
|
for neighbor in graph.neighbors(node):
|
||||||
|
if neighbor not in visited:
|
||||||
|
# 2. 'yield from' intercepts values from deep recursive steps
|
||||||
|
yield from dfs_helper(neighbor)
|
||||||
|
|
||||||
|
# 3. Start the recursion and pass the generator out
|
||||||
|
return dfs_helper(start_node)
|
||||||
|
|
||||||
|
|
||||||
|
# ==========================================
|
||||||
|
# 2. THE VISUALIZER ENGINE
|
||||||
|
# ==========================================
|
||||||
|
|
||||||
|
class GraphVisualizer:
|
||||||
|
def __init__(self, graph):
|
||||||
|
self.graph = graph
|
||||||
|
# Precompute layouts so nodes stay completely still
|
||||||
|
self.pos = nx.spring_layout(graph, seed=42)
|
||||||
|
|
||||||
|
def run_animation(self, algorithm_generator, title="Search Animation", delay=1.5):
|
||||||
|
"""
|
||||||
|
Consumes states from the algorithm generator and updates the frame.
|
||||||
|
"""
|
||||||
|
plt.ion() # Turn on interactive plotting mode
|
||||||
|
fig, ax = plt.subplots(figsize=(6, 5))
|
||||||
|
|
||||||
|
# Pull each state frame out of the search algorithm sequentially
|
||||||
|
for current_node, visited_nodes in algorithm_generator:
|
||||||
|
|
||||||
|
# Build node colors based on current frame data
|
||||||
|
color_map = []
|
||||||
|
for node in self.graph.nodes():
|
||||||
|
if node == current_node:
|
||||||
|
color_map.append('orange') # Current frontier node
|
||||||
|
elif node in visited_nodes:
|
||||||
|
color_map.append('lightgreen') # Already processed
|
||||||
|
else:
|
||||||
|
color_map.append('skyblue') # Unvisited
|
||||||
|
|
||||||
|
# Draw frame
|
||||||
|
ax.clear()
|
||||||
|
nx.draw(
|
||||||
|
self.graph, self.pos, with_labels=True,
|
||||||
|
node_color=color_map, node_size=800,
|
||||||
|
font_weight='bold', ax=ax
|
||||||
|
)
|
||||||
|
ax.set_title(f"{title}\nActive Node: {current_node}")
|
||||||
|
|
||||||
|
plt.draw()
|
||||||
|
plt.pause(delay)
|
||||||
|
|
||||||
|
plt.ioff()
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
# ==========================================
|
||||||
|
# 3. RUNNING THE CODE
|
||||||
|
# ==========================================
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Define a test graph
|
||||||
|
G = nx.Graph()
|
||||||
|
edges = [('A', 'B'), ('A', 'C'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('E', 'F')]
|
||||||
|
G.add_edges_from(edges)
|
||||||
|
|
||||||
|
# Initialize visualizer engine
|
||||||
|
viz = GraphVisualizer(G)
|
||||||
|
|
||||||
|
# Easily swap algorithms by passing different generator objects
|
||||||
|
print("Running Breadth-First Search...")
|
||||||
|
bfs_gen = bfs_algorithm(G, 'A')
|
||||||
|
viz.run_animation(bfs_gen, title="Breadth-First Search (BFS)", delay=1.0)
|
||||||
|
|
||||||
|
print("Running Depth-First Search...")
|
||||||
|
dfs_gen = recursive_dfs_algorithm(G, 'A')
|
||||||
|
viz.run_animation(dfs_gen, title="Depth-First Search (DFS)", delay=1.0)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
a = set("abracadabra")
|
||||||
|
print(a)
|
||||||
|
b = {"a", "b", "c"}
|
||||||
|
print(a & b)
|
||||||
|
print(a | b)
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
def main():
|
||||||
|
find_substring("123456789", 3)
|
||||||
|
|
||||||
|
|
||||||
|
def find_substring(string: str, start: int, end: int = None):
|
||||||
|
"""
|
||||||
|
Returns a substring of a given string and returns it as a string
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if end is None:
|
||||||
|
end = len(string)-1
|
||||||
|
|
||||||
|
result = ""
|
||||||
|
|
||||||
|
while start <= end:
|
||||||
|
result += string[start]
|
||||||
|
start += 1
|
||||||
|
print(result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
except IndexError:
|
||||||
|
print("Index out of range")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
|
||||||
|
class Game:
|
||||||
|
|
||||||
|
SIZE = (500, 500)
|
||||||
|
FPS = 30
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.screen = None
|
||||||
|
self.clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self.init_game()
|
||||||
|
self.game_loop()
|
||||||
|
self.exit_game()
|
||||||
|
|
||||||
|
def init_game(self):
|
||||||
|
random.seed()
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
def set_screen(self):
|
||||||
|
if self.screen is None:
|
||||||
|
self.screen = pygame.display.set_mode(Game.SIZE)
|
||||||
|
self.screen.fill((0, 0, 0))
|
||||||
|
return self.screen
|
||||||
|
|
||||||
|
def game_loop(self):
|
||||||
|
while True:
|
||||||
|
if not self.event_handling():
|
||||||
|
break
|
||||||
|
if not self.update_game():
|
||||||
|
break
|
||||||
|
self.draw_game()
|
||||||
|
self.clock.tick(self.FPS)
|
||||||
|
|
||||||
|
def event_handling(self):
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
keys = pygame.key.get_pressed()
|
||||||
|
# TODO: implement key events
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_game(self):
|
||||||
|
self.draw_game()
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_game(self):
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def exit_game(self):
|
||||||
|
pygame.quit()
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import pygame
|
||||||
|
from _game import Game
|
||||||
|
from compute import Pi, cos, sin
|
||||||
|
|
||||||
|
|
||||||
|
class Apollo(Game):
|
||||||
|
"""A small red circle orbiting the moon in the opposite direction."""
|
||||||
|
|
||||||
|
def __init__(self, earth_x, earth_y):
|
||||||
|
super().__init__()
|
||||||
|
self.center_x = earth_x
|
||||||
|
self.center_y = earth_y
|
||||||
|
# Moon orbit parameters
|
||||||
|
self.moon_theta = 0.05
|
||||||
|
self.moon_orbit_radius = 80
|
||||||
|
self.moon_radius = 5
|
||||||
|
# Apollo orbit parameters (opposite direction)
|
||||||
|
self.apollo_theta = -0.04
|
||||||
|
self.apollo_orbit_radius = 30
|
||||||
|
self.apollo_radius = 3
|
||||||
|
# Colors
|
||||||
|
self.earth_color = (0, 0, 255)
|
||||||
|
self.moon_color = (255, 255, 255)
|
||||||
|
self.apollo_color = (255, 0, 0)
|
||||||
|
self.screen = None
|
||||||
|
|
||||||
|
def update_game(self):
|
||||||
|
# Update moon angle
|
||||||
|
self.moon_theta += 0.02
|
||||||
|
if self.moon_theta > 2 * Pi:
|
||||||
|
self.moon_theta -= 2 * Pi
|
||||||
|
# Calculate moon position
|
||||||
|
moon_x = int(self.center_x + self.moon_orbit_radius * cos(self.moon_theta))
|
||||||
|
moon_y = int(self.center_y + self.moon_orbit_radius * sin(self.moon_theta))
|
||||||
|
|
||||||
|
# Update Apollo angle (opposite direction)
|
||||||
|
self.apollo_theta -= 0.02
|
||||||
|
if abs(self.apollo_theta) > 2 * Pi:
|
||||||
|
self.apollo_theta += 2 * Pi
|
||||||
|
# Calculate Apollo position relative to moon
|
||||||
|
apollo_x = int(moon_x + self.apollo_orbit_radius * cos(self.apollo_theta))
|
||||||
|
apollo_y = int(moon_y + self.apollo_orbit_radius * sin(self.apollo_theta))
|
||||||
|
|
||||||
|
# Draw everything
|
||||||
|
screen = self.set_screen()
|
||||||
|
screen.fill((0, 0, 0))
|
||||||
|
# Earth
|
||||||
|
pygame.draw.circle(screen, self.earth_color, (self.center_x, self.center_y), 20)
|
||||||
|
# Moon
|
||||||
|
pygame.draw.circle(screen, self.moon_color, (moon_x, moon_y), self.moon_radius)
|
||||||
|
# Apollo
|
||||||
|
pygame.draw.circle(screen, self.apollo_color, (apollo_x, apollo_y), self.apollo_radius)
|
||||||
|
pygame.display.flip()
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_game(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
apollo = Apollo(250, 250)
|
||||||
|
apollo.run()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,143 @@
|
|||||||
|
"""Utility module providing basic matrix operations.
|
||||||
|
|
||||||
|
Functions
|
||||||
|
---------
|
||||||
|
- :func:`matmul` – Multiply two matrices.
|
||||||
|
- :func:`transpose` – Transpose a matrix.
|
||||||
|
- :func:`rotate` – Rotate a matrix 90° clockwise.
|
||||||
|
|
||||||
|
All functions work on nested Python lists and perform basic
|
||||||
|
validation before operation.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
Matrix = list[list[float]]
|
||||||
|
Pi = 3.141592653589793
|
||||||
|
|
||||||
|
def sin(x: float) -> float:
|
||||||
|
"""Approximate sine using Taylor series up to 7th order."""
|
||||||
|
term = x
|
||||||
|
sum_ = term
|
||||||
|
for n in range(1, 7):
|
||||||
|
term *= -x * x / ((2 * n) * (2 * n + 1))
|
||||||
|
sum_ += term
|
||||||
|
return sum_
|
||||||
|
|
||||||
|
def cos(x: float) -> float:
|
||||||
|
"""Approximate cosine using Taylor series up to 7th order."""
|
||||||
|
term = 1.0
|
||||||
|
sum_ = term
|
||||||
|
for n in range(1, 7):
|
||||||
|
term *= -x * x / ((2 * n - 1) * (2 * n))
|
||||||
|
sum_ += term
|
||||||
|
return sum_
|
||||||
|
def main():
|
||||||
|
a = [[1, 2, 3], [4, 5, 6]]
|
||||||
|
b = [[7, 8], [10, 11], [12, 13]]
|
||||||
|
print(matmul(a,b))
|
||||||
|
print(rotate(a))
|
||||||
|
print(transpose(a))
|
||||||
|
|
||||||
|
def matmul(a: Matrix, b: Matrix) -> Matrix:
|
||||||
|
"""Return the product of two matrices.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
a: Matrix
|
||||||
|
Left‑hand matrix (shape ``m × n``).
|
||||||
|
b: Matrix
|
||||||
|
Right‑hand matrix (shape ``n × p``).
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
Matrix
|
||||||
|
Resulting matrix of shape ``m × p``.
|
||||||
|
|
||||||
|
Raises
|
||||||
|
------
|
||||||
|
ValueError
|
||||||
|
If the inner dimensions of the matrices do not match, or if
|
||||||
|
either argument is empty.
|
||||||
|
"""
|
||||||
|
if not a or not a[0] or not b or not b[0]:
|
||||||
|
raise ValueError("Input matrices must be non‑empty")
|
||||||
|
|
||||||
|
n_cols_a = len(a[0])
|
||||||
|
n_rows_b = len(b)
|
||||||
|
if n_cols_a != n_rows_b:
|
||||||
|
raise ValueError(
|
||||||
|
f"Inner dimensions must agree for matrix multiplication: "
|
||||||
|
f"{len(a)}×{n_cols_a} and {n_rows_b}×{len(b[0])}"
|
||||||
|
)
|
||||||
|
|
||||||
|
m = len(a)
|
||||||
|
p = len(b[0])
|
||||||
|
result: Matrix = [[0.0 for _ in range(p)] for _ in range(m)]
|
||||||
|
|
||||||
|
for i in range(m):
|
||||||
|
for j in range(p):
|
||||||
|
total = 0.0
|
||||||
|
for k in range(n_cols_a):
|
||||||
|
total += a[i][k] * b[k][j]
|
||||||
|
result[i][j] = total
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def transpose(matrix: Matrix) -> Matrix:
|
||||||
|
"""Return the transpose of a matrix.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
matrix: Matrix
|
||||||
|
A matrix represented as a list of row lists.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
Matrix
|
||||||
|
The transposed matrix.
|
||||||
|
|
||||||
|
Raises
|
||||||
|
------
|
||||||
|
ValueError
|
||||||
|
If the matrix is empty or rows are not of equal length.
|
||||||
|
"""
|
||||||
|
if not matrix or not matrix[0]:
|
||||||
|
raise ValueError("Input matrix must be non‑empty")
|
||||||
|
|
||||||
|
row_length = len(matrix[0])
|
||||||
|
for row in matrix:
|
||||||
|
if len(row) != row_length:
|
||||||
|
raise ValueError("All rows in the matrix must have the same length")
|
||||||
|
|
||||||
|
return [list(col) for col in zip(*matrix)]
|
||||||
|
|
||||||
|
|
||||||
|
def rotate(matrix: Matrix) -> Matrix:
|
||||||
|
"""Return the matrix rotated 90 degrees clockwise.
|
||||||
|
|
||||||
|
This is equivalent to transposing the matrix and then reversing
|
||||||
|
each row.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
matrix: Matrix
|
||||||
|
A matrix represented as a list of row lists.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
Matrix
|
||||||
|
The rotated matrix.
|
||||||
|
|
||||||
|
Raises
|
||||||
|
------
|
||||||
|
ValueError
|
||||||
|
If the matrix is empty or rows are not of equal length.
|
||||||
|
"""
|
||||||
|
transposed = transpose(matrix)
|
||||||
|
return [list(reversed(row)) for row in transposed]
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
||||||
|
"""End of module"""
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
import pygame
|
||||||
|
from _game import Game
|
||||||
|
from compute import Pi, cos, sin
|
||||||
|
|
||||||
|
|
||||||
|
class Moon(Game):
|
||||||
|
def __init__(self, x, y):
|
||||||
|
super().__init__()
|
||||||
|
self.center_x = x
|
||||||
|
self.center_y = y
|
||||||
|
self.theta = 0.05
|
||||||
|
self.orbit_radius = 80
|
||||||
|
self.moon_radius = 5
|
||||||
|
self.white_color = (255, 255, 255)
|
||||||
|
self.blue_color = (0, 0, 255)
|
||||||
|
self.screen = None
|
||||||
|
|
||||||
|
def update_game(self):
|
||||||
|
# Increment angle
|
||||||
|
self.theta += 0.02
|
||||||
|
if self.theta > 2 * Pi:
|
||||||
|
self.theta -= 2 * Pi
|
||||||
|
# Compute moon position on orbit using trigonometry
|
||||||
|
mx = int(self.center_x + self.orbit_radius * cos(self.theta))
|
||||||
|
my = int(self.center_y + self.orbit_radius * sin(self.theta))
|
||||||
|
|
||||||
|
# Ensure screen is initialized
|
||||||
|
screen = self.set_screen()
|
||||||
|
# Clear screen
|
||||||
|
screen.fill((0, 0, 0))
|
||||||
|
# Draw central blue circle
|
||||||
|
pygame.draw.circle(screen, self.blue_color, (self.center_x, self.center_y), 20)
|
||||||
|
# Draw orbiting white moon
|
||||||
|
pygame.draw.circle(screen, self.white_color, (mx, my), self.moon_radius)
|
||||||
|
# Flip display
|
||||||
|
pygame.display.flip()
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_game(self):
|
||||||
|
# Override Game.draw_game to prevent double flip
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
moon = Moon(250, 250)
|
||||||
|
moon.run()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import tkinter as tk
|
||||||
|
|
||||||
|
|
||||||
|
class AppWindow:
|
||||||
|
def __init__(self,width,height,title="AppWindow"):
|
||||||
|
self.root = tk.Tk()
|
||||||
|
self.root.title(title)
|
||||||
|
self.root.geometry(f"{width}x{height}")
|
||||||
|
def run(self):
|
||||||
|
self.init_widgets()
|
||||||
|
self.root.mainloop()
|
||||||
|
self.root.close()
|
||||||
|
def init_widgets(self):
|
||||||
|
pass
|
||||||
|
def close(self):
|
||||||
|
pass
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
|
||||||
|
class Game:
|
||||||
|
|
||||||
|
SIZE = (500, 500)
|
||||||
|
FPS = 30
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.screen = None
|
||||||
|
self.clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self.init_game()
|
||||||
|
self.game_loop()
|
||||||
|
self.exit_game()
|
||||||
|
|
||||||
|
def init_game(self):
|
||||||
|
random.seed()
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
def set_screen(self):
|
||||||
|
if self.screen is None:
|
||||||
|
self.screen = pygame.display.set_mode(Game.SIZE)
|
||||||
|
self.screen.fill((0, 0, 0))
|
||||||
|
return self.screen
|
||||||
|
|
||||||
|
def game_loop(self):
|
||||||
|
while True:
|
||||||
|
if not self.event_handling():
|
||||||
|
break
|
||||||
|
if not self.update_game():
|
||||||
|
break
|
||||||
|
self.draw_game()
|
||||||
|
self.clock.tick(self.FPS)
|
||||||
|
|
||||||
|
def event_handling(self):
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
keys = pygame.key.get_pressed()
|
||||||
|
# TODO: implement key events
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_game(self):
|
||||||
|
# TODO: implement game logic
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_game(self):
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def exit_game(self):
|
||||||
|
pygame.quit()
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
class Node:
|
||||||
|
def __init__(self,data):
|
||||||
|
self.data = data
|
||||||
|
self.adjacent_nodes = list()
|
||||||
|
|
||||||
|
def add_adjacency(self,node):
|
||||||
|
self.adjacent_nodes.append(node)
|
||||||
|
|
||||||
|
class Graph:
|
||||||
|
def __init__(self):
|
||||||
|
self.nodes = list()
|
||||||
|
|
||||||
|
def add_node(self,data):
|
||||||
|
node = Node(data)
|
||||||
|
self.nodes.append(node)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
for _ in range (10):
|
||||||
|
_ = Node(random.randint(0,100))
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
class Particle:
|
||||||
|
def __init__(self,x,y,color,fade_speed):
|
||||||
|
self.pos = (x,y)
|
||||||
|
self.velocity = (0,0)
|
||||||
|
self.accel = (0,0)
|
||||||
|
self.surface = None
|
||||||
|
self.color = color
|
||||||
|
self.fade_speed = fade_speed
|
||||||
|
self.alpha = 255
|
||||||
|
|
||||||
|
def set_surface(self,surface):
|
||||||
|
self.surface = surface
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self.velocity = (self.velocity[0]+self.accel[0], self.velocity[1]+self.accel[1])
|
||||||
|
self.pos = (self.pos[0] + self.velocity[0], self.pos[1] + self.velocity[1])
|
||||||
|
self.accel = (0,0)
|
||||||
|
|
||||||
|
def apply_force(self,force):
|
||||||
|
self.accel = (self.accel[0]+force[0],self.accel[1]+force[1])
|
||||||
|
|
||||||
|
def fade(self):
|
||||||
|
self.alpha -= self.fade_speed
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
from my_package.Particle_class import Particle
|
||||||
|
|
||||||
|
|
||||||
|
class Spark(Particle):
|
||||||
|
def __init__(self,x,y,color, circle_rad,fade_speed=4):
|
||||||
|
super().__init__(x,y,color,fade_speed)
|
||||||
|
self.y = y
|
||||||
|
self.rad = circle_rad
|
||||||
|
self.image = pygame.Surface((self.rad * 2, self.rad * 2), pygame.SRCALPHA)
|
||||||
|
pygame.draw.circle(self.image, self.color, (self.rad, self.rad), self.rad)
|
||||||
|
|
||||||
|
def appear(self, screen):
|
||||||
|
if self.alpha > 0:
|
||||||
|
self.image.set_alpha(self.alpha)
|
||||||
|
screen.blit(self.image, (self.pos[0] - self.rad, self.pos[1] - self.rad))
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://opencode.ai/config.json",
|
||||||
|
"provider": {
|
||||||
|
"thn": {
|
||||||
|
"npm": "@ai-sdk/openai-compatible",
|
||||||
|
"options": {
|
||||||
|
"baseURL": "http://141.75.33.244:8000/v1",
|
||||||
|
"apiKey": "sk-QXj1p40XOwNSze_5-eYrBzVh3qIgAfqoWPkAjNVqOS8"
|
||||||
|
},
|
||||||
|
"models": {
|
||||||
|
"gpt-oss:20b": { "name": "GPT-OSS 20B" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"model": "thn/gpt-oss:20b"
|
||||||
|
}
|
||||||
Reference in new issue
Block a user