您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用Python代碼實(shí)現(xiàn)簡單飛機(jī)大戰(zhàn)游戲”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用Python代碼實(shí)現(xiàn)簡單飛機(jī)大戰(zhàn)游戲”吧!
功能
玩家飛機(jī)可以移動(dòng),可以發(fā)射子彈,敵機(jī)隨機(jī)產(chǎn)生,自由墜落。未添加擊落敵機(jī)的功能。主要用來練習(xí)類的封裝與繼承。
源碼
# -*- coding=utf-8 -*- import pygame, random from pygame.locals import * class Base(object): def __init__(self, x, y, screen): self.screen = screen self.x = x self.y = y class Player(Base): def __init__(self): super().__init__(210, 600, screen) self.playerName = "./feiji/hero.gif" self.player = pygame.image.load(self.playerName).convert() self.bullet_list = [] def display(self): self.screen.blit(self.player, (self.x, self.y)) for bullet in self.bullet_list: bullet.display() bullet.mov() def move_left(self): self.x -= 10 def move_right(self): self.x += 10 def move_up(self): self.y -= 10 def move_down(self): self.y += 10 def shot(self): bullet = Bullet() self.bullet_list.append(bullet) class Bullet(Base): def __init__(self): super().__init__(player.x+40, player.y-20, screen) self.bulletName = "./feiji/bullet-3.gif" self.bullet = pygame.image.load(self.bulletName).convert() def display(self): self.screen.blit(self.bullet, (self.x, self.y)) def mov(self): self.y -= 10 class Enemy(Base): def __init__(self): super().__init__(random.randint(1, 10)*40, 0, screen) self.enemyName = "./feiji/enemy-1.gif" self.enemy = pygame.image.load(self.enemyName).convert() self.enemy_list = [] self.i = 0 def mov(self): self.y += 2 class EnemyArea(Enemy): def display(self): # 每隔一段時(shí)間創(chuàng)建一架敵機(jī) if self.i % 80 == 0: new_enemy = Enemy() self.enemy_list.append(new_enemy) self.i += 1 for enemy in self.enemy_list: self.screen.blit(enemy.enemy, (enemy.x, enemy.y)) enemy.mov() if __name__ == "__main__": # 創(chuàng)建一個(gè)窗口,用來顯示內(nèi)容 screen = pygame.display.set_mode((480, 890)) # 創(chuàng)建一個(gè)和窗口大小的圖片,用來充當(dāng)背景 background = pygame.image.load("./feiji/background.png").convert() player = Player() enemy = Enemy() enemy_area = EnemyArea() while True: screen.blit(background, (0, 0)) player.display() enemy_area.display() # 判斷按鍵 for event in pygame.event.get(): if event.type == QUIT: exit() elif event.type == KEYDOWN: if event.key == K_a or event.key == K_LEFT: player.move_left() elif event.key == K_d or event.key == K_RIGHT: player.move_right() elif event.key == K_w or event.key == K_UP: player.move_up() elif event.key == K_s or event.key == K_DOWN: player.move_down() elif event.key == K_SPACE: player.shot() elif event.key == K_q: exit() pygame.display.update() pygame.time.delay(10)
結(jié)果展示
感謝各位的閱讀,以上就是“怎么用Python代碼實(shí)現(xiàn)簡單飛機(jī)大戰(zhàn)游戲”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)怎么用Python代碼實(shí)現(xiàn)簡單飛機(jī)大戰(zhàn)游戲這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。