Skip to content

Commit

Permalink
Code style fixes.
Browse files Browse the repository at this point in the history
Making this example PEP 8 compatible.
  • Loading branch information
n0nick committed Jun 1, 2012
1 parent 44329a6 commit f93a1ad
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions chimp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
"""


#Import Modules
import os, pygame
# Import Modules
import os
import pygame
from pygame.locals import *
from pygame.compat import geterror

if not pygame.font: print ('Warning, fonts disabled')
if not pygame.mixer: print ('Warning, sound disabled')
if not pygame.font:
print ('Warning, fonts disabled')
if not pygame.mixer:
print ('Warning, sound disabled')

main_dir = os.path.split(os.path.abspath(__file__))[0]
data_dir = os.path.join(main_dir, 'data')

#functions to create our resources

# functions to create our resources
def load_image(name, colorkey=None):
fullname = os.path.join(data_dir, name)
try:
Expand All @@ -29,13 +33,15 @@ def load_image(name, colorkey=None):
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()


def load_sound(name):
class NoneSound:
def play(self): pass
def play(self):
pass
if not pygame.mixer or not pygame.mixer.get_init():
return NoneSound()
fullname = os.path.join(data_dir, name)
Expand All @@ -47,11 +53,12 @@ def play(self): pass
return sound


#classes for our game objects
# classes for our game objects
class Fist(pygame.sprite.Sprite):
"""moves a clenched fist on the screen, following the mouse"""
def __init__(self):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
# call Sprite initializer
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('fist.bmp', -1)
self.punching = 0

Expand All @@ -78,7 +85,8 @@ class Chimp(pygame.sprite.Sprite):
"""moves a monkey critter across the screen. it can spin the
monkey when it is punched."""
def __init__(self):
pygame.sprite.Sprite.__init__(self) #call Sprite intializer
# call Sprite initializer
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('chimp.bmp', -1)
screen = pygame.display.get_surface()
self.area = screen.get_rect()
Expand Down Expand Up @@ -126,69 +134,68 @@ def main():
"""this function is called when the program starts.
it initializes everything it needs, then runs in
a loop until the function returns."""
#Initialize Everything
# Initialize Everything
pygame.init()
screen = pygame.display.set_mode((468, 60))
pygame.display.set_caption('Monkey Fever')
pygame.mouse.set_visible(0)

#Create The Backgound
# Create The Backgound
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))

#Put Text On The Background, Centered
# Put Text On The Background, Centered
if pygame.font:
font = pygame.font.Font(None, 36)
text = font.render("Pummel The Chimp, And Win $$$", 1, (10, 10, 10))
textpos = text.get_rect(centerx=background.get_width()/2)
textpos = text.get_rect(centerx=background.get_width() / 2)
background.blit(text, textpos)

#Display The Background
# Display The Background
screen.blit(background, (0, 0))
pygame.display.flip()

#Prepare Game Objects
# Prepare Game Objects
clock = pygame.time.Clock()
whiff_sound = load_sound('whiff.wav')
punch_sound = load_sound('punch.wav')
chimp = Chimp()
fist = Fist()
allsprites = pygame.sprite.RenderPlain((fist, chimp))


#Main Loop
# Main Loop
going = True
while going:
clock.tick(60)

#Handle Input Events
# Handle Input Events
for event in pygame.event.get():
if event.type == QUIT:
going = False
elif event.type == KEYDOWN and event.key == K_ESCAPE:
going = False
elif event.type == MOUSEBUTTONDOWN:
if fist.punch(chimp):
punch_sound.play() #punch
punch_sound.play() # punch
chimp.punched()
else:
whiff_sound.play() #miss
whiff_sound.play() # miss
elif event.type == MOUSEBUTTONUP:
fist.unpunch()

allsprites.update()

#Draw Everything
# Draw Everything
screen.blit(background, (0, 0))
allsprites.draw(screen)
pygame.display.flip()

pygame.quit()

#Game Over
# Game Over


#this calls the 'main' function when this script is executed
# this calls the 'main' function when this script is executed
if __name__ == '__main__':
main()

0 comments on commit f93a1ad

Please sign in to comment.