U E D R , A S I H C RSS

비행기게임/Basis Source


import random, os.path
import pygame
from pygame.locals import *

import Behavior

MAX_SHOTS = 5

if not pygame.image.get_extended():
    raise SystemExit,"sorry, extended image module required"

SCREENRECT = Rect(0,0,1024,768)

def load_image(file):
    file = os.path.join('data',file)
    try:
        surface = pygame.image.load(file)
    except pygame.error:
        raise SystemExit, 'Could not load image "%s"%s'%(file,pygame.get_error)
    return surface.convert()

def load_images(*files):
    imgs = []
    for file in files:
        imgs.append(load_image(file))
    return imgs

class Player(pygame.sprite.Sprite):
    speed = 4

    FRAME = 1
    UP = 0
    REGULAR = 1
    DOWN = 2
    FrameFrequence = 5
    PreviousMove = 0
    count = 0
    imageCount = 0
    imageMax = 15
    imagefrequence = 5

    maxShots = 1

    experience = 0

    def __init__(self):
        pygame.sprite.Sprite.__init__(self,self.containers)
        self.image = self.images[self.REGULAR]
        self.rect = self.image.get_rect()
        self.rect.centerx = SCREENRECT.right
        self.rect.bottom = SCREENRECT.bottom - 100
        self.reloading = 0;
        self.timing = 0;

    def move(self, directionx,directiony, xy):
        if xy=='xy':
            self.rect.move_ip(directionx*self.speed,directiony*self.speed)
        elif xy=='x':
            self.rect.move_ip(directionx*self.speed,0)
        elif xy=='y':
            self.rect.move_ip(0,directiony*self.speed)
        self.rect = self.rect.clamp(SCREENRECT)

    def setImage(self, direction) :
        self.count=self.count+1

    def update(self):
        self.count = self.count + 1
        if self.count%self.imagefrequence == 0:
            if self.imageCount <self.imageMax :
                self.image = self.images[self.imageCount]
                self.imageCount= self.imageCount + 1
                self.count=0
            else :
                self.imageCount = 0

    def gunpos(self) :
        return self.rect.right-130, self.rect.bottom-100


class Shots(pygame.sprite.Sprite) :
    speedx = -10
    speedy = 0

    def __init__(self, pos, kind, speedy=0) :
        pygame.sprite.Sprite.__init__(self, self.containers)
        self.image = self.images[kind]
        self.rect = self.image.get_rect()
        self.rect.midbottom = pos
        self.speedy = speedy

    def update(self) :
        self.rect.move_ip(self.speedx, self.speedy)
        if self.rect.right <= SCREENRECT.left :
            self.kill()

    def pos(self) :
        self.kill()
        return self.rect.centerx, self.rect.centery

class EnemyShot1(Shots):
    speedx = 7
    speedy = 0
    count = 0
    imageCount = 0
    imageMax = 4
    imagefrequence = 5
    playerPosY = 0
    patientOfInducement = 50
    speedIncreaseRateOfY = 0.1

    def __init__(self, pos, playerPosY) :
        pygame.sprite.Sprite.__init__(self, self.containers)
        self.image = self.images[0]
        self.rect = self.image.get_rect()
        self.rect.midbottom = pos
        self.playerPosY = playerPosY

    def update(self):
        if self.rect.centery<(self.playerPosY-self.patientOfInducement):
            self.speedy+=self.speedIncreaseRateOfY
        elif self.rect.centery>(self.playerPosY+self.patientOfInducement):
            self.speedy-=self.speedIncreaseRateOfY

        self.rect.move_ip(self.speedx,self.speedy)
        if self.rect.right >= SCREENRECT.right :
            self.kill()
        self.count = self.count + 1
        if self.count%self.imagefrequence == 0:
            if self.imageCount <self.imageMax :
                self.image = self.images[self.imageCount]
                self.imageCount= self.imageCount + 1
                self.count=0
            else :
                self.imageCount = 0

