diff --git a/README.md b/README.md index 8d22be9..b1f55c7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,15 @@ # starfield -A starfield simulation using python 3 and tkinter +A colorized, readily re-sizable starfield simulation using Python 3 & Tkinter. -![anim](https://raw.githubusercontent.com/davidejones/starfield/master/anim.gif) +If you would like to learn how to use Python to create such graphics, then [you might enjoy this free training](https://www.udemy.com/course/introduction-to-turtle-graphics). + +Also feel free to enjoy the free tutorial @[Soft9000.com](https://soft9000.com/PY3KTG/index.html). + +![Larger, colorized, stars:](https://github.com/Python3-Training/starfield/blob/master/stars.gif) + +p.s. If you would like to plack (play + hack -=> Kobayashi Maru?) a game then you might enjoy [StarTrek for Python](https://github.com/Python3-Training/startrek1971). + +## zSupport? +If you want to support the effort, I seek no donations. Instead, simply feel free to purchase one of [my educational](https://www.udemy.com/user/randallnagy2/) or [printed](https://www.amazon.com/Randall-Nagy/e/B08ZJLH1VN?ref=sr_ntt_srch_lnk_1&qid=1660050704&sr=8-1) productions? diff --git a/main.py b/main.py index f21f99f..34e16bc 100644 --- a/main.py +++ b/main.py @@ -1,19 +1,42 @@ +#!/usr/bin/env python3 import math from tkinter import Tk, Canvas, mainloop from random import randrange +# 2020/09/15: Cloned. Updated to add a weighted colorization. -Rn + +STAR_SIZE = 12 + +COLOR_WHITE = 0 +COLOR_RED = 1 +COLOR_BLUE = 2 +COLOR_GREEN = 3 +COLOR_YELLOW= 4 class Star: - __slots__ = ['x', 'y', 'z', 'id', 'radius', 'fill'] + __slots__ = ['x', 'y', 'z', 'id', 'radius', 'r', 'g', 'b'] - def __init__(self, x, y, z) -> None: + def __init__(self, x, y, z, color) -> None: super().__init__() self.id = None self.x = x self.y = y self.z = z self.radius = 1 - self.fill = 0 + self.r = 255 + self.g = 255 + self.b = 255 + if color == COLOR_RED: + self.g = 0 + self.b = 0 + elif color == COLOR_GREEN: + self.r = 0 + self.b = 0 + elif color == COLOR_BLUE: + self.r = 0 + self.g = 0 + elif color == COLOR_YELLOW: + self.b = 0 class StarField: @@ -33,9 +56,11 @@ def __init__(self, width, height, depth=32, num_stars=500): self.canvas.pack() for x in range(num_stars): + color = randrange(30) star = Star(x=randrange(-self.width, self.width), y=randrange(-self.height, self.height), - z=randrange(1, self.max_depth)) + z=randrange(1, self.max_depth), + color=color) star.id = self.canvas.create_oval(star.x - star.radius, star.y - star.radius, star.x + star.radius, star.y + star.radius, fill='#FFFFFF') self.stars.append(star) @@ -46,8 +71,7 @@ def draw(self): for star in self.stars: # move depth star.z -= 0.19 - star.radius = (1 - float(star.z) / self.max_depth) * 1.7 - star.fill = int((1 - float(star.z) / self.max_depth) * 255) + star.radius = (1 - float(star.z) / self.max_depth) * STAR_SIZE # reset depth if star.z <= 0: @@ -55,7 +79,6 @@ def draw(self): star.y = randrange(-self.height, self.height) star.z = self.max_depth star.radius = 1 - star.fill = 0 # Transforms this 3D point to 2D using a perspective projection. factor = self.fov / (self.view_distance + star.z) @@ -63,9 +86,9 @@ def draw(self): y = -star.y * factor + self.height / 2 self.canvas.coords(star.id, x - star.radius, y - star.radius, x + star.radius, y + star.radius) - self.canvas.itemconfig(star.id, fill='#%02x%02x%02x' % (star.fill, star.fill, star.fill)) + self.canvas.itemconfig(star.id, fill='#%02x%02x%02x' % (star.r, star.g, star.b)) self.canvas.after(30, self.draw) if __name__ == '__main__': - s = StarField(800, 600) + s = StarField(1290, 730) diff --git a/stars.gif b/stars.gif new file mode 100644 index 0000000..772a99b Binary files /dev/null and b/stars.gif differ diff --git a/stars.mp4 b/stars.mp4 new file mode 100644 index 0000000..3b7f668 Binary files /dev/null and b/stars.mp4 differ