Skip to content

Instantly share code, notes, and snippets.

@horstjens
Created October 29, 2023 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horstjens/0a605db4f652b46f07599c460bf6d810 to your computer and use it in GitHub Desktop.
Save horstjens/0a605db4f652b46f07599c460bf6d810 to your computer and use it in GitHub Desktop.
vo wallsurfer
# gist.github.com/horstjens
import vpython as vp
import random
class Game:
scene = vp.canvas(width=1200, height=650, title="wall surfer")
dt = 0.1
xlimit = 20
ylimit = 10
#player = vp.cone(color=vp.color.red,
# pos=vp.vector(4,0,14),
# axis=vp.vector(0,0,-2.5),
# radius=0.75)
player = vp.pyramid(
color=vp.color.red,
pos=vp.vector(4,0,22), # start far away
size=vp.vector(1.75,0.5,2),
axis=vp.vector(0,0,-2.5),
)
player.xyspeed = 1.4
player.zspeed = -1
hp = 500 # hitpoints
label_hp = vp.label(pos=vp.vector(10, 600,0),
pixel_pos=True,
text="hp: 500",
color=vp.color.yellow,
align = "left",
font="System",
height=32,
box=False)
cameradistance = 1
startdistance = 4
zdistance = 10
xdistance = 5
walls = []
def f():
return vp.vector(Game.player.pos.x, Game.player.pos.y+5, Game.player.pos.z +Game.cameradistance)
Game.scene.autoscale = False
Game.scene.range = 10 # change this value to change zoom
#Game.scene.camera.pos=vp.vector(Game.player.pos.x, Game.player.pos.y+2, Game.player.pos.z+Game.startdistance)
Game.scene.center = Game.player.pos + vp.vector(0,0,-1) #??
#Game.scene.camera.follow(Game.player)
Game.scene.camera.follow(f)
y=0
for z in range(0,1001,Game.zdistance):
row = [True, False]
for _ in range(2):
row.append(random.choice((True, False)))
random.shuffle(row)
Game.walls.append(row)
for i, x in enumerate(range(0, Game.xlimit, Game.xdistance)):
if row[i]:
#for y in range(0, Game.ylimit+1,5):
# four pieces, one garanteed False, one garanteed True
#if random.random() < 0.5:
vp.box(pos=vp.vector(x,y,-z),
size=vp.vector(3,3,1),
color=vp.vector(abs(z/1000),0.3,0.3),
)
#
#
while Game.hp > 0:
Game.player.move = vp.vector(0,0,Game.player.zspeed) #
vp.rate(30)
keys = vp.keysdown()
#if "w" in keys:
# Game.player.move.y = Game.player.xyspeed
#if "s" in keys:
# Game.player.move.y = -Game.player.xyspeed
if "a" in keys:
Game.player.move.x = -Game.player.xyspeed #
if "d" in keys:
Game.player.move.x = Game.player.xyspeed #
Game.player.pos += Game.player.move * Game.dt
#player.move *= 0.999
# -------- player boundary -----
if Game.player.pos.x < 0:
Game.player.pos.x = 0
#if Game.player.pos.y < 0:
# Game.player.pos.y = 0
if Game.player.pos.x > Game.xlimit-Game.xdistance:
Game.player.pos.x = Game.xlimit-Game.xdistance
#if Game.player.pos.y > Game.ylimit:
# Game.player.pos.y = Game.ylimit
# ------- collision detection ----------
previous_i = -Game.player.pos.z // Game.zdistance -1
current_i = -Game.player.pos.z // Game.zdistance
next_i = -Game.player.pos.z // Game.zdistance +1
#print(previous_i, current_i, next_i)
Game.player.color = vp.color.red
#for i in (
# previous_i,
# current_i,
# next_i):
i = next_i
if i <= 0:
continue
#if i == previous_i:
# continue
#if i == current_i :
# continue
if abs(Game.player.pos.z - (Game.zdistance * -i)) < 0.5:
row = Game.walls[int(i)]
for i, x in enumerate(range(0, Game.xlimit, Game.xdistance)):
if row[int(i)]:
if abs(Game.player.pos.x - x) < (1.5+1.75):
Game.player.color = vp.color.yellow
Game.hp -= 1
Game.label_hp.text = f"hp: {Game.hp}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment