I am horribly (obssesed?) with writing a visually appealing game (simple) in python. My text adventures didn't appeal to my aufience (family), so I was looking at a tut on making a pac man clone, supposed to be line by line, it seemes choppy. Any ways, here is the code.
#! /usr/bin/env python
import os, sys
import pygame
from pygame.locals import *
from helpers import *
if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'
class PyManMain:
"""The Main PyMan Class - This class handles the main
initialization and creating of the Game."""
def __init__(self, width=640,height=480):
"""Initialize"""
"""Initialize PyGame"""
pygame.init()
"""Set the window Size"""
self.width = width
self.height = height
"""Create the Screen"""
self.screen = pygame.display.set_mode((self.width
, self.height))
def MainLoop(self):
"""This is the Main Loop of the Game"""
"""Load All of our Sprites"""
self.LoadSprites();
"""tell pygame to keep sending up keystrokes when they are
held down"""
pygame.key.set_repeat(500, 30)
"""Create the background"""
self.background = pygame.Surface(self.screen.get_size())
self.background = self.background.convert()
self.background.fill((0,0,0))
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == KEYDOWN:
if ((event.key == K_RIGHT)
or (event.key == K_LEFT)
or (event.key == K_UP)
or (event.key == K_DOWN)):
self.snake.move(event.key)
"""Check for collision"""
lstCols = pygame.sprite.spritecollide(self.snake
, self.pellet_sprites
, True)
"""Update the amount of pellets eaten"""
self.snake.pellets = self.snake.pellets + len(lstCols)
"""Do the Drawging"""
self.screen.blit(self.background, (0, 0))
if pygame.font:
font = pygame.font.Font(None, 36)
text = font.render("Pellets %s" % self.snake.pellets
, 1, (255, 0, 0))
textpos = text.get_rect(centerx=self.background.get_width()/2)
self.screen.blit(text, textpos)
self.pellet_sprites.draw(self.screen)
self.snake_sprites.draw(self.screen)
pygame.display.flip()
def LoadSprites(self):
"""Load the sprites that we need"""
self.snake = Snake()
self.snake_sprites = pygame.sprite.RenderPlain((self.snake))
"""figure out how many pellets we can display"""
nNumHorizontal = int(self.width/64)
nNumVertical = int(self.height/64)
"""Create the Pellet group"""
self.pellet_sprites = pygame.sprite.Group()
"""Create all of the pellets and add them to the
pellet_sprites group"""
for x in range(nNumHorizontal):
for y in range(nNumVertical):
self.pellet_sprites.add(Pellet(pygame.Rect(x*64, y*64, 64, 64)))
class Snake(pygame.sprite.Sprite):
"""This is our snake that will move around the screen"""
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('snake.png',-1)
self.pellets = 0
"""Set the number of Pixels to move each time"""
self.x_dist = 5
self.y_dist = 5
def move(self, key):
"""Move your self in one of the 4 directions according to key"""
"""Key is the pyGame define for either up,down,left, or right key
we will adjust outselfs in that direction"""
xMove = 0;
yMove = 0;
if (key == K_RIGHT):
xMove = self.x_dist
elif (key == K_LEFT):
xMove = -self.x_dist
elif (key == K_UP):
yMove = -self.y_dist
elif (key == K_DOWN):
yMove = self.y_dist
#self.rect = self.rect.move(xMove,yMove);
self.rect.move_ip(xMove,yMove);
class Pellet(pygame.sprite.Sprite):
def __init__(self, rect=None):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('pellet.png',-1)
if rect != None:
self.rect = rect
if __name__ == "__main__":
MainWindow = PyManMain()
MainWindow.MainLoop()
Anyways, how do I change the sprites used? I see the self.loadSprites function, so I was wondering, how do I edit the sprites this function loads? If neccessary, I will give info on the directories they are placed in.
Also, what should I learn/read to better make (also, easy up the development process) these games. I want to make a rpg pacman clone. Not sure exactly what that means, I guess, pick up weapons, hit points, etx (unles hitpoints are too intermediate I could just have lives).
I want to have more than the black maze backround also. Any ideas/suggestions on how to make this game would be appreciated.
Also, where could I find the most in depth tut on making a first game in python?