Advertisement
here2share

# PyRPG3D.py

Feb 24th, 2021
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.55 KB | None | 0 0
  1. # PyRPG3D.py
  2.  
  3. import Tkinter
  4. import os
  5. import time
  6.  
  7. # build map #
  8.  
  9. map = [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  10.        [1,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,1,1],
  11.        [1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1],
  12.        [1,0,0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1],
  13.        [1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,1],
  14.        [1,1,1,1,0,0,0,1,0,0,1,1,5,1,1,0,0,1,1],
  15.        [1,0,7,0,0,0,0,0,1,0,1,0,1,0,0,1,0,1,1],
  16.        [1,0,1,0,0,1,1,0,0,0,0,0,0,0,1,0,1,0,1],
  17.        [1,0,1,1,0,1,0,1,2,0,1,1,1,1,0,0,1,0,1],
  18.        [1,0,0,0,0,0,0,1,0,0,0,0,1,0,1,1,1,1,1],
  19.        [1,0,1,0,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1],
  20.        [1,0,0,0,0,1,0,0,0,0,1,0,0,1,1,0,0,1,1],
  21.        [1,0,1,1,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1],
  22.        [1,1,0,0,0,1,0,0,0,1,0,0,0,0,1,1,1,0,1],
  23.        [1,0,0,1,0,0,1,1,0,1,0,1,0,0,0,1,0,1,1],
  24.        [1,4,0,1,0,1,0,0,0,1,1,0,1,0,1,0,1,1,1],
  25.        [1,4,1,0,0,1,0,1,1,1,0,0,1,0,1,0,1,0,1],
  26.        [1,0,1,0,1,1,0,1,0,0,6,1,0,0,1,8,1,3,1],
  27.        [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]
  28.  
  29. map_height = len( map )
  30. map_width  = len( map[ 0 ] )
  31.  
  32. # turn map template into collection of cells #
  33.  
  34. def new_cell( state ):
  35.     return { "state":    state,
  36.          "visited":  0,
  37.          "contents": [] }
  38.  
  39. templateDict = { 0: 'empty',
  40.          1: 'wall',
  41.          2: 'enemy',
  42.          3: 'treasure',
  43.          4: 'fake_wall',
  44.          5: 'teleport',
  45.          6: 'stairs_up',
  46.          7: 'stairs_down',
  47.          8: 'stairs_both' }
  48.  
  49. for y in range( len( map ) ):
  50.     for x in range( len( map[ y ] ) ):
  51.         map[ y ][ x ] = new_cell( templateDict[ map[ y ][ x ] ] )
  52.  
  53. # initial settings #
  54.  
  55. pos = [1, 1]  # player pos: x, y
  56. facing = 'S'  # N,E,S,W?
  57.  
  58. # UI response functions #
  59.  
  60. def firstperson_click( event = None ):
  61.     (w,h) = ( canv.winfo_width(), canv.winfo_height() )
  62.     if   event.x < w/3:   move_left()
  63.     elif event.x > 2*w/3: move_right()
  64.     else:
  65.         if   event.y < h/3:   print "climb the stairs"
  66.         elif event.y > 2*h/3: print "decend the stairs"
  67.         else: move_forward()
  68.  
  69. def overhead_click(self, event):
  70.     pos = [int(event.x/canv2scale),int(event.y/canv2scale)]
  71.     drawmap()
  72.  
  73. # player motion functions #
  74.  
  75. def move_forward( event = None ):
  76.     global pos, map
  77.     (x,y) = (pos[0], pos[1])
  78.     dx = { "N":  0, "E": 1, "S": 0, "W": -1 }[ facing ]
  79.     dy = { "N": -1, "E": 0, "S": 1, "W":  0 }[ facing ]
  80.     if map[ y+dy ][ x+dx ][ "state" ] != 'wall':
  81.         pos = [ x+dx, y+dy ]
  82.         map[ y ][ x ][ "visited" ] = 1
  83.         drawmap()
  84.  
  85. def move_back( event = None ):
  86.     global pos, map
  87.     (x,y) = (pos[0], pos[1])
  88.     dx = { "N": 0, "E": -1, "S":  0, "W": 1 }[ facing ]
  89.     dy = { "N": 1, "E":  0, "S": -1, "W": 0 }[ facing ]
  90.     if map[ y+dy ][ x+dx ][ "state" ] != 'wall':
  91.         pos = [ x+dx, y+dy ]
  92.         map[ y ][ x ][ "visited" ] = 1
  93.         drawmap()
  94.  
  95. def turn_right( event = None ):
  96.     global facing
  97.     facing = { "N": "E",  "E": "S",  "S": "W",  "W": "N" }[ facing ]
  98.     drawmap()
  99.  
  100. def turn_left( event = None ):
  101.     global facing
  102.     facing = { "N": "W",  "E": "N",  "S": "E",  "W": "S" }[ facing ]
  103.     drawmap()
  104.  
  105. def turn_around( event = None ):
  106.     global facing
  107.     facing = { "N": "S",  "E": "W",  "S": "N",  "W": "E" }[ facing ]
  108.     drawmap()
  109.  
  110. # UI #
  111.  
  112. root = Tkinter.Tk()
  113. root.title( "PyRPG3D" )
  114.  
  115. images = {}
  116. for filename in filter( lambda x: x[-4:] == ".gif", os.listdir( os.curdir ) ):
  117.     images[ filename[:-4] ] = Tkinter.PhotoImage( file = filename )
  118.  
  119. firstperson = Tkinter.Frame( root )
  120. firstperson.pack( side='left', anchor=Tkinter.NW )
  121. overhead = Tkinter.Frame( root )
  122. overhead.pack( side='left', anchor=Tkinter.NW )
  123. canv = Tkinter.Canvas( firstperson, width=253, height=253, relief='sunken', bd=1 )
  124. canv.pack()
  125. root.bind( "<Button 1>", firstperson_click )
  126. root.bind( "<Control-Key>", turn_around )
  127. root.bind( "<Up>", move_forward )
  128. root.bind( "<Down>", move_back )
  129. root.bind( "<Left>", turn_left )
  130. root.bind( "<Right>", turn_right )
  131. canv2 = Tkinter.Canvas( overhead, width=253, height=253, relief='sunken', bd=1 )
  132. canv2.pack()
  133. canv2.bind( "<space>", overhead_click )
  134. side = Tkinter.Frame( firstperson )
  135. side.pack( side='left' )
  136. arrows = Tkinter.Frame( firstperson )
  137. arrows.pack( side='left' )
  138. title = Tkinter.Label( side, text='PyRPG3D' )
  139. title.pack( padx=20 )
  140.  
  141. '''
  142. def build_button( imagename, commandval, rowval, colval, bindstr ):
  143.     b = Tkinter.Button( arrows, image=images[ imagename ], command=commandval,
  144.                 width=21, height=21 )
  145.     b.grid( row=rowval, column=colval )
  146.     root.bind_all( bindstr, commandval )
  147.     return b
  148.  
  149. button_forward = build_button( "up",     move_forward, 0, 1, "" )
  150. button_back    = build_button( "down",   move_back,    2, 1, "" )
  151. button_left    = build_button( "left",   turn_left,    1, 0, "" )
  152. button_right   = build_button( "right",  turn_right,   1, 2, "" )
  153. button_turn    = build_button( "rotate", turn_around,  1, 1, "" )
  154. '''
  155.  
  156. label = Tkinter.Label( overhead, text="could put some zooming mode buttons here" )
  157. label.pack( pady=30 )
  158.  
  159. # map windows & calculate drawing scaling factor #
  160.  
  161. root.update()
  162. canv2scale = canv2.winfo_width()/(map_width+1)
  163.  
  164.  
  165.  
  166. # screen drawing functions #
  167.  
  168. def valid_cell( x, y ):
  169.     if (0 <= y < map_height) and (0 <= x < map_width):
  170.         return 1
  171.     return 0
  172.  
  173. def getview():
  174.     """
  175.     Return player's point of view
  176.     consisting of three lists,
  177.     closest to farthest.
  178.     """
  179.     view = []
  180.     (xpos,ypos) = (pos[0],pos[1])
  181.  
  182.     # use facing to determine rasterization #
  183.    
  184.     across_dx, across_dy = { "N": (+1,0),
  185.                  "E": (0,+1),
  186.                  "S": (-1,0),
  187.                  "W": (0,-1) }[ facing ]
  188.    
  189.     deep_dx, deep_dy = { "N": (-2,-1),
  190.                  "E": (+1,-2),
  191.                  "S": (+2,+1),
  192.                  "W": (-1,+2) }[ facing ]
  193.    
  194.     # cue up to start position #
  195.    
  196.     xpos = xpos + across_dx + deep_dx
  197.     ypos = ypos + across_dy + deep_dy
  198.  
  199.     for godeep in range( 3 ):
  200.  
  201.         # clear this level #
  202.        
  203.         vlevel = []
  204.  
  205.         # scan this level #
  206.        
  207.         for goacross in range( 3 ):
  208.             if valid_cell( xpos, ypos ):
  209.                 vlevel.append( map[ ypos ][ xpos ] )
  210.             else:
  211.                 vlevel.append( new_cell( "wall" ) )
  212.             xpos = xpos + across_dx
  213.             ypos = ypos + across_dy
  214.            
  215.         # go back to last spot #
  216.  
  217.         # (Yes, we could get rid of this by changing the deep_dx/dy.)
  218.        
  219.         xpos = xpos - across_dx
  220.         ypos = ypos - across_dy
  221.        
  222.         # cue up to start of next across #
  223.  
  224.         xpos = xpos + deep_dx
  225.         ypos = ypos + deep_dy
  226.  
  227.         view.append( vlevel )
  228.  
  229.     return view
  230.  
  231. def image( x, y, imagename ):
  232.     canv.create_image( x, y, anchor=Tkinter.NW, image=images[ imagename ] )
  233.  
  234. def drawmap():
  235.     local = getview()
  236.     canv.delete( Tkinter.ALL )
  237.     canv2.delete( Tkinter.ALL )
  238.     '''
  239.     image( 0, 0, "back" )
  240.  
  241.     if len( local ) > 1:
  242.         if local[1][0][ "state" ] == 'wall': image(0,   92, "vlevel-2-left" )
  243.         if local[1][1][ "state" ] == 'wall': image(80,  92, "vlevel-2-mid" )
  244.         if local[1][2][ "state" ] == 'wall': image(153, 92, "vlevel-2-right" )
  245.    
  246.     if local[0][0][ "state" ] == 'wall': image( 0,   30, "vlevel-1-left" )
  247.     if local[0][2][ "state" ] == 'wall': image( 178, 30, "vlevel-1-right" )
  248.     if local[0][1][ "state" ] == 'wall': image( 0,   30, "vlevel-1-mid" )
  249.     '''
  250.  
  251.     drawoverhead()
  252.  
  253. def drawoverhead():
  254.  
  255.     # draw map #
  256.    
  257.     y = 0
  258.     for row in map:
  259.         y = y + 1
  260.         x = 0
  261.         for cell in row:
  262.             x = x + 1
  263.             # call function depending on cell state #
  264.             { "wall":  draw_wall,
  265.               "enemy": draw_enemy,
  266.               "treasure": draw_treasure,
  267.               "fake_wall": draw_wall,
  268.               "teleport": draw_teleport,
  269.               "stairs_up": draw_stairs_up,
  270.               "stairs_down": draw_stairs_down,
  271.               "stairs_both": draw_stairs_both,
  272.               "empty": draw_nothing }[ cell[ "state" ] ]( x, y )
  273.  
  274.     # draw player #
  275.    
  276.     draw_rect( pos[0]+1, pos[1]+1, "black", 0.7, 1 )
  277.    
  278.     (_x,_y) = ( (pos[0]+1)*canv2scale, (pos[1]+1)*canv2scale )
  279.     d = (canv2scale / 2) * .9
  280.    
  281.     (_x1, _y1, _x2, _y2) = { "N": (_x, _y+d, _x, _y-d ),
  282.                  "E": (_x-d, _y, _x+d, _y ),
  283.                  "S": (_x, _y-d, _x, _y+d ),
  284.                  "W": (_x+d, _y, _x-d, _y ) }[ facing ]
  285.  
  286.     canv2.create_line( _x1, _y1, _x2, _y2, arrow='last', fill='white' )
  287.  
  288.  
  289. def draw_rect( x, y, color, scale, fill ):
  290.     (_x,_y) = ( x * canv2scale, y * canv2scale )
  291.     d = (canv2scale / 2) * scale
  292.     if fill == 0:
  293.         canv2.create_rectangle( _x-d, _y-d, _x+d, _y+d, outline=color )
  294.     else:
  295.         canv2.create_rectangle( _x-d, _y-d, _x+d, _y+d, fill=color, outline=color )
  296.  
  297. def draw_wall( x, y ):        draw_rect( x, y, "#808080", 1.0, 1 )
  298. def draw_enemy( x, y ):       draw_rect( x, y, "red", 0.7, 1 )
  299. def draw_treasure( x, y ):    draw_rect( x, y, "gold", 0.7, 1 )
  300. def draw_teleport( x, y ):    draw_rect( x, y, "darkgreen", 0.7, 1 )
  301. def draw_stairs_up( x, y ):   draw_rect( x, y, "darkgreen", 0.7, 0 )
  302. def draw_stairs_down( x, y ): draw_rect( x, y, "darkgreen", 0.7, 0 )
  303. def draw_stairs_both( x, y ): draw_rect( x, y, "darkgreen", 0.7, 0 )
  304. def draw_nothing( x, y ):     pass
  305.  
  306. root.update()
  307. drawmap()
  308. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement