2013년 12월 17일 화요일

[Python] Ball man Game

자료출처: 마이크로소프트웨어 2001년 08월호 이달의 디스켓

#!/usr/bin/python

# BallMan - Let's play with a redball
# v0.1
# by picxenk  (picxenk@dreamwiz.com)

import os, random
import pygame, pygame.image, pygame.font, pygame.mixer
from pygame.locals import *

#resource
ballmanfile = os.path.join('data', 'ballman1.jpg')
redballfile = os.path.join('data', 'redball.gif')
logofile = os.path.join('data', 'dac2.jpg')
titlefont = os.path.join('data', 'Comicbd.ttf')
tossfile = os.path.join('data', 'toss.wav')
boundfile = os.path.join('data', 'bound.wav')
titlename = 'Ball Man'
clubname = 'Dac club'
version = 'v0.1'

def main():
        pygame.init()
        screen = pygame.display.set_mode((500,500), HWSURFACE|DOUBLEBUF)
        pygame.display.set_caption(titlename + ' ' + version + '   ' + clubname)
        pygame.mouse.set_visible(0)

        back = pygame.Surface(screen.get_size())
        back.fill((255,255,255))

        font = pygame.font.Font(titlefont, 30)
        title = font.render(titlename, 1, (255, 0, 0))
        titlepos = (370, 0)
        back.blit(title, titlepos)

        ballman = pygame.image.load(ballmanfile).convert()
        redball = pygame.image.load(redballfile).convert()
        logo = pygame.image.load(logofile).convert()
        sndtoss = pygame.mixer.Sound(tossfile)
        sndbound = pygame.mixer.Sound(boundfile)

        ballmanpos = ballman.get_rect()
        redballpos = redball.get_rect()
        s = screen.get_rect()
        vermove = 0.0
        hormove = 3
        g = 0.1
        reload = 0

        while 1:

                e = pygame.event.poll()
                if e.type ==QUIT or (e.type == KEYDOWN and e.type == K_ESCAPE):
                        break
               
                redballpos.top += vermove
                vermove += g
                redballpos.left += hormove
                ballmanpos = (pygame.mouse.get_pos()[0]-27, s.bottom-65)
               
                if redballpos.bottom > s.bottom or redballpos.top < s.top:
                        vermove = -(vermove/1.4)
                        hormove = hormove/1.2
                        sndbound.play()
                if redballpos.left < s.left or redballpos.right > s.right:
                        hormove = - hormove
                        sndbound.play()
                       
                pressed = pygame.mouse.get_pressed()[0]
                if not reload and pressed :
                        if redballpos.collidepoint((ballmanpos[0]+27, ballmanpos[1])):               
                                vermove = -(vermove + random.randrange(1, 2, 1))
                                hormove = random.randrange(-3, 3, 1)
                                sndtoss.play()
                                sndbound.play()
                reload = pressed
                if not reload :
                        ballmanpos = ballmanpos[0], ballmanpos[1] + 5

                screen.blit(back, (0, 0))
                screen.blit(logo, (0, 0))
                screen.blit(ballman, ballmanpos)
                screen.blit(redball, redballpos)
                pygame.display.flip()
                       
if __name__ == '__main__':
        main()

댓글 없음:

댓글 쓰기