Skip to content

Commit

Permalink
Prettify the Tron example
Browse files Browse the repository at this point in the history
  • Loading branch information
lordmauve committed Oct 9, 2019
1 parent f4c01eb commit 18bb48e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 17 deletions.
55 changes: 47 additions & 8 deletions examples/tron/bike.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/tron/fonts/License.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Copyright (c) 2010, Jeff Bell [www.randombell.com] | [jeffbell@randombell.com].
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is available with a FAQ at: http://scripts.sil.org/OFL
Binary file added examples/tron/fonts/tr2n.ttf
Binary file not shown.
Binary file modified examples/tron/images/bike.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 17 additions & 9 deletions examples/tron/tron.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pygame import Surface
from pygame import transform
from collections import deque

WIDTH = 800
HEIGHT = 800
Expand All @@ -26,7 +27,7 @@ def grid_to_screen(x, y):


speed = GRID_SIZE
bike = Actor('bike', anchor_x=24)
bike = Actor('bike', anchor_x=28)

# Cardinal directions -> (angle, velocity, reverse)
DIRECTIONS = {
Expand All @@ -42,6 +43,7 @@ def reset_bike():
bike.pos = (WIDTH + GRID_SIZE) // 2, (HEIGHT + GRID_SIZE) // 2
bike.dead = False
bike.angle, bike.v, bike.reverse = DIRECTIONS[keys.RIGHT]
bike.trail = deque(maxlen=200)


def kill_bike():
Expand All @@ -54,6 +56,12 @@ def kill_bike():


def update():
# Fade down the trail
for t in bike.trail:
r, g, b, *_ = trails.get_at(t)
c = round(g * 0.99)
trails.set_at(t, (round(r * 0.97), c, c))

if bike.dead:
bike.explosion_radius += 20
return
Expand All @@ -66,17 +74,18 @@ def update():
trail_pos = screen_to_grid(x, y)

try:
current_value = trails.get_at(trail_pos)
current_value = trails.get_at(trail_pos)[2]
except IndexError:
# Out of bounds! we crashed
kill_bike()
return

if current_value == CYAN:
if current_value:
# We've already set this pixel, so this is a crash
kill_bike()
else:
trails.set_at(trail_pos, CYAN)
trails.set_at(trail_pos, (255, 255, 255))
bike.trail.append(trail_pos)


def draw():
Expand All @@ -88,12 +97,11 @@ def draw():
color=CYAN,
)
screen.draw.text(
'You are dead!\nPress SPACE to restart.',
'YOU ARE DEREZZED!\nPRESS SPACE TO RESTART',
center=(WIDTH // 2, 100),
color=(0, 30, 60),
fontsize=70,
owidth=0.1,
ocolor=(100, 255, 255),
color=CYAN,
fontsize=50,
fontname="tr2n"
)
else:
bike.draw()
Expand Down

0 comments on commit 18bb48e

Please sign in to comment.