python 簡答打飛機(物件導向)

來源:互聯網
上載者:User

標籤:import   sep   +=   預設   plane   rect   ase   back   append   

  1 import pygame  2 import time  3 import random  4 from pygame.locals import *  5   6   7 class Base(object):  8     def __init__(self, screen_temp, x, y, image_name):  9         self.x = x 10         self.y = y 11         self.screen = screen_temp 12         self.image = pygame.image.load(image_name) 13  14  15 class BasePlane(Base): 16     def __init__(self, screen_temp, x, y, image_name): 17         Base.__init__(self, screen_temp, x, y, image_name) 18         self.bullet_list = []  # 儲存發射出去的子彈對象的引用 19  20     def display(self): 21         self.screen.blit(self.image, (self.x, self.y)) 22  23         for bullet in self.bullet_list: 24             bullet.display() 25             bullet.move() 26  27             if bullet.judge():  # 判斷子彈是否越界 28                 self.bullet_list.remove(bullet) 29  30  31 class HeroPlane(BasePlane): 32     def __init__(self, screen_temp): 33         # 也可以用super().__init__(), 用這個方法不用加參數self 34         BasePlane.__init__(self,screen_temp,210,700,"./feiji/hero1.png") 35  36     def move_left(self): 37         self.x = self.x - 5 38  39     def move_right(self): 40         self.x = self.x + 5 41  42     def fire(self): 43         self.bullet_list.append(Bullet(self.screen, self.x, self.y)) 44  45  46 class EnemyPlane(BasePlane): 47     """敵機的類""" 48     def __init__(self, screen_temp): 49         BasePlane.__init__(self,screen_temp,0,0,"./feiji/enemy0.png") 50         self.direction = "right"  # 用來儲存飛機預設的顯示方向 51  52  53     def move(self): 54         if self.direction == "right": 55             self.x += 5 56  57         elif self.direction == "left": 58             self.x -= 5 59  60         if self.x > 430: 61             self.direction = "left" 62         elif self.x < 0: 63             self.direction = "right" 64  65     def fire(self): 66         random_num = random.randint(1,100) 67         if random_num == 8 or random_num == 88:     #降低子彈重複出現的可能 68             self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y)) 69  70  71 class BaseBullet(Base): 72     def __init__(self, screen_temp, x, y, image_name): 73         Base.__init__(self, screen_temp, x, y, image_name) 74  75     def display(self): 76         self.screen.blit(self.image, (self.x, self.y)) 77  78  79 class Bullet(BaseBullet): 80     def __init__(self, screen_temp, x, y): 81         BaseBullet.__init__(self,screen_temp,x+40,y-20,"./feiji/bullet.png") 82  83     def move(self): 84         self.y = self.y - 5 85  86     def judge(self): 87         if self.y < 0: 88             return True 89         else: 90             return False 91  92  93 class EnemyBullet(BaseBullet): 94     def __init__(self, screen_temp, x, y): 95  96         BaseBullet.__init__(self,screen_temp,x+25,y+40,"./feiji/bullet1.png") 97  98     def move(self): 99         self.y = self.y + 5100 101     def judge(self):102         if self.y > 852:103             return True104         else:105             return False106 107 108 def key_control(hero_temp):109     # 擷取事件,比如按鍵等110     for event in pygame.event.get():111 112         if event.type == QUIT:113             print("exit")114             exit()115 116         elif event.type == KEYDOWN:117             if event.key == K_a or event.key == K_LEFT:118                 print(‘left‘)119                 # x = x - 5120                 hero_temp.move_left()121 122             elif event.key == K_d or event.key == K_RIGHT:123                 print("right")124                 # x = x + 5125                 hero_temp.move_right()126 127             elif event.key == K_SPACE:128                 hero_temp.fire()129                 print("space")130 131 132 def main():133     # 建立一個視窗,用來顯示內容134     screen = pygame.display.set_mode((480, 852), 0, 32)135 136     # 建立一個和視窗大小相同的圖片,用來當背景137     background = pygame.image.load("./feiji/background.png")138 139     # 建立一個飛機140     hero = HeroPlane(screen)141 142     # 建立一個敵機143     enemy = EnemyPlane(screen)144 145     # 把背景圖片放到視窗中顯示146     while True:147         # 設定需要顯示的背景圖148         screen.blit(background, (0, 0))149 150         # 顯示飛機151         hero.display()152         enemy.display()153         enemy.move()  # 調用敵機的移動方法154         enemy.fire()155 156         # 更新需要顯示的內容157         pygame.display.update()158 159         key_control(hero)160 161         # 延遲降低CPU使用率162         time.sleep(0.01)163 164 165 if __name__ == ‘__main__‘:166     main()

 

python 簡答打飛機(物件導向)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.