line_moving.py

Created by wperez274

Created on May 02, 2025

973 Bytes

Rotating lines like in the classic NES Super Mario brothers.


import kandinsky as kd
import math
import time

def draw_line(x0, y0, length, angle_deg, color=(0, 0, 0)):
    angle_rad = math.radians(angle_deg)
    x1 = int(x0 + length * math.cos(angle_rad))
    y1 = int(y0 - length * math.sin(angle_rad))
    steps = max(abs(x1 - x0), abs(y1 - y0))
    for i in range(steps + 1):
        x = int(x0 + (x1 - x0) * i / steps)
        y = int(y0 + (y1 - y0) * i / steps)
        kd.set_pixel(x, y, color)

def rotate_line(x0, y0, length, color=(0, 0, 0)):
    angle = 0
    while True:
        kd.fill_rect(0, 0, 320, 240, (255, 255, 255))  # Clear screen (white background)
        draw_line(x0, y0, length, angle, color)  # Draw the line at the current angle
        angle += 5  # Increment the angle for the next frame
        if angle >= 360:
            angle = 0  # Reset angle after a full circle
        time.sleep(0.05)  # Delay for animation effect

rotate_line(160, 120, 50, (255, 0, 0))  # Rotate a red line

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.