dinooos.py

Created by 4223

Created on September 15, 2025

4.76 KB

rennen


# Type y# Dino Run (Chrome Dino-style) voor NumWorks
# Controls: OK of UP om te springen

from kandinsky import fill_rect, draw_string
from ion import keydown, KEY_UP, KEY_OK
from time import monotonic, sleep
from random import randrange, choice

# Schermgrootte (NumWorks bruikbaar gebied)
SCREEN_W = 320
SCREEN_H = 222

# Kleuren
WHITE = (255,255,255)
BLACK = (0,0,0)
GREEN = (76,175,80)
BROWN = (110,70,30)
DARKGREEN = (34,139,34)

# Grond
GROUND_Y = 170

# Dino (T-Rex)
DINO_X = 30
DINO_W = 24
DINO_H = 24

# Fysica
GRAVITY = 0.9
JUMP_V = -10

# Obstakel instellingen
OBSTACLE_MIN_GAP = 80
OBSTACLE_MAX_GAP = 160
OBSTACLE_SPEED_START = 3
OBSTACLE_SPEED_INCREASE = 0.001

# Timing
FRAME_TIME = 0.03


def cls():
    fill_rect(0, 0, SCREEN_W, SCREEN_H, WHITE)


def draw_ground(offset):
    # eenvoudige scrollende grondlijn
    for x in range(0, SCREEN_W, 20):
        if ((x+offset)//20) % 2 == 0:
            fill_rect(x, GROUND_Y, 20, 2, BLACK)


class Dino:
    def __init__(self):
        self.x = DINO_X
        self.y = GROUND_Y - DINO_H
        self.w = DINO_W
        self.h = DINO_H
        self.vy = 0
        self.on_ground = True

    def jump(self):
        if self.on_ground:
            self.vy = JUMP_V
            self.on_ground = False

    def update(self):
        self.vy += GRAVITY
        self.y += self.vy
        if self.y >= GROUND_Y - self.h:
            self.y = GROUND_Y - self.h
            self.vy = 0
            self.on_ground = True

    def draw(self):
        # T-Rex sprite (blokjes)
        # Lichaam
        fill_rect(int(self.x), int(self.y+8), 18, 14, BLACK)
        # Kop
        fill_rect(int(self.x+12), int(self.y), 12, 12, BLACK)
        # Oog
        fill_rect(int(self.x+20), int(self.y+3), 2, 2, WHITE)
        # Poot
        fill_rect(int(self.x+4), int(self.y+20), 6, 4, BLACK)
        fill_rect(int(self.x+12), int(self.y+20), 6, 4, BLACK)

    def bbox(self):
        return (int(self.x), int(self.y), int(self.w), int(self.h))


class Cactus:
    def __init__(self, x, speed):
        self.x = x
        self.speed = speed
        self.w = choice([12, 16, 20])
        self.h = choice([24, 32, 40])
        self.y = GROUND_Y - self.h

    def update(self):
        self.x -= self.speed

    def draw(self):
        # cactus: groene stam + armen
        fill_rect(int(self.x+self.w//3), int(self.y), self.w//3, self.h, DARKGREEN)
        if self.h > 24:
            fill_rect(int(self.x), int(self.y+6), self.w//3, self.h//2, DARKGREEN)
            fill_rect(int(self.x+self.w*2//3), int(self.y+10), self.w//3, self.h//2, DARKGREEN)

    def offscreen(self):
        return self.x + self.w < 0

    def bbox(self):
        return (int(self.x), int(self.y), int(self.w), int(self.h))


def collides(a, b):
    ax, ay, aw, ah = a
    bx, by, bw, bh = b
    return not (ax+aw <= bx or bx+bw <= ax or ay+ah <= by or by+bh <= ay)


def game_loop():
    dino = Dino()
    obstacles = []
    speed = OBSTACLE_SPEED_START
    score = 0
    start_time = monotonic()
    ground_offset = 0

    running = True
    last_frame = monotonic()
    while running:
        now = monotonic()
        last_frame = now

        # input
        if keydown(KEY_UP) or keydown(KEY_OK):
            dino.jump()

        # update
        dino.update()
        for ob in obstacles:
            ob.update()
        obstacles = [ob for ob in obstacles if not ob.offscreen()]

        if not obstacles:
            gap = randrange(OBSTACLE_MIN_GAP, OBSTACLE_MAX_GAP)
            obstacles.append(Cactus(SCREEN_W+gap, speed))
        else:
            last = obstacles[-1]
            if last.x < SCREEN_W - randrange(OBSTACLE_MIN_GAP, OBSTACLE_MAX_GAP):
                obstacles.append(Cactus(SCREEN_W, speed))

        speed += OBSTACLE_SPEED_INCREASE
        for ob in obstacles:
            ob.speed = speed

        for ob in obstacles:
            if collides(dino.bbox(), ob.bbox()):
                running = False

        score = int((monotonic() - start_time) * 10)

        # draw
        cls()
        ground_offset = (ground_offset + int(speed)) % 20
        draw_ground(ground_offset)
        dino.draw()
        for ob in obstacles:
            ob.draw()
        draw_string('Score: {}'.format(score), 4, 4, BLACK)

        sleep(FRAME_TIME)

    # game over
    cls()
    draw_ground(0)
    draw_string('Game Over', 110, 80, BLACK)
    draw_string('Score: {}'.format(score), 110, 100, BLACK)
    draw_string('Druk OK om opnieuw te starten', 40, 140, BLACK)
    while not keydown(KEY_OK):
        sleep(0.05)


while True:
    cls()
    draw_string('Chrome Dino (NumWorks)', 60, 60, BLACK)
    draw_string('OK of UP om te starten', 70, 100, BLACK)
    while not (keydown(KEY_OK) or keydown(KEY_UP)):
        sleep(0.05)
    while keydown(KEY_OK) or keydown(KEY_UP):
        sleep(0.01)
    game_loop()our text here

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.