Advertisement
here2share

# Star_Wars_Trench_Run_Revised_For_PYS60.py ZZZ

Jan 18th, 2015
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Star_Wars_Trench_Run_Revised_For_PYS60.py  -from-  http://www.codeskulptor.org/#demos-starwars.py
  2.  
  3. # I might add a bit more code soon enough, especially to guide the launching of the torpedoes.
  4.  
  5. # The original coding of Star Wars, written on 4th & 5th November 2012 by Steven Knock.
  6. # May the force be with you all.
  7.  
  8. RGB_BLACK=(0,0,0)
  9. RGB_WHITE=(255,255,255)
  10. RGB_GRAYLIGHT=(236,236,236)
  11. RGB_GRAY=(180,180,180)
  12. RGB_GRAYDARK=(169,169,169)
  13. RGB_RED=(255,0,0)
  14. RGB_ORANGE=(255,165,0)
  15. RGB_YELLOW=(255,255,0)
  16. RGB_GREEN=(0,128,0)
  17. RGB_BLUE=(0,0,255)
  18. RGB_CYAN=(0,255,255)
  19. RGB_PURPLE=(128,0,128)
  20.  
  21. import math
  22. import random
  23. #import simplegui
  24. import time
  25.  
  26. from graphics import *
  27. import appuifw
  28. import sensor
  29. import key_codes
  30. import e32
  31.  
  32. VERSION="v1.5"
  33.  
  34. CANVAS_WIDTH=640
  35. CANVAS_HEIGHT=360
  36. TRENCH_LENGTH_M=3500 ### 700 for quick testing
  37. TRENCH_WIDTH_M=10
  38. TRENCH_HEIGHT_M=10
  39. TRENCH_WALL_INTERVAL_M=20
  40. TRENCH_COLOUR=RGB_GRAY
  41. EXHAUST_PORT_POSITION_M=TRENCH_LENGTH_M-100
  42. EXHAUST_PORT_WIDTH_M=TRENCH_WIDTH_M/3.0
  43. PROTON_TORPEDO_RANGE_M=200
  44. PROTON_TORPEDO_RADIUS_M=60
  45. PROTON_TORPEDO_SPAN_M=200
  46. LAUNCH_POSITION_M=EXHAUST_PORT_POSITION_M-PROTON_TORPEDO_RANGE_M
  47. DISTANCE_COLOUR=RGB_RED
  48. DEATH_STAR_RADIUS=CANVAS_HEIGHT*0.4
  49. DEATH_STAR_COLOUR=RGB_GRAY
  50. LINE_WIDTH=2
  51.  
  52. MODE_INTRO=0
  53. MODE_GAME=1
  54. MODE_VICTORY=2
  55. MODE_END_DELAY=3
  56.  
  57. SHIP_WIDTH_M=1
  58. SHIP_HEIGHT_M=0.8
  59.  
  60. NEAR_PLANE_M=0.1
  61. FAR_PLANE_M=180.0
  62. SCALE_WIDTH=CANVAS_WIDTH/2
  63. SCALE_HEIGHT=CANVAS_HEIGHT/2
  64.  
  65. FORWARD_VELOCITY_MS=2
  66. PROTON_TORPEDO_VELOCITY_MS=FORWARD_VELOCITY_MS*2
  67. VELOCITY_MAX_MS=15.0
  68. VELOCITY_DAMPEN=0.7
  69. ACCELERATION_MSS=0.02
  70.  
  71. TORPEDO_COLOUR=RGB_RED
  72. EXHAUST_PORT_COLOUR=RGB_RED
  73. INTRO_TEXT_COLOUR=RGB_YELLOW
  74. WARNING_TEXT_COLOUR=RGB_RED
  75. PARTICLE_COLOUR=RGB_WHITE
  76. FONT_STYLE="normal"
  77.  
  78. BTN_OFF=(-1,-1), (-1,-1)
  79.  
  80. BARRIER_COLOURS=((255, 0, 0), (255, 192, 0), (0, 255, 0), (255, 255, 0), (0, 255, 255), (255, 0, 255))
  81.  
  82. 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))
  83.  
  84. START_BUTTON=15, 205, CANVAS_WIDTH-15, 305
  85. LAUNCH_BUTTON=CANVAS_WIDTH-250, 195, CANVAS_WIDTH-15, 305
  86.  
  87. # Ship Variables
  88. pos=[0,0,0]
  89. vel=FORWARD_VELOCITY_MS
  90.  
  91. # Proton Torpedo Variables
  92. pt_pos=[]
  93. trp=[0,0,0]
  94. pt_launch_position=-1
  95. launch_mode=False
  96.  
  97. # Exhaust Port in Range
  98. reached_launch_position=False
  99.  
  100. # Trench Barriers
  101. barriers=[]
  102. current_barrier_index=0
  103. fill_colour=None
  104.  
  105. # Message
  106. message=""
  107. message_delay=0
  108. message_tick=0
  109.  
  110. # Stars
  111. stars=[]
  112.  
  113. # Victory
  114. explosion_countdown=0
  115. particles=[]
  116.  
  117. # Game State
  118. game_mode=MODE_INTRO
  119. dead=False
  120.  
  121. # Actual FPS
  122. last_time=0
  123. fps=60
  124.  
  125. import appuifw
  126. import sensor
  127.  
  128. def circle(x, y, r, color):
  129.     r=abs(r)
  130.     img.ellipse((x-r, y-r, x+r, y+r), fill=color)
  131.  
  132. def draw(rect):
  133.     try:
  134.         canvas.blit(img)
  135.     except: pass
  136.  
  137. xyz=(0,0,0)
  138. def redraw():
  139.     global xyz
  140.     xyz=(sens.x,sens.y,sens.z)
  141.  
  142. appuifw.app.orientation='landscape'
  143. appuifw.app.screen='large'
  144. appuifw.app.directional_pad=False
  145. canvas=appuifw.Canvas(redraw_callback=draw)
  146. appuifw.app.body=canvas
  147. sens=sensor.AccelerometerXYZAxisData(data_filter=sensor.LowPassFilter())
  148. sens.set_callback(data_callback=redraw)
  149.  
  150. img=Image.new(canvas.size)
  151.  
  152. # Return the centre point of the canvas
  153. def get_canvas_centre():
  154.     return (CANVAS_WIDTH // 2, CANVAS_HEIGHT // 2)
  155.  
  156. # Return the centre point of the button
  157. def get_button_centre(xy):
  158.     x,y,x2,y2=xy
  159.     x,y=x+int((x2-x)/2),y+4 #y+int((y2-y)/2)
  160.     return (x,y)
  161.  
  162. # Creates an array of stars randomly scattered on the canvas
  163. def create_stars():
  164.     while len(stars) < 300:
  165.         x=random.randrange(CANVAS_WIDTH)
  166.         y=random.randrange(CANVAS_HEIGHT)
  167.         stars.append((x, y))
  168.  
  169. # Creates all of the barriers that appear in the game. Each Barrier is represented by three elements:
  170. # Start Position-the distance along the trench that the barrier starts
  171. # Length-the length of the barrier
  172. # Blocks-an array of 9 ints, either 1 or 0, that indicate which blocks in a 3x3 square appear in the barrier.
  173. # The Barriers are placed in a list which is sequenced by the Start Position of the Barriers. This allows
  174. #   the rendering and collision code to consider only the barriers immediately surrounding the ship.
  175. def create_barriers():
  176.     global barriers
  177.     barriers=[]
  178.  
  179.     # Determine Start Position and Last Position
  180.     s=150.0
  181.     limit=LAUNCH_POSITION_M-150
  182.     while s < limit:
  183.         # Create a totally solid barrier
  184.         blocks=[]
  185.         for i in range(0, 9):
  186.             blocks.append(1)
  187.  
  188.         # Punch a number of empty blocks in the barrier, based on how close to the end of the trench the barrier is.
  189.         empty_blocks=1.0-(s/limit)
  190.         empty_blocks=int((empty_blocks*8))+2
  191.         for i in range(0, empty_blocks):
  192.             blocks[random.randrange(9)]=0
  193.  
  194.         # Calculate a random length
  195.         l=random.randrange(5)+5
  196.         barriers.append((s, l, blocks))
  197.         s += l
  198.         s += 40+random.randrange(30)
  199.  
  200. # Initialise all game variables to prepare for a new game
  201. def init_game():
  202.     global game_mode, pos, vel, pt_pos, pt_launch_position, reached_launch_position, current_barrier_index
  203.     global explosion_countdown, fill_colour, launch_mode, trp,dead
  204.     game_mode=MODE_GAME
  205.     pos=[0, 0, 0]
  206.     trp=[0, 0, 0]
  207.     vel=FORWARD_VELOCITY_MS
  208.     pt_pos=[]
  209.     launch_mode=None
  210.     dead=False
  211.     pt_launch_position=-1
  212.     fill_colour=None
  213.     reached_launch_position=False
  214.     current_barrier_index=0
  215.     explosion_countdown=0
  216.     create_barriers()
  217.     set_message("\n\n\n\n\nUse the Force")
  218.     canvas.bind(key_codes.EButton1Down, start_btn, (BTN_OFF))
  219.  
  220. # Calculates the distance remaining until the ship reaches the torpedoes launch position
  221. def get_distance_to_launch_position():
  222.     return LAUNCH_POSITION_M-pos[2]
  223.  
  224. # Indicates whether the ship is 'close' to the launch position.
  225. def is_close_to_launch_position():
  226.     return math.fabs(get_distance_to_launch_position()) < 100.0
  227.  
  228. # Fire the Proton Torpedoes, if they haven't already been launched.
  229. # They are initially positioned slightly under the ship.
  230. def launch_proton_torpedoes():
  231.     global pt_launch_position
  232.  
  233.     canvas.bind(key_codes.EButton1Down, launch_btn, (BTN_OFF))
  234.     if pt_launch_position < 0 and is_close_to_launch_position():
  235.         pt_pos.append([pos[0]-PROTON_TORPEDO_SPAN_M, pos[1]+1, pos[2]])
  236.         pt_pos.append([pos[0]+PROTON_TORPEDO_SPAN_M, pos[1]+1, pos[2]])
  237.         pt_launch_position=pos[2]
  238.  
  239. # Sets the current message. The message appears for 3 seconds.
  240. def set_message(new_message):
  241.     global message, message_delay, message_tick
  242.     message=new_message
  243.     message_delay=3
  244.     message_tick=0
  245.  
  246. # Projects a 3d point into a 2d canvas coordinate. The 3d coordinates are based on +x -> right, +y -> down +z -> away.
  247. # The origin of the 3d coordinate system is the ship's initial position in the middle of the start of the trench.
  248. def project(point):
  249.     distance=point[2]-pos[2]
  250.     x=(point[0]-pos[0])/(distance+NEAR_PLANE_M)
  251.     y=(point[1]-pos[1])/(distance+NEAR_PLANE_M)
  252.     x *= SCALE_WIDTH
  253.     y *= SCALE_HEIGHT
  254.     x += (CANVAS_WIDTH // 2)
  255.     y += (CANVAS_HEIGHT // 2)
  256.     return (x, y)
  257.  
  258. # Displays the Death sequence. If the flashing colours are enabled, then a red rectangle is drawn onto the screen every other frame.
  259.  
  260. # Draws the trench. Firstly, four lines are drawn from the player's z position towards the end of the trench.
  261. # Secondly, a rectangle is drawn at the end of the trench.
  262. # Thirdly, the lines along the wall are drawn.
  263. def render_trench():
  264.    
  265.     if pos[2] < TRENCH_LENGTH_M:
  266.         tw=TRENCH_WIDTH_M // 2
  267.         th=TRENCH_HEIGHT_M // 2
  268.         trench=([-tw, -th], [tw, -th], [tw, th], [-tw, th])
  269.         trench_p=[]
  270.         for t in trench:
  271.             near=list(t)
  272.             near.append(pos[2])
  273.             far=list(t)
  274.             far.append(TRENCH_LENGTH_M)
  275.             near_p=project(near)
  276.             far_p=project(far)
  277.             img.line(((near_p), (far_p)), outline=TRENCH_COLOUR, width=LINE_WIDTH)
  278.             trench_p.append(far_p)
  279.  
  280.         # Draw far wall
  281.         trench_p.append(trench_p[0])
  282.         if pos[2] < trench_p:
  283.             img.polygon((trench_p), width=LINE_WIDTH, fill=TRENCH_COLOUR)
  284.  
  285.         # Draw vertical walls
  286.         distance=(int(pos[2]+TRENCH_WALL_INTERVAL_M) // TRENCH_WALL_INTERVAL_M)*TRENCH_WALL_INTERVAL_M
  287.         limit=min(pos[2]+FAR_PLANE_M, TRENCH_LENGTH_M)
  288.         while distance < limit:
  289.             for side in [-1, 1]:
  290.                 p1=project((side*tw, -th, distance))
  291.                 p2=project((side*tw, th, distance))
  292.                 img.line(((p1), (p2)), outline=TRENCH_COLOUR, width=LINE_WIDTH)
  293.             distance += TRENCH_WALL_INTERVAL_M
  294.  
  295. # Draws a single barrier.
  296. def render_barrier(barrier):
  297.     n=barrier[0]            # Barrier Start Position
  298.     f=n+barrier[1]      # Barrier End Position
  299.     m=barrier[2]            # Barrier Block Array
  300.     w=TRENCH_WIDTH_M/3      # Block Width
  301.     h=TRENCH_HEIGHT_M/3     # Block Height
  302.     hw=w/2.0                # Block Half Width
  303.     hh=h/2.0                # Block Half Height
  304.  
  305.     # Calculate the colour of the blocks, based on base colour and distance.
  306.     # The barrier's base colour is taken from its start position.
  307.     distance=1.0-0.9*(n-pos[2])/FAR_PLANE_M
  308.     base_colour=BARRIER_COLOURS[int(n % len(BARRIER_COLOURS))]
  309.  
  310.     i=0                 # Block Index (0 to 8)
  311.     for y in range(-1, 2):
  312.         for x in range(-1, 2):
  313.             if m[i] == 1:   # Test if Block is present
  314.                 px=x*w  # Coordinates at the centre of the block
  315.                 py=y*h
  316.                 cube=(  # Define a tuple containing the coordinates for this cube. They are indexed by BLOCK_VERTEX.
  317.                     (px-hw, py-hh, n),
  318.                     (px+hw, py-hh, n),
  319.                     (px+hw, py+hh, n),
  320.                     (px-hw, py+hh, n),
  321.                     (px-hw, py-hh, f),
  322.                     (px+hw, py-hh, f),
  323.                     (px+hw, py+hh, f),
  324.                     (px-hw, py+hh, f)
  325.                 )
  326.  
  327.                 # Project the 3d coordinates into 2d canvas coordinates
  328.                 cube_p=[]
  329.                 for p in cube:
  330.                     cube_p.append(project(p))
  331.  
  332.                 if not pos[2] > barrier[0] and pos[2] < barrier[0]+barrier[1]:
  333.                     for vi in BLOCK_VERTEX:
  334.                         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])
  335.                         img.line(((x1,y1), (x2,y2)), outline=base_colour, width=LINE_WIDTH)
  336.             i += 1
  337.  
  338. # Draws all of the visible barriers. The game remembers the first visible barrier (current_barrier_index),
  339. # so that each frame it doesn't need to go through the entire list of barriers to get the first that is visible.
  340. # The visible barriers are always inserted at the front of their own list, which ensures that they are drawn back to front.
  341. def render_barriers():
  342.     global current_barrier_index
  343.     visible_barriers=[]
  344.  
  345.     index=current_barrier_index
  346.     while index < len(barriers):
  347.         barrier=barriers[index]
  348.         index += 1
  349.         visible=(barrier[0]+barrier[1]-pos[2]) > NEAR_PLANE_M
  350.         visible=visible and (barrier[0]-pos[2] < FAR_PLANE_M)
  351.         if visible:
  352.             visible_barriers.insert(0, barrier)
  353.         elif pos[2] > barrier[0]:
  354.             current_barrier_index=index
  355.         else:
  356.           break
  357.  
  358.     for barrier in visible_barriers:
  359.         render_barrier(barrier)
  360.  
  361. def render_exhaust_port():
  362.     if reached_launch_position:
  363.         if launch_mode != 'launched' and pt_launch_position == -1:
  364.             img.rectangle(((LAUNCH_BUTTON)), outline=RGB_RED, width=4)
  365.             canvas.bind(key_codes.EButton1Down, launch_btn, ((LAUNCH_BUTTON[0],LAUNCH_BUTTON[1]), (LAUNCH_BUTTON[2],LAUNCH_BUTTON[3])))
  366.             draw_text_button_centre('LAUNCH', LAUNCH_BUTTON, 10, 50, RGB_RED)
  367.         y=TRENCH_HEIGHT_M/2
  368.         z=EXHAUST_PORT_POSITION_M
  369.         w=EXHAUST_PORT_WIDTH_M
  370.         hw=w/2
  371.         hole=((-hw, y, z-hw), (hw, y, z-hw), (hw, y, z+hw), (-hw, y, z+hw))
  372.         coords=[]
  373.         for p in hole:
  374.             coords.append(project(p))
  375.         coords.append(coords[0])
  376.         if pos[2] < TRENCH_LENGTH_M-100:
  377.             img.polygon(coords, 4, EXHAUST_PORT_COLOUR)
  378.  
  379.         #img.line((-w,y,-hw,y), LINE_WIDTH, EXHAUST_PORT_COLOUR)
  380.         #img.line((w,y,hw,y), LINE_WIDTH, EXHAUST_PORT_COLOUR)
  381.         #img.line((0,z-w,0,z-hw), LINE_WIDTH, EXHAUST_PORT_COLOUR)
  382.         #img.line((0,z+w,0,z+hw), LINE_WIDTH, EXHAUST_PORT_COLOUR)
  383.  
  384. def render_torpedoes():
  385.     global trp
  386.     if launch_mode == 'launched':
  387.         cX,cY=0,trp[1]
  388.         rad=PROTON_TORPEDO_RADIUS_M
  389.         for port in [0,1]:
  390.             LR=trp[0]+(PROTON_TORPEDO_SPAN_M*port)
  391.             img.ellipse((cX+LR,cY,cX+LR+rad,cY+rad), outline=TORPEDO_COLOUR, fill=RGB_BLACK, width=2)
  392.         #trp=[trp[0],trp[1],trp[2]+(vel*3)]
  393.        
  394. def render_distance():
  395.     distance=int(get_distance_to_launch_position())
  396.     if distance > 0:
  397.         distance_str=str(distance)
  398.         while len(distance_str) < 5:
  399.             distance_str="0"+distance_str
  400.         distance_str += "m"
  401.         draw_text_centre(distance_str, CANVAS_HEIGHT-4, 29, DISTANCE_COLOUR)
  402.  
  403. def render_message():
  404.     global message_delay
  405.     if (message_delay > 0):
  406.         y=76
  407.         for line in message.split("\n"):
  408.             draw_text_centre(line, y, 35, RGB_YELLOW)
  409.             y += 45
  410.  
  411. def move_ship():
  412.     global pos, pt_launch_position
  413.  
  414.     # Pull up at the end of the Trench
  415.     if pos[2] > EXHAUST_PORT_POSITION_M:
  416.         pos[1] += -1
  417.         if pt_launch_position < 0:
  418.             set_message("\n\n\n\nYou forgot to fire your torpedoes!")
  419.             pt_launch_position=0
  420.  
  421.     # Slow down when poised to launch torpedo
  422.     factor=float(fps)
  423.     if pt_launch_position < 0 and is_close_to_launch_position():
  424.         factor *= 4
  425.  
  426.     pos[2] += vel
  427.  
  428. def move_torpedoes():
  429.     global pt_pos, explosion_countdown
  430.     if len(pt_pos) > 0:
  431.         hit=False
  432.         bullseye=False
  433.         for p in pt_pos:
  434.             # Check if the torpedo has reached the point at which it dives towards the floor of the trench
  435.             if p[2]-pt_launch_position >= PROTON_TORPEDO_RANGE_M:
  436.                 p[1] += PROTON_TORPEDO_VELOCITY_MS*0.5/fps
  437.             else:
  438.                 p[2] += PROTON_TORPEDO_VELOCITY_MS/fps
  439.  
  440.             # Check if the torpedo has hit the floor of the trench
  441.             if p[1] > TRENCH_HEIGHT_M/2:
  442.                 hw=EXHAUST_PORT_WIDTH_M/2
  443.                 z=EXHAUST_PORT_POSITION_M
  444.                 ex1=-hw
  445.                 ex2=hw
  446.                 ez1=z-hw
  447.                 ez2=z+hw
  448.                 # Check if torpedo entirely fit within the exhaust port
  449.                 if  p[0]-PROTON_TORPEDO_RADIUS_M >= ex1 and \
  450.                     p[0]+PROTON_TORPEDO_RADIUS_M <= ex2 and \
  451.                     p[2]-PROTON_TORPEDO_RADIUS_M >= ez1 and \
  452.                     p[2]+PROTON_TORPEDO_RADIUS_M <= ez2:
  453.                     bullseye=True
  454.                 hit=True
  455.         if hit:
  456.             pt_pos=[]       # Delete the torpedoes
  457.             if bullseye:
  458.                 set_message("\nGreat shot kid... \nThat was the one in a million!")
  459.                 explosion_countdown=180
  460.             else:
  461.                 set_message("\nNegative-It just impacted off the surface")
  462.  
  463. # Keep the ship within the bounds of the trench.
  464. def constrain_ship():
  465.     tw=TRENCH_WIDTH_M // 2
  466.     th=TRENCH_HEIGHT_M // 2
  467.  
  468.     # Keep the ship within the horizontal span of the trench
  469.     m=SHIP_WIDTH_M/2+1
  470.     if pos[0] < int(-tw+m):
  471.         pos[0]=(-tw+m)
  472.     elif pos[0] > int(tw-m):
  473.         pos[0]=(tw-m)
  474.  
  475.     # Keep the ship within the vertical span of the trench
  476.     m=SHIP_HEIGHT_M/2
  477.     if pos[1] < int(-th+m) and pt_launch_position < 0:      # Allow the ship to leave the trench after it has launched the torpedoes
  478.         pos[1]=(-th+m)
  479.     elif pos[1] > int(th-m):
  480.         pos[1]=(th-m)
  481.  
  482. # Determine whether the ship has collided with any blocks
  483. def check_for_collisions():
  484.     global dead
  485.  
  486.     if current_barrier_index < len(barriers):
  487.         barrier=barriers[current_barrier_index]
  488.  
  489.         # Check if we are in the same Z position as the barrier
  490.         if pos[2]+2 > barrier[0] and pos[2]+2 < barrier[0]+barrier[1]:
  491.             # Calculate the area that our ship occupies
  492.             x1=pos[0]-SHIP_WIDTH_M/2.0
  493.             x2=x1+SHIP_WIDTH_M
  494.             y1=pos[1]-SHIP_HEIGHT_M/2.0
  495.             y2=y1+SHIP_HEIGHT_M
  496.  
  497.             # Calculate block size
  498.             bw=TRENCH_WIDTH_M/3.0
  499.             bh=TRENCH_HEIGHT_M/3.0
  500.             bhw=bw/2.0
  501.             bhh=bh/2.0
  502.             for by in range(-1,2):
  503.                 by1=by*bh-bhh
  504.                 by2=by1+bh
  505.  
  506.                 # Check to see whether we intersect vertically
  507.                 if y1 < by2 and y2 > by1:
  508.                     for bx in range(-1,2):
  509.                         block_index=(by+1)*3+bx+1
  510.                         if barrier[2][block_index] == 1:
  511.                             bx1=bx*bw-bhw
  512.                             bx2=bx1+bw
  513.  
  514.                             # Check to see whether we intersect horizontally
  515.                             if x1 < bx2 and x2 > bx1:
  516.                                 set_message('')
  517.                                 draw_text_centre('GAME OVER', SCALE_HEIGHT+30, 120, RGB_YELLOW)
  518.                                 dead=True
  519.  
  520. def generate_messages():
  521.     global reached_launch_position
  522.  
  523.     if not reached_launch_position and is_close_to_launch_position():
  524.         reached_launch_position=True
  525.         set_message("You're all clear kid, now let's\nblow this thing and go home")
  526.  
  527. def render_deathstar():
  528.     centre=get_canvas_centre()
  529.     centre=centre [0], centre [1]*0.85
  530.     radius=DEATH_STAR_RADIUS
  531.     encircle=(centre[0]-radius, centre[1]-radius, centre[0]+radius, centre[1]+radius)
  532.     if fill_colour == None:
  533.         img.ellipse(encircle, outline=DEATH_STAR_COLOUR, fill=RGB_BLACK, width=2)
  534.         DEATH_STAR_WEAPON=radius*0.42
  535.         rad=radius*0.25
  536.         cX,cY=centre[0]-DEATH_STAR_WEAPON, centre[1]-DEATH_STAR_WEAPON
  537.         img.ellipse((cX-rad, cY-rad, cX+rad, cY+rad),outline=DEATH_STAR_COLOUR, fill=RGB_BLACK, width=2)
  538.         img.line(((centre[0]-radius, centre[1]-3), (centre[0]+radius, centre[1]-3)), outline=DEATH_STAR_COLOUR, width=LINE_WIDTH)
  539.         img.line(((centre[0]-radius, centre[1]+3), (centre[0]+radius, centre[1]+3)), outline=DEATH_STAR_COLOUR, width=LINE_WIDTH)
  540.  
  541. def render_stars():
  542.     star_colours=[]
  543.     for brightness in stars:
  544.         shade=random.randrange(64, 255)
  545.         colour=shade, shade, shade
  546.         star_colours.append(colour)
  547.  
  548.     i=0
  549.     for star in stars:
  550.         img.point(star, star_colours[i],width=2)
  551.         i += 1
  552.  
  553. def draw_text_centre(text, y, size, colour):
  554.     centre=get_canvas_centre()
  555.     style=(FONT_STYLE,size,None)
  556.     pos=(centre[0])-(img.measure_text(unicode(text), font=style)[1])/2
  557.     img.text((pos,y), unicode(text), colour, style)
  558.  
  559. def draw_text_button_centre(text, button, y, size, colour):
  560.     centre=get_button_centre(button)
  561.     style=(FONT_STYLE,size,None)
  562.     pos=(centre[0])-(img.measure_text(unicode(text), font=style)[1])/2
  563.     img.text((pos,centre[1]+size), unicode(text), colour, style)
  564.  
  565. def draw_text_right(text, x, y, size, colour):
  566.     style=(FONT_STYLE,size,None)
  567.     pos=x-img.measure_text(unicode(text), font=style)[1]
  568.     img.text((pos,y), unicode(text), colour, style)
  569.  
  570. def render_intro_text():
  571.     centre=get_canvas_centre()
  572.     draw_text_centre("Star Wars", 180, 132, INTRO_TEXT_COLOUR)
  573.     draw_text_centre("Tap here to begin your attack run", 250, 38, INTRO_TEXT_COLOUR)
  574.     draw_text_centre("Tilt to move", 280, 21, INTRO_TEXT_COLOUR)
  575.     draw_text_right(VERSION, CANVAS_WIDTH-16, 14, 14, INTRO_TEXT_COLOUR)
  576.     img.rectangle(((START_BUTTON)), outline=INTRO_TEXT_COLOUR, width=LINE_WIDTH)
  577.  
  578.     x1=centre[0]-160
  579.     y1=centre[1]+205
  580.     x2=centre[0]+160
  581.     y2=centre[1]+225
  582.     #img.polygon(((x1, y1), (x2, y1), (x2, y2), (x1, y2)), 1, RGB_BLACK, RGB_BLACK)
  583.  
  584. def create_particles():
  585.     global particles
  586.  
  587.     radius=DEATH_STAR_RADIUS
  588.     particles=[]
  589.     for i in range(0, 500):
  590.         a=random.random()*2*math.pi
  591.         m=random.random()
  592.         x=math.sin(a)*m*radius
  593.         y=math.cos(a)*m*radius
  594.         particles.append([x, y])
  595.  
  596. def render_particles():
  597.     c=get_canvas_centre()
  598.     for p in particles:
  599.         x=p[0]+c[0]
  600.         y=p[1]+c[1]
  601.         canvas.draw_circle((x, y), 1, 1, PARTICLE_COLOUR)
  602.  
  603. def move_particles():
  604.     c=get_canvas_centre()
  605.     for p in particles:
  606.         x=p[0]+c[0]
  607.         y=p[1]+c[1]
  608.         if x >= 0 and x < CANVAS_WIDTH and y >= 0 and y < CANVAS_HEIGHT:
  609.             v=1.1
  610.             p[0] *= v
  611.             p[1] *= v
  612.  
  613. def render_victory():
  614.     global game_mode, explosion_countdown,colour
  615.  
  616.     render_stars()
  617.     if explosion_countdown <= 0:
  618.         if explosion_countdown > -160:
  619.             base_colour=(64, 32, 16)
  620.             factor=-explosion_countdown/10.0
  621.             colour=[]
  622.             for c in base_colour:
  623.                 colour.append(c*factor)
  624.             render_deathstar()
  625.         elif explosion_countdown == -160:
  626.             create_particles()
  627.         elif explosion_countdown > -400:
  628.             render_particles()
  629.             move_particles()
  630.         else:
  631.             game_mode=MODE_END_DELAY
  632.     else:
  633.         render_deathstar()
  634.     explosion_countdown -= 1
  635.  
  636. def game_reset():
  637.     global game_mode
  638.     if dead:
  639.         canvas.bind(key_codes.EButton1Down, launch_btn, (BTN_OFF))
  640.         game_mode=MODE_END_DELAY
  641.  
  642. def delay_restart():
  643.     global game_mode
  644.     t=time.time()+3
  645.     while t > time.time():
  646.         pass
  647.     game_mode=MODE_INTRO
  648.     render_intro()
  649.  
  650. def render_intro():
  651.     img.clear(RGB_BLACK)
  652.     render_stars()
  653.     render_deathstar()
  654.     render_intro_text()
  655.     draw(())
  656.     canvas.bind(key_codes.EButton1Down, start_btn, ((START_BUTTON[0],START_BUTTON[1]), (START_BUTTON[2],START_BUTTON[3])))
  657.  
  658. def update_time():
  659.     global last_time, message_delay, fps
  660.     t=time.time()
  661.     if last_time > 0:
  662.         delta=(t-last_time)
  663.         fps=1.0/delta
  664.         if message_delay > 0:
  665.             message_delay -= delta
  666.     last_time=t
  667.  
  668. def render():
  669.     update_time()
  670.  
  671. def exit_btn(event):
  672.     pass
  673. def start_btn(event):
  674.     global game_mode, Ypos
  675.     Ypos=xyz[0]
  676.     if game_mode == MODE_INTRO:
  677.         game_mode=MODE_GAME
  678.         init_game()
  679. def launch_btn(event):
  680.     global launch_mode,trp
  681.     launch_mode='launched'
  682.     trp=(pos[0]+SCALE_WIDTH-(PROTON_TORPEDO_SPAN_M/2)-(PROTON_TORPEDO_RADIUS_M/2),pos[1]+SCALE_HEIGHT,pos[2])
  683.     launch_proton_torpedoes()
  684.  
  685. def api_events():
  686.     global game_mode, pos
  687.  
  688.     x,y=xyz[1],xyz[0] ### orientation flipped
  689.     if game_mode == MODE_GAME:
  690.         pos[0] += ACCELERATION_MSS*(x*1.8)
  691.         pos[1] += ACCELERATION_MSS*((Ypos-y)*-1.8)
  692.  
  693.     elif game_mode == MODE_VICTORY and explosion_countdown <= 0:
  694.         game_mode=MODE_END_DELAY
  695.  
  696. create_stars()
  697. render_intro()
  698. draw(())
  699.  
  700. def forLight():
  701.     appuifw.e32.reset_inactivity()
  702.     timer.after(10,forLight)
  703.  
  704. def exit():
  705.     global game_mode, main_loop
  706.     game_mode='Exit'
  707.     main_loop=False
  708. appuifw.app.exit_key_handler=exit
  709.  
  710. timer=appuifw.e32.Ao_timer()
  711. sens.start_listening()
  712. forLight()
  713.  
  714. # This is the main game processing function.
  715. main_loop=True
  716. while main_loop:
  717.     while game_mode == MODE_GAME:
  718.         img=Image.new(canvas.size)
  719.         img.clear(RGB_BLACK)
  720.         api_events()
  721.         move_ship()
  722.         constrain_ship()
  723.         move_torpedoes()
  724.         check_for_collisions()
  725.         generate_messages()
  726.         if message_delay <= 0:
  727.             game_mode=MODE_END_DELAY
  728.         render_trench()
  729.         render_barriers()
  730.         render_exhaust_port()
  731.         render_torpedoes()
  732.         render_distance()
  733.         render_message()
  734.         draw(())
  735.         game_reset()
  736.         e32.ao_yield()
  737.     if game_mode == MODE_END_DELAY:
  738.         delay_restart()
  739.     e32.ao_yield()
  740. #
  741. sens.stop_listening()
  742. timer.cancel()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement