![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2007
Posts: 17
Rep Power: 0
![]() |
Pacman in Python Code
I have been working on this code and can't figure it out. I have followed this website: http://www.ibiblio.org/obp/pyBiblio/...ml/pacman.html
just to get an idea and follow along to get my code to work. However, it's not working! This si what I did and I would like to know what I did wrong: #! /usr/bin/env python
import os, sys
import pygame
from pygame.locals import *
#...
the_maze = Maze()
while not the_maze.finished():
the_maze.play()
the_maze.done()
grid_size = 30
margin = grid_size
background_color = Color.black
wall_color = make_color(0.6, 0.9, 0.9)
#The shape of the maze. Each character
#represents a different type of object.
# % - Wall
# . - Food
# o - Capsule
# G - Ghost (Python, Lynux, Unix, MSACS)
# P - Garridoman (Pacman)
# Other characters are ignored.
the_layout = [
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
"%.....%.................%.....%",
"%o%%%.%.%%%.%%%%%%%.%%%.%.%%%o%",
"%.%.....%......%......%.....%.%",
"%...%%%.%.%%%%.%.%%%%.%.%%%...%",
"%%%.%...%.%.........%.%...%.%%%",
"%...%.%%%.%.%%%%%%%.%.%%%.%...%",
"%.%%%.......%GG GG%.......%%%.%",
"%...%.%%%.%.%%%%%%%.%.%%%.%...%",
"%%%.%...%.%.........%.%...%.%%%",
"%...%%%.%.%%%%.%.%%%%.%.%%%...%",
"%.%.....%......%......%.....%.%",
"%o%%%.%.%%%.%%%%%%%.%%%.%.%%%o%",
"%.....%........P........%.....%",
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"]
class Maze:
#...
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):
for y in range(height):
char = layout[max_y - y][x]
self.make_object((x, y), char)
def to_screen(self, point):
(x, y) = point
x = x*grid_size + margin
y = y*grid_size + margin
return (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):
end_graphic()
self.map = []
def make_object(self, point, character):
(x, y) = point
if character == '%':
self.map[y][x] = Wall(self, point)
elif character == 'P':
garridoman = Garridoman(self, point)
self.movable.append(garridoman)
def set_layout(self, layout):
height = len(layout)
width = len(layout[0])
self.make_window(width, height)
self.make_map(width, height)
self.movables = []
max_y = height - 1
for x in range(width):
for y in range(height):
char = layout[max_y - y][x]
self.make_object((x, y), char)
for movable in self.movables:
movable.draw()
def done(self):
end_graphics()
self.map = []
self.movables = []
def play(self):
for movable in self.movables:
movable.move()
sleep(0.05)
def set_layout(self, layout):
height = len(layout)
width = len(layout)[0])
self.make_window(width, height)
self.movable = []
self.food_count = 0
max_y = height - 1
for x in range(width):
for y in range(height):
char = layout[max_y - y][x]
for movable in self.movables:
movable.draw()
def make_object(self, point, character):
(x, y) = point
if character == '%':
self.map[y][x] = Wall(self, point)
elif character == 'P':
garridoman = Garridoman(self, point)
self.movables.append(garridoman)
elif character == '.':
self.food_count = self.food_count + 1
self.map[y][x] Food(self, point)
def make_object(self, point, character):
elif character == 'G'
ghost = Ghost(self, point)
self.movables.append(ghost)
ghost_colors = []
ghost_colors.append(Color.red)
ghost_colors.append(Color.green)
ghost_colors.append(Color.blue)
ghost_colors.append(Color.purple)
ghost_speed = 0.25
def garridoman_is(self, garridoman, point):
for movable in self.movable:
movable.garridoman_is(garridoman, point)
pass
def lose(self):
print "You lose!"
self.game_over = 1
def make_object(self, point, character):
elif character == 'o':
self.map[y][x] = Capsule(self, point)
def remove_capsule(self, place):
(x, y) = place
self.map[y][x] = Nothing()
for movable in self.movables:
movable.capsule_eaten()
class Capsule(Immovable):
def _init_(self, maze, point):
self.place = point
self.screen_point = maze.to_screen(point)
self.maze = maze
self.draw()
capsule_color = Color_white
capsule_size = grid_size * 0.3
def draw(self):
(screen_x, screen_y) = self.screen_point
self.dot = circle(screen_x, screen_y,
capsule_size,
color = capsule_color,
filled = 1)
def eat(self, garridoman):
remove_from_screen(self.dot)
self.maze.remove_capsule(self.place)
class Ghost(Movable):
def _init_(self, maze, start):
global ghost_colors
self.next_point = start
self.movement = (0, 0)
self.color = ghost_colors[0]
ghost_colors[:1] = []
ghost_colors.append(self.colors)
self.original_color = self.color
self.time_left = 0
Movable.-init_(self, maze,
start, ghost_speed)
def capsule_eaten(self):
self.change_color(scared_color)
self.time_left = scared_time
def change_color(self, new_color):
self.color = new_color
self.redraw()
ghost_shape = [
(0, -0.5),
(0.25, -0.75),
(0.5, -0.5),
(0.75, -0.75),
(0.75, 0.5),
(0.5, 0.75),
(-0.5, 0.75),
(-0.75, 0.5),
(-0.75, -0.75),
(-0.5, -0.5),
(-0.25, -0.75)
]
def draw(self):
maze = self.maze
(screen_x, screen_y) = (
maze.to_screen(self.place) )
coords = []
for (x, y) in ghost_shape:
coords.append(x*grid_size + screen_x,
y*grid_size + screen_y)
self.body = polygon(coords, self.color,
closed = 1,
filled = 1)
def choose_move(self):
(move_x, move_y) = self.movement
(nearest_x, nearest_y) = (
self.nearest_grid_point() _
possible_moves = []
if move_x >= 0 and self.can.move_by((1, 0)):
possible_moves.append((1, 0))
if move_x <= 0 and self.can_move_by((-1, 0))
possible_moves.append((-1, 0))
if move_y >= 0 and self.can.move_by((0, 1)):
possible_moves.append((0, 1))
if move_y <= 0 and self.canmove_by(0, -1)):
possible_moves.append((0, -1))
if len(possible_moves) != 0:
move = random_choice(possible_moves)
(move_x, move_y) = move
else:
move_x = -move_x
move_y = -move_y
move = (move_x, move_y)
(x, y) = self.place
self.next_point = (x+move_x, y+move_y)
self.movement = move
return self.furthest_move(move)
def can_move_by(self, move):
move = self.further_move(move)
return move != (0, 0)
def move_by(self, move):
(old_x, old_y) = self.place
(move_x, move_y) = move
(new_x, new_y) = (old_x+move_x, old_y+move_y)
self.place = (new_x, new_y)
screen_move = (move_x * grid_size,
move_y * grid_size)
move_by(self.body, screen_move)
def captured(self, garridoman):
self.place = self.start
self color = self.original_color
self.time_left = 0
self.redraw()
def garridoman_is(self, garridoman, point):
(my_x, my_y) = self.place
(his_x, his_y) = point
X = my_x - his_x
Y = my_y - his_y
DxD = X*X + Y*Y
limit = 1.6*1.6
if DxD < limit:
self.bump_into(garridoman)
def bump_into(self, garridoman):
if self.time_left != 0:
self.captured(garridoman)
else:
self.maze.lose()
def redraw(self):
old_body = self.body
self.draw()
remove_from_screen(old_body)
warning_time = 50
def move(self):
(current_x, current_y) = self.place
(next_x, next_y) = self.next_point
move = (next_x - current_x,
next_y - current_y)
move = self.furthest_move(move)
if move == (0, 0):
move = self.choose_move()
self.move_by(move)
if self.time_left > 0:
self.update_scared()
def update_scared(self):
self.time_left = self.time_left -1
time_left = self.time_left
if time_left % 2 ==0:
color = self.original_color
else:
color = scared_color
self.change_color(color)
class Food(Immovable):
def _init_(self, maze, point):
self.place = point
self. screen_point = maze.to_screen(point)
self.maze = maze
self.draw()
food_color = Color.orange
food_size = grid_size * 0.15
def draw(self):
(screen_x, screen_y) = self.screen_point
self.dot = circle(screen_x, screen_y,
food_size,
color = food_color,
filled = 1)
def eat(self, garridoman):
remove_from_screen(self.dot)
self.maze.remove_food(self.place)
def remove_food(self, place):
(x, y) = place
self.map[y][x] = Nothing()
self.food_count = self.food_count - 1
if self.food_count == 0:
self.win()
def win(self):
print "You win!"
self.game_over = 1
class Immovable:
pass
def is_a_wall(self):
return 0
def eat(self, pacman):
pass
class Nothin(Immovable):
pass
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_movable()
dot_size = grid_size * 0.2
circle(screen_x, screen_y, dot_size,
color = wall_color, filled = 1)
(x, y) = self.place
neighbors = [ (x+1, y), (x-1, y),
(x, y+1), (x, y-1)]
for neighbor in neighbors:
self.check_neighbor(neighbor)
allow_movables()
maze = self.maze
screen_point = maze.to_screen(self.place)
angle = self.get_angle()
endpoints = (self.direction + angle,
self.direction + 360 - angle)
self.body = circle(screen_point, garridoman_color,
color = garridoman_color,
filled = 1,
endpoints = endpoints)
def get_angle(self):
(x, y) = self.place
(nearest_x, nearest_y) = (
self.nearest_grid_point() )
distance = ( abs(x-nearest_x) +
abs(y-nearest_y) )
return 1 + 90*distance
def check_neighbor(self, neighbor):
maze + self.maze
object = maze.object_at(neighbor)
if object.is_a_wall():
here = self.screen_point
there = maze.to_screen(neighbor)
line(here, there, color = wall_color)
allow_movables()
def object_at(self, point):
(x, y) = point
if y < 0 or y >= self.width:
return Nothing()
return self.map[y][x]
def is_a_wall(self):
return 1
class Movable:
def _init_(self, maze, point, speed):
self.maze = maze
self.place = point
self.speed = speed
self.start = point
def furthest_move(self, movement):
(move_x, move_y) = movement
(current_x, current_y) = self.place
nearest = self.nearest_grid_point()
(nearest_x, nearest_y) = nearest
maze = self.maze
if move_x > 0:
next_point = (nearest_x+1, nearest_y)
if maze.object_at(next_point).is_a_wall():
if current_x+move_x > nearest_x:
move_x = nearest_x - current_x
elif move_x < 0:
next_point = (nearest_x-1, nearest_y)
if maze.object_at(next_point).is_a_wall():
if current_x+move_x < nearest_x:
move_x = nearest_x - current_x
if move_y > 0:
next_point = (nearest_x, nearest_y+1)
if maze.object_at(next_point).is_a_wall():
if current_y+move_y > nearest_y:
move_y = nearest_y - current_y
elif move_y < 0:
next_point = (nearest x, nearest_y-1)
if maze.object_at(next_point).is_a_wall():
if current_y+move_y < nearest_y:
move_y = nearest_y - current_y
if move_x > self.speed:
move_x = self.speed
elif move_x < -self.speed:
move_x = -self.speed
if move_y > self.speed:
move_y = self.speed
elif move_y < -self.speed:
move_y = -self.speed
return (move_x, move_y)
def nearest_grid_point(self):
(current_x, current_y) = self.place
grid_x = int(current_x + 0.5)
grid_y = int(current_y + 0.5)
return (grid_x, grid_y)
garridoman_color = Color.yellow
garridoman_size = grid_size * 0.8
garridoman_speed = 0.25
def capsule_eaten(self):
pass
scared_color = Color.white
scared_time = 300
class Garridoman(Movable):
def _init_(self, maze, point):
Movable._init_(self, maze, point
garridoman_speed)
def move(self):
keys = keys_pressed()
if 'z' in keys: self.move_left()
elif 'x' in keys: self.move_right()
elif ';' in keys: self.move_up()
elif '.' in keys: self.move_down()
self.maze.garridoman_is(self, self.place)
def move_left(self):
self.try_move((-1, 0))
def move_right(self):
self.try_move((1, 0))
def move_up(self):
self.try_move((0, 1))
def move_down(self):
self.try_move((0, -1))
def try_move(self, move):
(move_x, move_y) = move
(current_x, current_y) = self.place
(nearest_x, nearest_y) = (
self.nearest_grid_point() )
if self.furthest_move(move) == (0,0):
return
if move_x != 0 and current_y != nearest_y:
move_x = 0
move_y = nearest_y - current_y
elif move_y != 0 and current_x != nearest_x:
move_y = 0
move_x = nearest_x - current_x
move = self.furthest_move((move_x, move_y))
self.move_by(move)
def _init_(self, maze, point):
self.direction = 0
Moveable._init_(self, maze, point,
garridoman_speed)
def move_by(self, move):
self.update_position(move)
old_body = self.body
self.draw()
remove_from_screen(old_body)
def update_position(self, move):
(old_x, old_y) = self.place
(move_x, move_y) = move
(new_x, new_y) = (old_x+move_x, old_y+move_y)
self.place = (new_x, new_y)
if move_x > 0:
self.direction = 0
elif move_y > 0:
self.direction = 90
elif move_x < 0:
self.direction = 180
elif move_y < 0:
self.direction = 270
def move_by(self, move):
self.update_position(move)
old_body = self.body
self.draw()
remove_from_screen(old_body)
(x, y) = self.place
nearest_point = self.nearest_grid_point()
(nearest_x, nearest_y) = nearest_point
distance = ( abs(x-nearest_x) +
abs(y-nearest_y))
if distance < self.speed * 3/4:
object = self.maze.object_at(nearest_point)
object.eat(self)Last edited by DaWei; Nov 18th, 2007 at 5:55 AM. Reason: Added code tags. Read the rules. |
|
|
|
|
|
#2 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
Re: Pacman in Python Code
You're going to have to tell us the exact error you are getting and the expected behavior. Nobody is going to go through all that; it would be like finding a needle in a haystack without knowing what the needle looks like.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
|
Re: Pacman in Python Code
Quote:
generaly speaking its good practice to break classes into seperate files. It looks that you are copying pasting from the site. are we debugging his code or your code?
__________________
i dont know much about programming but i try to help |
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: Pacman in Python Code
@mrynit :
There's nothing wrong really with having those classes in the same file... in fact I'd say you want them there. It's when your classes handle functionality, in an isolated fashion, that your main program requires, that you don't need them in the same place... I'd say. @acwbrat : Line 128: width = len(layout)[0]) Lines 146-147: garridoman = Garridoman(self, point)
self.movables.append(garridoman)Line 150: self.map[y][x] Food(self, point) Line 153: elif character == 'G' I'm sorry but, I have to stop here. These are all mistakes before the program even finishes compiling... meaning they are the most basic/fundamental and easily fixable errors. If you had spent even an hour looking at previous Python code, or reading a basic tutorial, you would know how to fix all of these errors in an instant. Sorry, but that Pacman tutorial is obviously not a good starting point, I don't think you're learning anything. Take a few steps back and try something else. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Compiled Python code dependencies | titaniumdecoy | Python | 12 | Jun 23rd, 2006 7:04 AM |
| Compiling Python code? | titaniumdecoy | Python | 4 | May 29th, 2006 10:44 AM |
| Neatening some Python code | coldDeath | Python | 12 | Oct 31st, 2005 9:36 AM |
| Convert Python script to C++ code | clanotheduck | Python | 17 | Sep 25th, 2005 8:55 AM |