I'm a newbie to python, and I was wondering whether anyone could give me any pointers on how to improve the following code;
import pygame, sys, time
from pygame.locals import *
# Define defaults
bg = (0,0,0) # background colour
spl = (255,255,255) # splash colour
ver = "INDEV v1.0" # current version
img = "icn.png" # corner icon (UNUSED)
cur = "cur.png" # cursor icon
player = "player.png" # player
logo = "pixl.png" # splash logo
currtile_x = 0
currtile_y = 0
# Player details
px = 20
py = 20
speedy = 25
up = False
down = False
left = False
right = False
singlerun = 1
# Load Map
with open('townhall.map', 'r') as f:
for line in f:
for character in line:
if character == "\n":
print "Newline"
else:
if character == "x":
print "WALL"
else:
if character == "a":
print "LAND"
# Other
completed = 0
clock = pygame.time.Clock()
splashboot = 0
# Initialise screen
pygame.init()
pygame.mouse.set_visible(False)
screen = pygame.display.set_mode((640, 420))
pygame.display.set_caption('The Missing Piece ' + ver)
# Unused
#icon = pygame.image.load(img).convert_alpha()
#pygame.display.set_icon(icon)
# Initialise sprites
try:
cursor = pygame.image.load(cur).convert_alpha()
logo = pygame.image.load(logo).convert_alpha()
player = pygame.image.load(player).convert_alpha()
except:
print "Unexpected error loading sprites!"
raw_input("Press ENTER to exit")
pygame.quit()
raise
# Splash screen
while splashboot != 25:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
splash = pygame.Surface(screen.get_size())
splash = splash.convert()
splash.fill(spl)
x, y = screen.get_size()
gx = x - 600
yx = (y / 2) - 25
font = pygame.font.Font(None, 20)
splashver = font.render(ver, 20, (0, 0, 0))
screen.blit(splash, (0, 0))
screen.blit(logo, (gx,yx))
screen.blit(splashver, (520, yx + 50))
pygame.display.update()
splashboot += 1
time.sleep(0.1)
print splashboot
# Fill background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(bg)
# Define instructions
font = pygame.font.Font(None, 20)
ver = font.render("The Missing Piece " + ver, 20, (0, 0, 0))
conl1 = font.render("W = UP", 20, (0, 0, 0))
conl2 = font.render("A = LEFT", 20, (0, 0, 0))
conl3 = font.render("S = DOWN", 20, (0, 0, 0))
conl4 = font.render("D = RIGHT", 20, (0, 0, 0))
# Event loop
while completed != 1:
dt = clock.tick(60)
speed = speedy / float(dt)
for event in pygame.event.get():
if event.type == QUIT:
completed = 1
pygame.quit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
left = True
if event.key == K_RIGHT:
right = True
if event.key == K_UP:
up = True
if event.key == K_DOWN:
down = True
if event.key == K_a:
left = True
if event.key == K_d:
right = True
if event.key == K_w:
up = True
if event.key == K_s:
down = True
if event.type == KEYUP:
if event.key == K_LEFT:
left = False
if event.key == K_RIGHT:
right = False
if event.key == K_UP:
up = False
if event.key == K_DOWN:
down = False
if event.key == K_a:
left = False
if event.key == K_d:
right = False
if event.key == K_w:
up = False
if event.key == K_s:
down = False
# direction scripts
if up:
py -= speed
if down:
py += speed
if left:
px -= speed
if right:
px += speed
# cursor settings
mousex,mousey = pygame.mouse.get_pos()
mousex -= cursor.get_width()/2
mousey -= cursor.get_height()/2
x, y = screen.get_size()
gx = x - 200
# draw background
screen.blit(background, (0, 0))
#pygame.draw.rect(screen, (255,255,255), (10,10,200,200), 0) # testbox
pygame.draw.rect(screen, (255,255,255), (gx,0,200,y), 0) # sidebar
# draw player
screen.blit(player, (px, py))
# draw version
screen.blit(ver, (gx,1))
# draw instructions
screen.blit(conl1, (gx+2,20))
screen.blit(conl2, (gx+2,32))
screen.blit(conl3, (gx+2,44))
screen.blit(conl4, (gx+2,56))
# draw cursor
screen.blit(cursor, (mousex, mousey))
pygame.display.update()
I have been given one or two suggestions about snippets of the code, but I would like to know how to make my code more efficient and simpler, as in this, I seem to have gone the long / difficult way around :(
Here is a zip containing the resources; http://www.mediafire.com/?twuww34c3b759cj