I'm coding a pac man clone but I cant place the walls correctly. Heres my code:
#Pac-man
#A clone of the classic game, Pac-man
import pygame,sys
from pygame.locals import *pygame.init()WINWIDTH=640 #width of the window in
pixels
WINHEIGHT=480 #height of the window in pixelsFPS=30 #frames per second#
r g b
YELLOW=(255,255, 0)
BLACK= ( 0, 0, 0)
WHITE= (255,255,255)
BLUE= ( 0, 0,255)
RED= (255, 0, 0)#the layout of walls on the level
levelLayout="""
________| __ || |__| ||________|
"""def get_text(underline,text,size,pos,color):
"""Returns a rect object and a surface object to be displayed on the
screen. """
BASICFONT=pygame.font.Font('freesansbold.ttf',size)
BASICFONT.set_underline(underline) surfObj=BASICFONT.render(text,True,color)
rectObj=surfObj.get_rect()
rectObj.center=(pos) return surfObj,rectObjdef run_game():
"""Iterates through the game loop until the player is game over. """
#default variables for the pac-man object that the player controls
pacPosX=int(WINWIDTH/2)
pacPosY=int(WINHEIGHT/2) #tracks if the player is game over or not
game_over=False #tracks which direction the player wants to move in,
stationary by default
direction=None #tracks which part of the level layout is being iterated
over
indexX=0
indexY=0 lineList=[] while not game_over:
DISPLAYSURF.fill(BLACK) for wall in levelLayout: #iterates
through the level layout and places walls accordingly
if wall=="|":
lineList.append([(indexX,indexY),(indexX,indexY+21)])
elif wall=="_":
lineList.append([(indexX,indexY),(indexX+80,indexY)])
indexX+=80 if indexX==WINWIDTH:
indexX=0
indexY+=160
indexX=0 #reset the X index
indexY=0 #reset the Y index
print(lineList) for drawWall in range(len(lineList)): #draws the
walls onto the screen
pygame.draw.line(DISPLAYSURF,BLUE,lineList[drawWall][0],lineList[drawWall][1])
#draws pac man
pygame.draw.circle(DISPLAYSURF,YELLOW,(pacPosX,pacPosY),10)
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
elif event.type==KEYUP: #find out which direction the player wants
pac man to move in
if event.key==K_LEFT:
direction='left'
elif event.key==K_RIGHT:
direction='right'
elif event.key==K_UP:
direction='up'
elif event.key==K_DOWN:
direction='down' if direction: #if the player wants
to move pac man, move him in the desired direction
if direction=='left':
pacPosX-=6
elif direction=='right':
pacPosX+=6
elif direction=='up':
pacPosY-=6
elif direction=='down':
pacPosY+=6 #wrap pac man to the opposite end of the
screen if he goes off
if pacPosX<0:
pacPosX=WINWIDTH
elif pacPosX>WINWIDTH:
pacPosX=0
elif pacPosY<0:
pacPosY=WINHEIGHT
elif pacPosY>WINHEIGHT:
pacPosY=0 pygame.display.update()
FPSCLOCK.tick(FPS)def main():
global DISPLAYSURF,FPSCLOCK
DISPLAYSURF=pygame.display.set_mode((WINWIDTH,WINHEIGHT))
pygame.display.set_caption('Pac-Man') FPSCLOCK=pygame.time.Clock()
while True:
run_game()main()
For some reason, the walls end up all over the place and I'm not too sure why.
Any help is much appreciated. Myles Broomes