initial commit
This commit is contained in:
commit
39f56a3b10
83 files changed
+3583
No files matched your search
@@ -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()
|
||||
Reference in new issue
Block a user