Shot/main.py

142 lines
4.6 KiB
Python
Raw Permalink Normal View History

2024-02-27 18:33:30 +01:00
#!/usr/bin/python3
import pygame
from math import radians,sin,cos
2024-02-27 21:17:59 +01:00
import levels
2024-02-27 18:33:30 +01:00
pygame.init()
default_font = pygame.font.SysFont("sans", 14)
2024-02-27 21:17:59 +01:00
def get_debug_text():
def text(text,line_counter):
debug_text = default_font.render(str(text), True, (240, 240, 240))
screen.blit(debug_text,(0,line_counter*default_font.get_height()))
line_counter = 1
globalvars =globals()
for a in globalvars:
text(f"{str(a)} = {str(globalvars[a])}",line_counter)
line_counter += 1
2024-02-27 20:23:23 +01:00
def bubble_sort(liste):
n = len(liste)
for i in range(n):
for j in range(n - i - 1):
if liste[j] > liste[j + 1]:
liste[j], liste[j + 1] = liste[j + 1], liste[j]
2024-02-27 21:17:59 +01:00
def make_level(level):
for a in levels.levels[level][1]:
arrows.append(Arrow(a,circle_pos,circle_radius))
return levels.levels[level][0]
2024-02-27 20:23:23 +01:00
class Arrow():
def __init__(self,winkel,pos,radius) -> None:
self.radius = radius
self.winkel = winkel
self.pos = pos
self.länge = 60
def draw(self):
radian = radians(self.winkel+rotate)
pygame.draw.line(screen, (255, 255, 255),
(self.pos[0] + sin(radian) * self.radius, self.pos[1] + cos(radian) * self.radius),
(self.pos[0] + sin(radian) * (self.radius + self.länge), self.pos[1] + cos(radian) * (self.radius + self.länge)),
6)
pygame.draw.circle(screen,color,(self.pos[0] + sin(radian) * (self.radius + self.länge), self.pos[1] + cos(radian) * (self.radius + self.länge)),7)
def sind_winkel_nahe(self,winkel,toleranz=3):
winkel1 = winkel % 360
winkel2 = self.winkel % 360
differenz = abs(winkel1 - winkel2)
if differenz <= toleranz or differenz >= 360 - toleranz:return True
else:return False
class Bow():
def __init__(self,pos) -> None:
self.cooldown = 2
self.pos = pos
2024-02-27 21:17:59 +01:00
self.posa = self.pos
self.animate = False
2024-02-27 20:23:23 +01:00
def draw(self):
pygame.draw.line(screen, (255, 255, 255),
2024-02-27 21:17:59 +01:00
self.posa,
(self.posa[0]+60, self.posa[1]),6)
pygame.draw.circle(screen,color,(self.posa[0]+60, self.posa[1]),7)
def update(self):
if self.animate:
x,y = self.posa
x += 40
self.posa = x,y
if x >= circle_pos[0]-60:
self.animate = False
self.posa = self.pos
arrows.append(Arrow(-90-rotate,circle_pos,circle_radius))
self.draw()
2024-02-27 20:23:23 +01:00
def shot(self):
2024-02-27 21:17:59 +01:00
self.animate = True
2024-02-27 18:33:30 +01:00
fill_screen = True
show_debug = False
2024-02-27 21:17:59 +01:00
screen = pygame.display.set_mode((900,500),pygame.RESIZABLE)
2024-02-27 18:33:30 +01:00
pygame.display.set_caption("Shot")
screensize = pygame.display.get_window_size()
clock = pygame.time.Clock()
2024-02-27 21:17:59 +01:00
circle_pos = screensize[0]//5*4,screensize[1]//2
circle_radius = 110
2024-02-27 20:23:23 +01:00
rotate = 0
rotate_speed = 1
2024-02-27 21:17:59 +01:00
bow = Bow((screensize[0]//5,screensize[1]//2))
2024-02-27 20:23:23 +01:00
arrows = []
bg_color = (0,0,0)
color = (255,255,255)
2024-02-27 21:17:59 +01:00
level = 0
to_shot = make_level(0)
2024-02-27 18:33:30 +01:00
running = True
if __name__ == "__main__":
while running:
2024-02-27 21:17:59 +01:00
arrow_angles = []
2024-02-27 18:33:30 +01:00
if fill_screen:
2024-02-27 20:23:23 +01:00
screen.fill(bg_color)
2024-02-27 18:33:30 +01:00
pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.K_ESCAPE]:
exit()
2024-02-27 20:23:23 +01:00
if pressed_keys[pygame.K_a] and not preview_pressed_keys[pygame.K_a]:
2024-02-27 18:33:30 +01:00
fill_screen = not fill_screen
2024-02-27 20:23:23 +01:00
if pressed_keys[pygame.K_SPACE] and not preview_pressed_keys[pygame.K_SPACE]:
bow.shot()
2024-02-27 18:33:30 +01:00
if pressed_keys[pygame.K_d] and not preview_pressed_keys[pygame.K_d]:
show_debug = not show_debug
preview_pressed_keys = pressed_keys
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.WINDOWRESIZED:
screensize = pygame.display.get_window_size()
2024-02-27 20:23:23 +01:00
if event.type == pygame.WINDOWRESIZED:
screensize = pygame.display.get_window_size()
2024-02-27 21:17:59 +01:00
circle_pos = screensize[0]//5*4,screensize[1]//2
bow.pos = screensize[0]//5,screensize[1]//2
bow.posa = screensize[0]//5,screensize[1]//2
2024-02-27 20:23:23 +01:00
for a in arrows:
a.pos = circle_pos
2024-02-27 21:17:59 +01:00
bow.update()
2024-02-27 20:23:23 +01:00
pygame.draw.circle(screen,color,circle_pos,circle_radius)
for a in arrows:
a.draw()
2024-02-27 21:17:59 +01:00
arrow_angles.append(a.winkel)
2024-02-27 20:23:23 +01:00
rotate += 2
2024-02-27 18:33:30 +01:00
if show_debug:
get_debug_text()
pygame.display.flip()
clock.tick(60)