class SuperClassOfEachClass(pygame.sprite.Sprite):
    def __init__(self):
        pass

    def setContainers(self,arg):
        self.containers = arg
        pygame.sprite.Sprite.__init__(self,self.containers)

    def setImage(self,arg):
        self.image = arg
        self.rect = self.image.get_rect()
        #self.setPos()

    def setImages(self,arg):
        self.images = arg

    def update(self):
        self.rect.move_ip(self.speed,0)

class Item(pygame.sprite.Sprite, SuperClassOfEachClass):
    speed  = 10
    def setPos(self, posx, posy):
        self.rect.centery = posy+100
        self.rect.centerx = posx+100
    def update(self):
        self.rect.move_ip(self.speed,0)

class Enemy(pygame.sprite.Sprite, SuperClassOfEachClass):

    #Default gauage of Enemys
    speedx = 10
    speedy = 2
    imageCount = 0
    imageMax = 5
    imagefrequence = 5
    count = 0
    ImageUpwardShape = 1
    ImageDownwardShape = 0
    life = 1
    pattern = 0
    playerPosY = 0
    shotRate = 9 #If ShotRate is high the enemy shot least than low

    def __init__(self, life, imageMax, playerPosY):
        self.life = life
        self.imageMax = imageMax
        self.playerPosY = playerPosY

    def setSpritePattern(self, pattern):
        self.pattern = pattern

    def setPos(self, posx, posy):
        self.rect.centery = posy+100
        self.rect.centerx = posx+100

    def setImage(self,arg):
        self.image = arg
        self.rect = self.image.get_rect()
        self.sprite = pygame.Surface(self.rect.size)
        #self.sprite.blit(0,(0,0))
        #self.image = self

    def update(self):

        self.count = self.count + 1
        if self.count%self.imagefrequence == 0:
            if self.imageCount <self.imageMax and self.imageCount >= 0 :
                self.image = self.images[self.imageCount]
                if self.ImageUpwardShape==1:
                    self.imageCount = self.imageCount + 1

                else:
                    self.imageCount = self.imageCount -1
            elif self.imageCount==self.imageMax-1:
                self.ImageUpwardShape = 0
            elif self.imageCount==0:
                self.ImageUpwardShape = 1
            else:
                self.imageCount = 0

        if self.count%(self.imagefrequence*self.shotRate) == 0:
            EnemyShot1(self.gunpos(), self.playerPosY)
            self.count = 0

        Behavior.Behavior(self,self.pattern)
        if self.rect.right >= SCREENRECT.right :
            self.kill()

    def gunpos(self) :
        return self.rect.right, self.rect.bottom


class Explosion(pygame.sprite.Sprite):
    defaultLife = 10;

    def __init__(self,pos):
        pygame.sprite.Sprite.__init__(self,self.containers)
        self.image = self.images[0]
        self.rect = self.image.get_rect()
        self.rect.centerx = pos[0]
        self.rect.centery = pos[1]
        self.lifes = self.defaultLife

    def update(self):
        self.lifes = self.lifes- 1
        if self.lifes <0:
            self.kill()

class Building(pygame.sprite.Sprite,SuperClassOfEachClass):
    rect = Rect(0,0,0,0)
    def __init__(self):
        self.speed = 15

    def setImage(self,arg):
        self.image = arg
        self.rect = self.image.get_rect()
        self.setPos()

    def setPos(self):
        self.rect.centerx = SCREENRECT.left
        self.rect.centery = (SCREENRECT.bottom/10)*int(9)

    def update(self):
        self.rect.move_ip(self.speed,0)

class Score:
    def __init__(self):
        self.score = 0

    def plusScore(self,arg):
        if arg == 1:
            self.score += 10
        elif arg == 2:
            self.score += 20
        self.printScore()

    def printScore(self):
        # To do show in GUI
        print self.score


def DynamicEnemyAssign(enemyList, countOfEnemy, Enemy, life, imageMax, enemy_containers, Enemy_img, pathAndKinds, line, playerPosY):
    enemyList[countOfEnemy] = Enemy(life,imageMax, playerPosY)
    enemyList[countOfEnemy].setContainers(enemy_containers)
    enemyList[countOfEnemy].setImage(Enemy_img[0])
    enemyList[countOfEnemy].setImages(Enemy_img)
    enemyList[countOfEnemy].setSpritePattern(pathAndKinds[line][4])
    enemyList[countOfEnemy].setPos(SCREENRECT.left,(SCREENRECT.bottom/10)*int(pathAndKinds[line][0]))

