Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Star_Wars_Trench_Run_Revised_For_PYS60.py -from- http://www.codeskulptor.org/#demos-starwars.py
- # I might add a bit more code soon enough, especially to guide the launching of the torpedoes.
- # The original coding of Star Wars, written on 4th & 5th November 2012 by Steven Knock.
- # May the force be with you all.
- RGB_BLACK=(0,0,0)
- RGB_WHITE=(255,255,255)
- RGB_GRAYLIGHT=(236,236,236)
- RGB_GRAY=(180,180,180)
- RGB_GRAYDARK=(169,169,169)
- RGB_RED=(255,0,0)
- RGB_ORANGE=(255,165,0)
- RGB_YELLOW=(255,255,0)
- RGB_GREEN=(0,128,0)
- RGB_BLUE=(0,0,255)
- RGB_CYAN=(0,255,255)
- RGB_PURPLE=(128,0,128)
- import math
- import random
- #import simplegui
- import time
- from graphics import *
- import appuifw
- import sensor
- import key_codes
- import e32
- VERSION="v1.5"
- CANVAS_WIDTH=640
- CANVAS_HEIGHT=360
- TRENCH_LENGTH_M=3500 ### 700 for quick testing
- TRENCH_WIDTH_M=10
- TRENCH_HEIGHT_M=10
- TRENCH_WALL_INTERVAL_M=20
- TRENCH_COLOUR=RGB_GRAY
- EXHAUST_PORT_POSITION_M=TRENCH_LENGTH_M-100
- EXHAUST_PORT_WIDTH_M=TRENCH_WIDTH_M/3.0
- PROTON_TORPEDO_RANGE_M=200
- PROTON_TORPEDO_RADIUS_M=60
- PROTON_TORPEDO_SPAN_M=200
- LAUNCH_POSITION_M=EXHAUST_PORT_POSITION_M-PROTON_TORPEDO_RANGE_M
- DISTANCE_COLOUR=RGB_RED
- DEATH_STAR_RADIUS=CANVAS_HEIGHT*0.4
- DEATH_STAR_COLOUR=RGB_GRAY
- LINE_WIDTH=2
- MODE_INTRO=0
- MODE_GAME=1
- MODE_VICTORY=2
- MODE_END_DELAY=3
- SHIP_WIDTH_M=1
- SHIP_HEIGHT_M=0.8
- NEAR_PLANE_M=0.1
- FAR_PLANE_M=180.0
- SCALE_WIDTH=CANVAS_WIDTH/2
- SCALE_HEIGHT=CANVAS_HEIGHT/2
- FORWARD_VELOCITY_MS=2
- PROTON_TORPEDO_VELOCITY_MS=FORWARD_VELOCITY_MS*2
- VELOCITY_MAX_MS=15.0
- VELOCITY_DAMPEN=0.7
- ACCELERATION_MSS=0.02
- TORPEDO_COLOUR=RGB_RED
- EXHAUST_PORT_COLOUR=RGB_RED
- INTRO_TEXT_COLOUR=RGB_YELLOW
- WARNING_TEXT_COLOUR=RGB_RED
- PARTICLE_COLOUR=RGB_WHITE
- FONT_STYLE="normal"
- BTN_OFF=(-1,-1), (-1,-1)
- BARRIER_COLOURS=((255, 0, 0), (255, 192, 0), (0, 255, 0), (255, 255, 0), (0, 255, 255), (255, 0, 255))
- BLOCK_VERTEX=((0, 1), (1, 2), (2, 3), (3, 0), (0, 4), (1, 5), (2, 6), (3, 7), (4, 5), (5, 6), (6, 7), (7, 4), (0, 2), (1, 3))
- START_BUTTON=15, 205, CANVAS_WIDTH-15, 305
- LAUNCH_BUTTON=CANVAS_WIDTH-250, 195, CANVAS_WIDTH-15, 305
- # Ship Variables
- pos=[0,0,0]
- vel=FORWARD_VELOCITY_MS
- # Proton Torpedo Variables
- pt_pos=[]
- trp=[0,0,0]
- pt_launch_position=-1
- launch_mode=False
- # Exhaust Port in Range
- reached_launch_position=False
- # Trench Barriers
- barriers=[]
- current_barrier_index=0
- fill_colour=None
- # Message
- message=""
- message_delay=0
- message_tick=0
- # Stars
- stars=[]
- # Victory
- explosion_countdown=0
- particles=[]
- # Game State
- game_mode=MODE_INTRO
- dead=False
- # Actual FPS
- last_time=0
- fps=60
- import appuifw
- import sensor
- def circle(x, y, r, color):
- r=abs(r)
- img.ellipse((x-r, y-r, x+r, y+r), fill=color)
- def draw(rect):
- try:
- canvas.blit(img)
- except: pass
- xyz=(0,0,0)
- def redraw():
- global xyz
- xyz=(sens.x,sens.y,sens.z)
- appuifw.app.orientation='landscape'
- appuifw.app.screen='large'
- appuifw.app.directional_pad=False
- canvas=appuifw.Canvas(redraw_callback=draw)
- appuifw.app.body=canvas
- sens=sensor.AccelerometerXYZAxisData(data_filter=sensor.LowPassFilter())
- sens.set_callback(data_callback=redraw)
- img=Image.new(canvas.size)
- # Return the centre point of the canvas
- def get_canvas_centre():
- return (CANVAS_WIDTH // 2, CANVAS_HEIGHT // 2)
- # Return the centre point of the button
- def get_button_centre(xy):
- x,y,x2,y2=xy
- x,y=x+int((x2-x)/2),y+4 #y+int((y2-y)/2)
- return (x,y)
- # Creates an array of stars randomly scattered on the canvas
- def create_stars():
- while len(stars) < 300:
- x=random.randrange(CANVAS_WIDTH)
- y=random.randrange(CANVAS_HEIGHT)
- stars.append((x, y))
- # Creates all of the barriers that appear in the game. Each Barrier is represented by three elements:
- # Start Position-the distance along the trench that the barrier starts
- # Length-the length of the barrier
- # Blocks-an array of 9 ints, either 1 or 0, that indicate which blocks in a 3x3 square appear in the barrier.
- # The Barriers are placed in a list which is sequenced by the Start Position of the Barriers. This allows
- # the rendering and collision code to consider only the barriers immediately surrounding the ship.
- def create_barriers():
- global barriers
- barriers=[]
- # Determine Start Position and Last Position
- s=150.0
- limit=LAUNCH_POSITION_M-150
- while s < limit:
- # Create a totally solid barrier
- blocks=[]
- for i in range(0, 9):
- blocks.append(1)
- # Punch a number of empty blocks in the barrier, based on how close to the end of the trench the barrier is.
- empty_blocks=1.0-(s/limit)
- empty_blocks=int((empty_blocks*8))+2
- for i in range(0, empty_blocks):
- blocks[random.randrange(9)]=0
- # Calculate a random length
- l=random.randrange(5)+5
- barriers.append((s, l, blocks))
- s += l
- s += 40+random.randrange(30)
- # Initialise all game variables to prepare for a new game
- def init_game():
- global game_mode, pos, vel, pt_pos, pt_launch_position, reached_launch_position, current_barrier_index
- global explosion_countdown, fill_colour, launch_mode, trp,dead
- game_mode=MODE_GAME
- pos=[0, 0, 0]
- trp=[0, 0, 0]
- vel=FORWARD_VELOCITY_MS
- pt_pos=[]
- launch_mode=None
- dead=False
- pt_launch_position=-1
- fill_colour=None
- reached_launch_position=False
- current_barrier_index=0
- explosion_countdown=0
- create_barriers()
- set_message("\n\n\n\n\nUse the Force")
- canvas.bind(key_codes.EButton1Down, start_btn, (BTN_OFF))
- # Calculates the distance remaining until the ship reaches the torpedoes launch position
- def get_distance_to_launch_position():
- return LAUNCH_POSITION_M-pos[2]
- # Indicates whether the ship is 'close' to the launch position.
- def is_close_to_launch_position():
- return math.fabs(get_distance_to_launch_position()) < 100.0
- # Fire the Proton Torpedoes, if they haven't already been launched.
- # They are initially positioned slightly under the ship.
- def launch_proton_torpedoes():
- global pt_launch_position
- canvas.bind(key_codes.EButton1Down, launch_btn, (BTN_OFF))
- if pt_launch_position < 0 and is_close_to_launch_position():
- pt_pos.append([pos[0]-PROTON_TORPEDO_SPAN_M, pos[1]+1, pos[2]])
- pt_pos.append([pos[0]+PROTON_TORPEDO_SPAN_M, pos[1]+1, pos[2]])
- pt_launch_position=pos[2]
- # Sets the current message. The message appears for 3 seconds.
- def set_message(new_message):
- global message, message_delay, message_tick
- message=new_message
- message_delay=3
- message_tick=0
- # Projects a 3d point into a 2d canvas coordinate. The 3d coordinates are based on +x -> right, +y -> down +z -> away.
- # The origin of the 3d coordinate system is the ship's initial position in the middle of the start of the trench.
- def project(point):
- distance=point[2]-pos[2]
- x=(point[0]-pos[0])/(distance+NEAR_PLANE_M)
- y=(point[1]-pos[1])/(distance+NEAR_PLANE_M)
- x *= SCALE_WIDTH
- y *= SCALE_HEIGHT
- x += (CANVAS_WIDTH // 2)
- y += (CANVAS_HEIGHT // 2)
- return (x, y)
- # Displays the Death sequence. If the flashing colours are enabled, then a red rectangle is drawn onto the screen every other frame.
- # Draws the trench. Firstly, four lines are drawn from the player's z position towards the end of the trench.
- # Secondly, a rectangle is drawn at the end of the trench.
- # Thirdly, the lines along the wall are drawn.
- def render_trench():
- if pos[2] < TRENCH_LENGTH_M:
- tw=TRENCH_WIDTH_M // 2
- th=TRENCH_HEIGHT_M // 2
- trench=([-tw, -th], [tw, -th], [tw, th], [-tw, th])
- trench_p=[]
- for t in trench:
- near=list(t)
- near.append(pos[2])
- far=list(t)
- far.append(TRENCH_LENGTH_M)
- near_p=project(near)
- far_p=project(far)
- img.line(((near_p), (far_p)), outline=TRENCH_COLOUR, width=LINE_WIDTH)
- trench_p.append(far_p)
- # Draw far wall
- trench_p.append(trench_p[0])
- if pos[2] < trench_p:
- img.polygon((trench_p), width=LINE_WIDTH, fill=TRENCH_COLOUR)
- # Draw vertical walls
- distance=(int(pos[2]+TRENCH_WALL_INTERVAL_M) // TRENCH_WALL_INTERVAL_M)*TRENCH_WALL_INTERVAL_M
- limit=min(pos[2]+FAR_PLANE_M, TRENCH_LENGTH_M)
- while distance < limit:
- for side in [-1, 1]:
- p1=project((side*tw, -th, distance))
- p2=project((side*tw, th, distance))
- img.line(((p1), (p2)), outline=TRENCH_COLOUR, width=LINE_WIDTH)
- distance += TRENCH_WALL_INTERVAL_M
- # Draws a single barrier.
- def render_barrier(barrier):
- n=barrier[0] # Barrier Start Position
- f=n+barrier[1] # Barrier End Position
- m=barrier[2] # Barrier Block Array
- w=TRENCH_WIDTH_M/3 # Block Width
- h=TRENCH_HEIGHT_M/3 # Block Height
- hw=w/2.0 # Block Half Width
- hh=h/2.0 # Block Half Height
- # Calculate the colour of the blocks, based on base colour and distance.
- # The barrier's base colour is taken from its start position.
- distance=1.0-0.9*(n-pos[2])/FAR_PLANE_M
- base_colour=BARRIER_COLOURS[int(n % len(BARRIER_COLOURS))]
- i=0 # Block Index (0 to 8)
- for y in range(-1, 2):
- for x in range(-1, 2):
- if m[i] == 1: # Test if Block is present
- px=x*w # Coordinates at the centre of the block
- py=y*h
- cube=( # Define a tuple containing the coordinates for this cube. They are indexed by BLOCK_VERTEX.
- (px-hw, py-hh, n),
- (px+hw, py-hh, n),
- (px+hw, py+hh, n),
- (px-hw, py+hh, n),
- (px-hw, py-hh, f),
- (px+hw, py-hh, f),
- (px+hw, py+hh, f),
- (px-hw, py+hh, f)
- )
- # Project the 3d coordinates into 2d canvas coordinates
- cube_p=[]
- for p in cube:
- cube_p.append(project(p))
- if not pos[2] > barrier[0] and pos[2] < barrier[0]+barrier[1]:
- for vi in BLOCK_VERTEX:
- x1,y1,x2,y2=int(cube_p[vi[0]][0]), int(cube_p[vi[0]][1]), int(cube_p[vi[1]][0]), int(cube_p[vi[1]][1])
- img.line(((x1,y1), (x2,y2)), outline=base_colour, width=LINE_WIDTH)
- i += 1
- # Draws all of the visible barriers. The game remembers the first visible barrier (current_barrier_index),
- # so that each frame it doesn't need to go through the entire list of barriers to get the first that is visible.
- # The visible barriers are always inserted at the front of their own list, which ensures that they are drawn back to front.
- def render_barriers():
- global current_barrier_index
- visible_barriers=[]
- index=current_barrier_index
- while index < len(barriers):
- barrier=barriers[index]
- index += 1
- visible=(barrier[0]+barrier[1]-pos[2]) > NEAR_PLANE_M
- visible=visible and (barrier[0]-pos[2] < FAR_PLANE_M)
- if visible:
- visible_barriers.insert(0, barrier)
- elif pos[2] > barrier[0]:
- current_barrier_index=index
- else:
- break
- for barrier in visible_barriers:
- render_barrier(barrier)
- def render_exhaust_port():
- if reached_launch_position:
- if launch_mode != 'launched' and pt_launch_position == -1:
- img.rectangle(((LAUNCH_BUTTON)), outline=RGB_RED, width=4)
- canvas.bind(key_codes.EButton1Down, launch_btn, ((LAUNCH_BUTTON[0],LAUNCH_BUTTON[1]), (LAUNCH_BUTTON[2],LAUNCH_BUTTON[3])))
- draw_text_button_centre('LAUNCH', LAUNCH_BUTTON, 10, 50, RGB_RED)
- y=TRENCH_HEIGHT_M/2
- z=EXHAUST_PORT_POSITION_M
- w=EXHAUST_PORT_WIDTH_M
- hw=w/2
- hole=((-hw, y, z-hw), (hw, y, z-hw), (hw, y, z+hw), (-hw, y, z+hw))
- coords=[]
- for p in hole:
- coords.append(project(p))
- coords.append(coords[0])
- if pos[2] < TRENCH_LENGTH_M-100:
- img.polygon(coords, 4, EXHAUST_PORT_COLOUR)
- #img.line((-w,y,-hw,y), LINE_WIDTH, EXHAUST_PORT_COLOUR)
- #img.line((w,y,hw,y), LINE_WIDTH, EXHAUST_PORT_COLOUR)
- #img.line((0,z-w,0,z-hw), LINE_WIDTH, EXHAUST_PORT_COLOUR)
- #img.line((0,z+w,0,z+hw), LINE_WIDTH, EXHAUST_PORT_COLOUR)
- def render_torpedoes():
- global trp
- if launch_mode == 'launched':
- cX,cY=0,trp[1]
- rad=PROTON_TORPEDO_RADIUS_M
- for port in [0,1]:
- LR=trp[0]+(PROTON_TORPEDO_SPAN_M*port)
- img.ellipse((cX+LR,cY,cX+LR+rad,cY+rad), outline=TORPEDO_COLOUR, fill=RGB_BLACK, width=2)
- #trp=[trp[0],trp[1],trp[2]+(vel*3)]
- def render_distance():
- distance=int(get_distance_to_launch_position())
- if distance > 0:
- distance_str=str(distance)
- while len(distance_str) < 5:
- distance_str="0"+distance_str
- distance_str += "m"
- draw_text_centre(distance_str, CANVAS_HEIGHT-4, 29, DISTANCE_COLOUR)
- def render_message():
- global message_delay
- if (message_delay > 0):
- y=76
- for line in message.split("\n"):
- draw_text_centre(line, y, 35, RGB_YELLOW)
- y += 45
- def move_ship():
- global pos, pt_launch_position
- # Pull up at the end of the Trench
- if pos[2] > EXHAUST_PORT_POSITION_M:
- pos[1] += -1
- if pt_launch_position < 0:
- set_message("\n\n\n\nYou forgot to fire your torpedoes!")
- pt_launch_position=0
- # Slow down when poised to launch torpedo
- factor=float(fps)
- if pt_launch_position < 0 and is_close_to_launch_position():
- factor *= 4
- pos[2] += vel
- def move_torpedoes():
- global pt_pos, explosion_countdown
- if len(pt_pos) > 0:
- hit=False
- bullseye=False
- for p in pt_pos:
- # Check if the torpedo has reached the point at which it dives towards the floor of the trench
- if p[2]-pt_launch_position >= PROTON_TORPEDO_RANGE_M:
- p[1] += PROTON_TORPEDO_VELOCITY_MS*0.5/fps
- else:
- p[2] += PROTON_TORPEDO_VELOCITY_MS/fps
- # Check if the torpedo has hit the floor of the trench
- if p[1] > TRENCH_HEIGHT_M/2:
- hw=EXHAUST_PORT_WIDTH_M/2
- z=EXHAUST_PORT_POSITION_M
- ex1=-hw
- ex2=hw
- ez1=z-hw
- ez2=z+hw
- # Check if torpedo entirely fit within the exhaust port
- if p[0]-PROTON_TORPEDO_RADIUS_M >= ex1 and \
- p[0]+PROTON_TORPEDO_RADIUS_M <= ex2 and \
- p[2]-PROTON_TORPEDO_RADIUS_M >= ez1 and \
- p[2]+PROTON_TORPEDO_RADIUS_M <= ez2:
- bullseye=True
- hit=True
- if hit:
- pt_pos=[] # Delete the torpedoes
- if bullseye:
- set_message("\nGreat shot kid... \nThat was the one in a million!")
- explosion_countdown=180
- else:
- set_message("\nNegative-It just impacted off the surface")
- # Keep the ship within the bounds of the trench.
- def constrain_ship():
- tw=TRENCH_WIDTH_M // 2
- th=TRENCH_HEIGHT_M // 2
- # Keep the ship within the horizontal span of the trench
- m=SHIP_WIDTH_M/2+1
- if pos[0] < int(-tw+m):
- pos[0]=(-tw+m)
- elif pos[0] > int(tw-m):
- pos[0]=(tw-m)
- # Keep the ship within the vertical span of the trench
- m=SHIP_HEIGHT_M/2
- if pos[1] < int(-th+m) and pt_launch_position < 0: # Allow the ship to leave the trench after it has launched the torpedoes
- pos[1]=(-th+m)
- elif pos[1] > int(th-m):
- pos[1]=(th-m)
- # Determine whether the ship has collided with any blocks
- def check_for_collisions():
- global dead
- if current_barrier_index < len(barriers):
- barrier=barriers[current_barrier_index]
- # Check if we are in the same Z position as the barrier
- if pos[2]+2 > barrier[0] and pos[2]+2 < barrier[0]+barrier[1]:
- # Calculate the area that our ship occupies
- x1=pos[0]-SHIP_WIDTH_M/2.0
- x2=x1+SHIP_WIDTH_M
- y1=pos[1]-SHIP_HEIGHT_M/2.0
- y2=y1+SHIP_HEIGHT_M
- # Calculate block size
- bw=TRENCH_WIDTH_M/3.0
- bh=TRENCH_HEIGHT_M/3.0
- bhw=bw/2.0
- bhh=bh/2.0
- for by in range(-1,2):
- by1=by*bh-bhh
- by2=by1+bh
- # Check to see whether we intersect vertically
- if y1 < by2 and y2 > by1:
- for bx in range(-1,2):
- block_index=(by+1)*3+bx+1
- if barrier[2][block_index] == 1:
- bx1=bx*bw-bhw
- bx2=bx1+bw
- # Check to see whether we intersect horizontally
- if x1 < bx2 and x2 > bx1:
- set_message('')
- draw_text_centre('GAME OVER', SCALE_HEIGHT+30, 120, RGB_YELLOW)
- dead=True
- def generate_messages():
- global reached_launch_position
- if not reached_launch_position and is_close_to_launch_position():
- reached_launch_position=True
- set_message("You're all clear kid, now let's\nblow this thing and go home")
- def render_deathstar():
- centre=get_canvas_centre()
- centre=centre [0], centre [1]*0.85
- radius=DEATH_STAR_RADIUS
- encircle=(centre[0]-radius, centre[1]-radius, centre[0]+radius, centre[1]+radius)
- if fill_colour == None:
- img.ellipse(encircle, outline=DEATH_STAR_COLOUR, fill=RGB_BLACK, width=2)
- DEATH_STAR_WEAPON=radius*0.42
- rad=radius*0.25
- cX,cY=centre[0]-DEATH_STAR_WEAPON, centre[1]-DEATH_STAR_WEAPON
- img.ellipse((cX-rad, cY-rad, cX+rad, cY+rad),outline=DEATH_STAR_COLOUR, fill=RGB_BLACK, width=2)
- img.line(((centre[0]-radius, centre[1]-3), (centre[0]+radius, centre[1]-3)), outline=DEATH_STAR_COLOUR, width=LINE_WIDTH)
- img.line(((centre[0]-radius, centre[1]+3), (centre[0]+radius, centre[1]+3)), outline=DEATH_STAR_COLOUR, width=LINE_WIDTH)
- def render_stars():
- star_colours=[]
- for brightness in stars:
- shade=random.randrange(64, 255)
- colour=shade, shade, shade
- star_colours.append(colour)
- i=0
- for star in stars:
- img.point(star, star_colours[i],width=2)
- i += 1
- def draw_text_centre(text, y, size, colour):
- centre=get_canvas_centre()
- style=(FONT_STYLE,size,None)
- pos=(centre[0])-(img.measure_text(unicode(text), font=style)[1])/2
- img.text((pos,y), unicode(text), colour, style)
- def draw_text_button_centre(text, button, y, size, colour):
- centre=get_button_centre(button)
- style=(FONT_STYLE,size,None)
- pos=(centre[0])-(img.measure_text(unicode(text), font=style)[1])/2
- img.text((pos,centre[1]+size), unicode(text), colour, style)
- def draw_text_right(text, x, y, size, colour):
- style=(FONT_STYLE,size,None)
- pos=x-img.measure_text(unicode(text), font=style)[1]
- img.text((pos,y), unicode(text), colour, style)
- def render_intro_text():
- centre=get_canvas_centre()
- draw_text_centre("Star Wars", 180, 132, INTRO_TEXT_COLOUR)
- draw_text_centre("Tap here to begin your attack run", 250, 38, INTRO_TEXT_COLOUR)
- draw_text_centre("Tilt to move", 280, 21, INTRO_TEXT_COLOUR)
- draw_text_right(VERSION, CANVAS_WIDTH-16, 14, 14, INTRO_TEXT_COLOUR)
- img.rectangle(((START_BUTTON)), outline=INTRO_TEXT_COLOUR, width=LINE_WIDTH)
- x1=centre[0]-160
- y1=centre[1]+205
- x2=centre[0]+160
- y2=centre[1]+225
- #img.polygon(((x1, y1), (x2, y1), (x2, y2), (x1, y2)), 1, RGB_BLACK, RGB_BLACK)
- def create_particles():
- global particles
- radius=DEATH_STAR_RADIUS
- particles=[]
- for i in range(0, 500):
- a=random.random()*2*math.pi
- m=random.random()
- x=math.sin(a)*m*radius
- y=math.cos(a)*m*radius
- particles.append([x, y])
- def render_particles():
- c=get_canvas_centre()
- for p in particles:
- x=p[0]+c[0]
- y=p[1]+c[1]
- canvas.draw_circle((x, y), 1, 1, PARTICLE_COLOUR)
- def move_particles():
- c=get_canvas_centre()
- for p in particles:
- x=p[0]+c[0]
- y=p[1]+c[1]
- if x >= 0 and x < CANVAS_WIDTH and y >= 0 and y < CANVAS_HEIGHT:
- v=1.1
- p[0] *= v
- p[1] *= v
- def render_victory():
- global game_mode, explosion_countdown,colour
- render_stars()
- if explosion_countdown <= 0:
- if explosion_countdown > -160:
- base_colour=(64, 32, 16)
- factor=-explosion_countdown/10.0
- colour=[]
- for c in base_colour:
- colour.append(c*factor)
- render_deathstar()
- elif explosion_countdown == -160:
- create_particles()
- elif explosion_countdown > -400:
- render_particles()
- move_particles()
- else:
- game_mode=MODE_END_DELAY
- else:
- render_deathstar()
- explosion_countdown -= 1
- def game_reset():
- global game_mode
- if dead:
- canvas.bind(key_codes.EButton1Down, launch_btn, (BTN_OFF))
- game_mode=MODE_END_DELAY
- def delay_restart():
- global game_mode
- t=time.time()+3
- while t > time.time():
- pass
- game_mode=MODE_INTRO
- render_intro()
- def render_intro():
- img.clear(RGB_BLACK)
- render_stars()
- render_deathstar()
- render_intro_text()
- draw(())
- canvas.bind(key_codes.EButton1Down, start_btn, ((START_BUTTON[0],START_BUTTON[1]), (START_BUTTON[2],START_BUTTON[3])))
- def update_time():
- global last_time, message_delay, fps
- t=time.time()
- if last_time > 0:
- delta=(t-last_time)
- fps=1.0/delta
- if message_delay > 0:
- message_delay -= delta
- last_time=t
- def render():
- update_time()
- def exit_btn(event):
- pass
- def start_btn(event):
- global game_mode, Ypos
- Ypos=xyz[0]
- if game_mode == MODE_INTRO:
- game_mode=MODE_GAME
- init_game()
- def launch_btn(event):
- global launch_mode,trp
- launch_mode='launched'
- trp=(pos[0]+SCALE_WIDTH-(PROTON_TORPEDO_SPAN_M/2)-(PROTON_TORPEDO_RADIUS_M/2),pos[1]+SCALE_HEIGHT,pos[2])
- launch_proton_torpedoes()
- def api_events():
- global game_mode, pos
- x,y=xyz[1],xyz[0] ### orientation flipped
- if game_mode == MODE_GAME:
- pos[0] += ACCELERATION_MSS*(x*1.8)
- pos[1] += ACCELERATION_MSS*((Ypos-y)*-1.8)
- elif game_mode == MODE_VICTORY and explosion_countdown <= 0:
- game_mode=MODE_END_DELAY
- create_stars()
- render_intro()
- draw(())
- def forLight():
- appuifw.e32.reset_inactivity()
- timer.after(10,forLight)
- def exit():
- global game_mode, main_loop
- game_mode='Exit'
- main_loop=False
- appuifw.app.exit_key_handler=exit
- timer=appuifw.e32.Ao_timer()
- sens.start_listening()
- forLight()
- # This is the main game processing function.
- main_loop=True
- while main_loop:
- while game_mode == MODE_GAME:
- img=Image.new(canvas.size)
- img.clear(RGB_BLACK)
- api_events()
- move_ship()
- constrain_ship()
- move_torpedoes()
- check_for_collisions()
- generate_messages()
- if message_delay <= 0:
- game_mode=MODE_END_DELAY
- render_trench()
- render_barriers()
- render_exhaust_port()
- render_torpedoes()
- render_distance()
- render_message()
- draw(())
- game_reset()
- e32.ao_yield()
- if game_mode == MODE_END_DELAY:
- delay_restart()
- e32.ao_yield()
- #
- sens.stop_listening()
- timer.cancel()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement