from tkinter import * from random import randrange as rand from math import radians, cos, sin def obroc(x, y, sx, sy, a_stopnie): a_rad = radians(a_stopnie) x_nowy = (x - sx) * cos(a_rad) - (y - sy) * sin(a_rad) + sx y_nowy = (x - sx) * sin(a_rad) + (y - sy) * cos(a_rad) + sy return x_nowy, y_nowy def klawisz_wcisniety(event): key = event.keysym.lower() if key in wcisniete_klawisze: wcisniete_klawisze[key] = True def klawisz_puszczony(event): key = event.keysym.lower() if key in wcisniete_klawisze: wcisniete_klawisze[key] = False def gluwna(): rusz() root.after(16, gluwna) def rusz(): global wcisniete_klawisze, mapa_y, mapa_x, obroc_o v = 4 a_rad = radians(obroc_o) dx = v * sin(a_rad) dy = v * cos(a_rad) if wcisniete_klawisze["a"]: obroc_o -= 2 if wcisniete_klawisze["d"]: obroc_o += 2 if wcisniete_klawisze["w"]: if -1500 < mapa_x + dx < 3600 and -800 < mapa_y - dy < 1800: canvas.move("gwiazdy", -dx, dy) canvas.move(koniec_mapy, -dx, dy) mapa_x += dx mapa_y -= dy if wcisniete_klawisze["s"]: if -1500 < mapa_x - dx < 3600 and -800 < mapa_y + dy < 1800: canvas.move("gwiazdy", dx, -dy) canvas.move(koniec_mapy, dx, -dy) mapa_x -= dx mapa_y += dy nowe_punkty = [] for i in range(0, len(statek_bazowy), 2): bx = statek_bazowy[i] by = statek_bazowy[i + 1] nx, ny = obroc(bx, by, srodek_ekranu_x, srodek_ekranu_y, obroc_o) nowe_punkty.extend([nx, ny]) canvas.coords(statek, *nowe_punkty) if wcisniete_klawisze["w"] or wcisniete_klawisze["s"]: r = 6 s1 = canvas.create_oval(srodek_ekranu_x - r, srodek_ekranu_y - r, srodek_ekranu_x + r, srodek_ekranu_y + r, fill="#000000", outline="", tags="gwiazdy") for i in range(100): wartosc_rgb = int((99 - i) * (255 / 99)) he = f"{wartosc_rgb:02X}" kolor = f"#{he}0000" root.after(i * 12, lambda k=kolor: canvas.itemconfig(s1, fill=k)) root.after(1200, lambda: canvas.delete(s1)) canvas.tag_lower(s1) root = Tk() root.geometry("1000x700") root.attributes("-fullscreen", True) canvas = Canvas(root, bg="black", highlightthickness=0) canvas.pack(fill="both", expand=True) koniec_mapy = canvas.create_rectangle(-1900, -1000, 3800, 2000, fill="", outline="red", width=5) for i in range(5000): x = rand(-1900, 3800) y = rand(-1000, 2000) r = 2 g = canvas.create_oval(x, y, x + r, y + r, fill="white", outline="", tags="gwiazdy") wcisniete_klawisze = {"w": False, "s": False, "a": False, "d": False, "space": False} root.update() x_w = canvas.winfo_width() y_w = canvas.winfo_height() srodek_ekranu_x = x_w / 2 srodek_ekranu_y = y_w / 2 mapa_x = srodek_ekranu_x mapa_y = srodek_ekranu_y x = 20 y = 30 statek_bazowy = [srodek_ekranu_x, srodek_ekranu_y - y, srodek_ekranu_x + x, srodek_ekranu_y + y, srodek_ekranu_x, srodek_ekranu_y, srodek_ekranu_x - x, srodek_ekranu_y + y] statek = canvas.create_polygon(*statek_bazowy, fill="lightblue", outline="") obroc_o = 0 root.bind("", klawisz_wcisniety) root.bind("", klawisz_puszczony) root.bind("", lambda event: root.destroy()) gluwna() root.mainloop()