first pygame study, python
#!/usr/bin/python
import sys, os
import pygame
if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'
def get_image_dir():
return "data"
def get_sound_dir():
return "data"
#return image surface
def load_image(name):
fullname = os.path.join(get_image_dir(), name)
try:
image = pygame.image.load(fullname)
except pygame.error, message:
print fullname, message
raise SystemExit, message
image = image.convert()
return image
#end funcion load_image
def load_sound(name):
class NoneSound:
def play(self): pass
if not pygame.mixer or not pygame.mixer.get_init():
return NoneSound()
fullname = os.path.join(get_sound_dir(), name)
try:
sound = pygame.mixer.Sound(fullname)
except pygame.error, message:
print fullname, message
raise SystemExit, message
return sound
#end function load_sound
def create_font(size, color, text):
if not pygame.font:
return None
font = pygame.font.Font(None, size)
surface = font.render(text, True, color)
rect = surface.get_rect()
return surface, rect
#end function create_font
def quit_game():
print "Game quit, bye!"
bye_sound = load_sound("goodbye.wav")
bye_sound.play()
pygame.time.wait(1*1000)
sys.exit(0)
#end function
def main():
#Initialize Everything
game_size = width,height = 320,240
pygame.init()
screen = pygame.display.set_mode(game_size) #surface
pygame.display.set_caption('kf701 first pygame app')
#Print system info
print "Game base on python, pygame, SDL"
print "Pygame ver:", pygame.version.ver
print "SDL ver:", pygame.get_sdl_version()
print "Game for Myself, Start"
#Create The Backgound Surface
bgcolor = 255,255,255
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(bgcolor)
#Put Text On The Background
string = "study pygame first"
textsurface, textrect = create_font(30, (10,100,10), string)
textrect.center = (width/2,height/2)
background.blit(textsurface, textrect)
#FPS control
game_fps = 25
clock = pygame.time.Clock()
#image
image = load_image("ball.gif")
image.set_alpha(80)
image_rect = image.get_rect()
image_step = [2, 2]
circle_rect = pygame.Rect(width-20,20,20,20)
circle_step = [2, 2]
#Sound
punch_sound = load_sound("punch.wav")
#Start main loop
while True:
clock.tick(game_fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game()
image_rect = image_rect.move(image_step)
if image_rect.left width:
image_step[0] = -image_step[0]
punch_sound.play()
if image_rect.top height:
image_step[1] = -image_step[1]
punch_sound.play()
circle_rect = circle_rect.move(circle_step)
if circle_rect.left width:
circle_step[0] = -circle_step[0]
punch_sound.play()
if circle_rect.top height:
circle_step[1] = -circle_step[1]
punch_sound.play()
# update the full display Surface to the screen
screen.blit(background, (0,0))
screen.blit(image, image_rect)
pygame.draw.circle(screen, (100,50,50), circle_rect.center, 20)
pygame.display.flip()
# end main loop
# end main function
if __name__ == "__main__":
main()