All pastes #90232 Raw Edit

Stuff

public text v1 · immutable
#90232 ·published 2006-07-17 14:19 UTC
rendered paste body
from livewires import *

#
# Game
#

the_maze = Maze()
while not the_maze.finished():
	the_maze.play()
the_maze.done()

#
# Grids , colours
#

grid_size = 30
margin = grid_size
background_colour = Colour.black
wall_colour = make_colour(0.6,0.9,0.9)

#
# MAZE MAP
# % = wall
# . = food
# o = capsule
# g = ghost
# p = pacman
# else is ignored

the_layout = [
	"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
	"%.....%..................%....%",
	"%o%%%.%.%%%.%%%%%%.%%%...%.%%o%",
	"%.%.....%......%.....%......%.%",
	"%....%%%.%.%%%.%.%%%.%.%%%....%",
	"%%%%.%...%...........%...%.%%%%",
	"%....%%%..%%%%% %%%%...%%%....%",
	"%...%...%.%gg    gg%..%.......%",
	"%.........%%%%%%%%%%..........%",
	"%%%%%%%%....%%...%%.....%%%%%%%",
	"%....o%...%%...%%%.....%%o%%%%%",
	"%%%%%...%%%...........%%%.%%%%%",
	"%...%...%%%.%.%%%..%..%%..%%%%%",
	"%.%...%%%%%.p.%%....%....%%%%%%",
	"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"]

#
# MAZE Class
#

class Maze:
	def __init__(self):
		self.have_window = 0
		self.game_over = 0
		self.set_layout(the_layout)

	def set_layout(self,layout):
		height = len(layout)
		width = len(layout[0])
		self.make_window(width, height)
		self.make_map(width, height)

		max_y = height -1
		for x in range(width):
			char = layout[max_y - y][x]
			self.make_object((x,y),char)
	def make_window(self,width,height):
		grid_width = (width-1) * grid_size
		grid_height = (height-1) * grid_size
		screen_width = 2*margin + grid_width
		screen_height = 2*margin+ grid_height
		begin_graphics(screen_width,
			screen_height,
			background_colour)
		allow_movables()

	def to_screen(self,point):
		(x,y) = point
		x = x*grid_size+ margin
		y = y*grid_size + margin
		retur (x,y)

	def make_map(self,width,height):
		self.width = width
		self.height = height
		self.map = []
		for y in range(height):
			new_row = []
			for x in range(width):
				new_row.append(Nothing())
			self.map.append(new_row)

	def make_object(self,point,character):
		(x,y) = point
		if character == "%":
			self.map[y][x] = Wall(self,point)

	def finished(self):
		return self.game_over
	def play(self):
		sleep(0.05)
	def done(self):
		move(50,50)
		set_colour(Colour.red)
		text(" - - GAME OVER - -")
		sleep(10)
		end_graphics()
		self.map = []


#
# IMMOVABLE class
#

class Immovable:
	pass

#
# NOTHING class
#

class Nothing(Immovable):
	pass


#
# WALL class
#

class Wall(Immovable):
	def __init__(self,maze,point):
		self.place = point
		self.screen_point = maze.to_screen(point)
		self.maze = maze
		self.draw()

	def draw(self):
		(screen_x, screen_y) = self.screen_point
		forbid_movables()
		dot_size = grid_size * 0.2
		circle(screen_x, screen_y, dor_size,
			colour = wall_colour, filled = 1)
		allow_movables()