ball.py
Beschleunigter Ball unter Gravitationseinfluss.
Über Pfeiltasten kann er zusätzlich bewegt werden.
Ein Pfeil stellt den Geschwindigkeitsvektor dar.
Ideal als Vorlage für jump-and-run-Varianten....!
ball.py — text/python-source, 1 KB (1437 bytes)
Dateiinhalt
# -*- coding: utf-8 -*-
from visual import *
# Blick auf die Szene
scene.forward=(0,-1,-2)
scene.background=color.white
#Bodenplatte
floor = box(length=4, height=0.0125, width=4, color=color.blue)
# Ball mit Geschwindigkeit und repr. Vektorpfeil
ball = sphere(pos=(0,1,0), color=color.red, opacity=0.8)
ball.velocity = vector(0,-1,0)
pfeil = arrow(pos=ball.pos, axis=ball.velocity, color=color.yellow, shaftwidth=0.25)
# Zeitinkrement
dt = 0.02
# Endlosschleife
while True:
rate(100)
ball.pos = ball.pos + ball.velocity*dt
# Falls Tastatur gedrückt...
if scene.kb.keys:
s = scene.kb.getkey()
print s
if s == 'left': # links
ball.velocity.x += -0.1
elif s == 'right': # right
ball.velocity.x += 0.1
elif s == 'up': # vor
ball.velocity.z -= 0.1
elif s == 'down': # zurück
ball.velocity.z += 0.1
elif s == '0': # zurück auf Ursprung setzen; Geschw. null; Kamera ran
ball.pos = (0,1,0)
ball.velocity = vector(0,0,0)
scene.range = 3
else:
ball.velocity.y = 10
if ball.y < 1 and ball.velocity.y < 0:
# Reduktion der Geschwindigkeit = Dämpfung
ball.velocity.y = -0.65*ball.velocity.y
ball.y = 1
else:
ball.velocity.y = ball.velocity.y - 9.8*dt
pfeil.pos=ball.pos
pfeil.axis=ball.velocity