def DynamicItemAssign(itemList, countOfItem, Item, imageMax, item_containers, Item_img ,pos):
    itemList[countOfItem] = Item()
    itemList[countOfItem].setContainers(item_containers)
    itemList[countOfItem].setImage(Item_img[0])
    itemList[countOfItem].setImages(Item_img)
    itemList[countOfItem].setPos(pos[0], pos[1])

def SearchKilledEnemy(enemyList, Enemy):
    for enemy in EnemyenemyList:
        if enemy.life == 0 :
            return 1
    return 0


def main(winstyle = 0):

    #Initialize
    pygame.init()

    #Set the display mode
    winstyle = 0
    bestdepth = pygame.display.mode_ok(SCREENRECT.size, winstyle,32)
    screen = pygame.display.set_mode(SCREENRECT.size, winstyle,bestdepth)

    #create the background, tile the bgd image
    background = pygame.Surface(SCREENRECT.size)

    #Initialize Game groups
    all = pygame.sprite.RenderUpdates()
    shots = pygame.sprite.Group()
    player1 = pygame.sprite.Group()
    enemy_sprite = pygame.sprite.Group()
    enemy2_sprite = pygame.sprite.Group()
    building_1_sprite = pygame.sprite.Group()
    enemyShot1 = pygame.sprite.Group()
    item_sprite = pygame.sprite.Group()

    #asign default groups to each sprite class
    Player.containers = all,player1
    Shots.containers = shots, all
    enemy_containers = enemy_sprite, all
    building_1_containers = building_1_sprite, all
    EnemyShot1.containers = all, enemyShot1
    item_containers = all, item_sprite
    enemy2_containers = all, enemy2_sprite

    Explosion.containers = all

    #assign instance ty each class
    building_1 = Building()
    building_1.setContainers(building_1_containers)

    score = Score()

    #Load images, assign sprite classes
    Enemy1_img = load_images('enemy1_000.gif','enemy1_002.gif','enemy1_004.gif','enemy1_006.gif','enemy1_008.gif')
    #enemy_1[0].setImage(Enemy1_img[0])
    #enemy_1[0].setImages(Enemy1_img)
    shotImgs = load_images('bullet1.gif', 'bullet2.gif', 'bullet3.gif', 'bullet4.gif')
    Shots.images = shotImgs
    imgs = load_images('dragon000.gif','dragon002.gif','dragon004.gif','dragon006.gif','dragon008.gif','dragon010.gif','dragon012.gif','dragon014.gif','dragon016.gif','dragon018.gif','dragon020.gif','dragon022.gif','dragon024.gif','dragon026.gif','dragon028.gif','dragon030.gif')
    Player.images = imgs

    img = load_image('building_1.gif')
    building_1.setImage(img)

    img = load_image('explosion1.gif')
    Explosion.images = [img]

    img = load_images('ShotOfEnemy1_000.gif','ShotOfEnemy1_002.gif','ShotOfEnemy1_004.gif','ShotOfEnemy1_006.gif')
    EnemyShot1.images = img
    Enemy2_img = load_images('enemy2_000.gif','enemy2_002.gif','enemy2_004.gif','enemy2_006.gif')
    Enemy3_img = load_images('enemy3_000.gif','enemy3_002.gif','enemy3_004.gif','enemy3_006.gif','enemy3_008.gif')
    Enemy4_img = load_images('enemy4_000.gif','enemy4_002.gif','enemy4_004.gif','enemy4_006.gif')
    Enemy5_img = load_images('enemy5_000.gif','enemy5_002.gif','enemy5_004.gif','enemy5_006.gif')
    Enemy6_img = load_images('enemy6.gif')
    #Enemy2.images = img
    item1_img = load_images('item1.gif')

    #decorate the game window
    pygame.mouse.set_visible(0)

    #Create some starting values
    clock = pygame.time.Clock()

    #initialize our starting sprites
    player = Player()

    #open the path and kind of enemy
    file = open(os.path.join('PathOfEnemy.txt'))
    pathAndKinds = file.readlines()
    one = 0
    line = 0
    countOfEnemy = 0
    MAX_ENEMY = 5
    MAX_ITEM = 6
    Enemy1Kill = 0

    #initialize list of Enemys
    enemy_1 = range(MAX_ENEMY)
    enemy_2 = range(MAX_ENEMY)
    item_1 = range(MAX_ITEM)

    curGun = 0

    while player.alive():

        #get input
        for event in pygame.event.get():
            if event.type == QUIT or \
                (event.type == KEYDOWN and event.key==K_ESCAPE):
                return
            if event.type == KEYDOWN :
                if event.key == K_0 :
                    player.maxShots = player.maxShots + 1
                    if player.maxShots == 4 :
                        player.maxShots = 1

        keystate = pygame.key.get_pressed()

        #clear/erase the last drawn sprites
        all.clear(screen, background)

        #handle player input
        direction1 = keystate[K_RIGHT] - keystate[K_LEFT]
        direction2 = keystate[K_DOWN] - keystate[K_UP]
        if direction1 !=0 and direction2 != 0 :
            if direction2 < 0 :
                player.setImage(Player.UP)
            elif direction2 > 0 :
                player.setImage(Player.DOWN)
            player.move(direction1,direction2,'xy')

        elif direction1!=0:
            player.move(direction1,direction2,'x')

        elif direction2!=0:
            if direction2 < 0 :
                player.setImage(Player.UP)
            elif direction2 > 0 :
                player.setImage(Player.DOWN)
            player.move(direction1,direction2,'y')

        elif direction2 == 0 :
            player.setImage(Player.REGULAR)

        firing = keystate[K_SPACE]

        if player.reloading and firing and len(shots) <= MAX_SHOTS and player.timing % 10 is 0:
            if curGun == 3 :
                x,y = player.gunpos()

                for i in range(-30 * (player.maxShots - 1) + y, 30 * (player.maxShots - 1) + y + 1 , 30) :
                    shot = Shots((x,i), curGun, (i-y)/30)
            else :
                Shots(player.gunpos(), curGun)

        if player.timing % 2 is 0 :
            player.reloading = firing

        player.reloading = firing
        player.timing += 1

        #Enemy & Building Control Statements...
        if player.timing%70==0:
            if(int(pathAndKinds[line][2])==0):
                DynamicEnemyAssign(enemy_1, countOfEnemy, Enemy, 1, 4, enemy_containers, Enemy3_img, pathAndKinds, line, player.gunpos()[1])

            elif(int(pathAndKinds[line][2])==1):
                DynamicEnemyAssign(enemy_2, countOfEnemy, Enemy,3, 4, enemy_containers, Enemy2_img, pathAndKinds, line, player.gunpos()[1])

            building_1.setPos()
            if countOfEnemy>=MAX_ENEMY-1:
                countOfEnemy=-1
            countOfEnemy +=1
            line += 1

        for shot in pygame.sprite.groupcollide(shots,enemy_sprite,1,1).keys():
            Explosion(shot.pos())
            DynamicItemAssign(item_1, countOfEnemy, Item, 1, item_containers, item1_img, shot.pos())
            score.plusScore(1)

        for shot in pygame.sprite.groupcollide(shots,enemy_sprite,1,1).keys():
            Explosion(shot.pos())
            score.plusScore(2)

        for shot in pygame.sprite.groupcollide(item_sprite, player1, 1, 0).keys() :
            player.experience += 1
            if player.experience is 30 and curGun != 3:
                curGun += 1
                player.experience = 0

            elif curGun == 3 and player.experience is 30 :
                player.maxShots += 1

        #update all the sprites
        all.update()
        #draw the scene
        dirty = all.draw(screen)
        pygame.display.update(dirty)

        #cap the framerate
        clock.tick(80)



#call the "main" function if running this script
if __name__=='__main__':main()
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:38
Processing time 0.0208 sec