On Saturday 02 September 2006 11:41, you wrote:
> [EMAIL PROTECTED] wrote:
> > Hi. I'm new to Python . . .very new. I was just wondering, once I've
> > written a program that opens the graphics window and I've got some
> > things going on in the grahics window, how do I display text in the
> > grahics window? I need to be able to display changeable text, so
that
> > the words and numbers can be altered with what is going on inside
the
> > graphics window. If that doesn't make much sense, maybe I could try
> > sending a bit of the code from my program.
>
> A bit of code would certainly help. How did you open this "graphics
> window"?
>
>
> Richard
Okay, I'm not sure what part of the code is most needed, so I'll just
give the whole thing, note that the opening of this "graphics window"
as I called it, occurred in the "Maze" class. Also note that I am using
version 2.4.3 of Python and work in the GUI IDLE.
from livewires import *
grid_size = 30
margin = grid_size
background_colour = Colour.black
wall_colour = make_colour(0.6, 0.9, 0.9)
pacman_colour = Colour.yellow
pacman_size = grid_size * 0.8
pacman_speed = 0.25
food_colour = Colour.red
food_size = grid_size * 0.15
ghost_colours = []
ghost_colours.append(Colour.red)
ghost_colours.append(Colour.green)
ghost_colours.append(Colour.blue)
ghost_colours.append(Colour.purple)
ghost_speed = 0.25
capsule_colour = Colour.white
capsule_size = grid_size * 0.3
scared_colour = Colour.white
scared_time = 300
warning_time = 50
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 )
]
# The shape of the maze. Each character
# represents a different type of object.
# % - Wall
# . - Food
# o - Capsule
# G - Ghost
# P - Pacman
# Other characters are ignored
the_layout = [
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
"%.....%.................%.....%",
"%o%%%.%.%%%.%%%%%%%.%%%.%.%%%o%",
"%.%.....%......%......%.....%.%",
"%...%%%.%.%%%%.%.%%%%.%.%%%...%",
"%%%.%...%.%.........%.%...%.%%%",
"%...%.%%%.%.%%% %%%.%.%%%.%...%",
"%.%%%.......%GG GG%.......%%%.%",
"%...%.%%%.%.%%%%%%%.%.%%%.%...%",
"%%%.%...%.%.........%.%...%.%%%",
"%...%%%.%.%%%%.%.%%%%.%.%%%...%",
"%.%.....%......%......%.....%.%",
"%o%%%.%.%%%.%%%%%%%.%%%.%.%%%o%",
"%.....%........P........%.....%",
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
]
class Immovable:
def is_a_wall(self):
return 0
def eat(self, pacman):
pass
class Nothing(Immovable):
pass
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)
self.movables = []
self.food_count = 0
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 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
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)
elif character == 'P':
pacman = Pacman(self, point)
self.movables.append(pacman)
elif character == '.':
self.food_count = self.food_count + 1
self.map[y][x] = Food(self, point)
elif character == 'G':
ghost = Ghost(self, point)
self.movables.append(ghost)
elif character == 'o':
self.map[y][x] = Capsule(self, point)
def finished(self):
return self.game_over
def play(self):
for movable in self.movables:
movable.move()
sleep(0.05)
def object_at(self, point):
(x,y ) = point
if y < 0 or y >= self.height:
return Nothing()
if x < 0 or x >= self.width:
return Nothing()
return self.map[y][x]
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 remove_capsule(self, place):
(x, y) = place
self.map[y][x] = Nothing()
for movable in self.movables:
movable.capsule_eaten()
def pacman_is(self, pacman, point):
for movable in self.movables:
movable.pacman_is(pacman, point)
def win(self):
#! /usr/bin/env python
import Pacman01
print "YOU WIN!"
self.game_over = 1
def lose(self):
print "YOU LOSE!"
self.game_over = 1
def done(self):
end_graphics()
self.map = []
self.movables = []
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()
(x, y) = self.place
neighbours = [ (x+1, y), (x-1, y),
(x, y+1), (x, y-1) ]
for neighbour in neighbours:
self.check_neighbour(neighbour)
allow_movables()
def is_a_wall(self):
return 1
def check_neighbour(self, neighbour):
maze = self.maze
object = maze.object_at(neighbour)
if object.is_a_wall():
here = self.screen_point
there = maze.to_screen(neighbour)
line(here, there, colour = wall_colour)
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)
def capsule_eaten(self):
pass
class Pacman(Movable):
def __init__(self, maze, point):
self.direction = 0
Movable.__init__(self, maze, point,
pacman_speed)
def move(self):
keys = keys_pressed()
if 'a' in keys: self.move_left()
elif 'd' in keys: self.move_right()
elif 'w' in keys: self.move_up()
elif 's' in keys: self.move_down()
elif 'x' in keys: self.move_down()
self.maze.pacman_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 draw(self):
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, pacman_size,
colour = pacman_colour,
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 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)
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 pacman_is(self, pacman, point):
pass
class Food(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
self.dot = circle(screen_x, screen_y,
food_size,
colour = food_colour,
filled = 1)
def eat(self, pacman):
remove_from_screen(self.dot)
self.maze.remove_food(self.place)
class Ghost(Movable):
def __init__(self, maze, start):
global ghost_colours
self.next_point = start
self.movement = (0, 0)
self.colour = ghost_colours[0]
ghost_colours[:1] = []
ghost_colours.append(self.colour)
self.original_colour = self.colour
self.time_left = 0
Movable.__init__(self, maze,
start, ghost_speed)
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)
coords.append(y*grid_size + screen_y)
self.body = polygon(coords, self.colour,
closed = 1,
filled = 1)
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 < warning_time:
if time_left % 2 ==0:
colour = self.original_colour
else:
colour = scared_colour
self.change_colour(colour)
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.can_move_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.furthest_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 pacman_is(self, pacman, 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(pacman)
def bump_into(self, pacman):
if self.time_left !=0:
self.captured(pacman)
else:
self.maze.lose()
def captured(self, pacman):
self.place = self.start
self.colour = self.original_colour
self.time_left = 0
self.redraw()
def capsule_eaten(self):
self.change_colour(scared_colour)
self.time_left = scared_time
def change_colour(self, new_colour):
self.colour = new_colour
self.redraw()
def redraw(self):
old_body = self.body
self.draw()
remove_from_screen(old_body)
class Capsule(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
self.dot = circle(screen_x, screen_y,
capsule_size,
colour = capsule_colour,
filled = 1)
def eat(self, pacman):
remove_from_screen(self.dot)
self.maze.remove_capsule(self.place)
the_maze = Maze()
while not the_maze.finished():
the_maze.play()
the_maze.done()
Hope that helps
_____________________________________________________________________
PrivatePhone - FREE telephone number & voicemail.
A number so private, you can make it public.
http://www.privatephone.com
--
http://mail.python.org/mailman/listinfo/python-list