導航:首頁 > 編程語言 > 航班排序python

航班排序python

發布時間:2024-06-09 16:26:32

Ⅰ 如何為pygame的飛機大戰增加一個分數排行榜python

import pygame
from plane_sprites import*
from Background import BackGround
from Enemy import Enemy
from Hero import Hero
from time import sleep
from Bullet import Bullet

class Plane_main(object):
def __init__(self):
print('游戲初始化')
self.screen = pygame.display.set_mode(SCREEN_RECT.size)
self.clock = pygame.time.Clock()
self.__create_sprites()

pygame.time.set_timer(CREATE_ENEMY_EVENT,1000)
pygame.time.set_timer(HERO_FIRE_EVENT, 500)
self.count = 0
self.score = 0
def __create_sprites(self):
# 背景組
self.back_group = pygame.sprite.Group()
bg1 = BackGround()
bg2 = BackGround(True)
pygame.mixer.init()
pygame.mixer.music.load("./Capo Proctions - Journey 00_00_00-00_00_59.ogg")
pygame.mixer.music.play(-1)

self.back_group.add(bg1,bg2)
# 敵機組
self.enemy_group = pygame.sprite.Group()
#敵級銷毀組
self.enemy1_down_group=pygame.sprite.Group()
# 英雄組
self.hero_group = pygame.sprite.Group()
self.hero = Hero()
self.hero_group.add(self.hero)
def start_game(self):
pygame.init()
print("開始游戲...")
# pygame.mixer.init()
# pygame.mixer.music.load("./Capo Proctions - Journey 00_00_00-00_00_59.ogg")
# pygame.mixer.music.play(-1)

# gameIcon = pygame.image.load("./name.png")

# pygame.display.set_icon(gameIcon)

while True:

self.count+=1
# 1. 設置刷新幀率
self.clock.tick(10)
# 2. 事件監聽
self.__event_handler()

# 3. 碰撞檢測
self.__check_collide()
# 4. 更新精靈組
self.__update_sprites()
# 5. 更新屏幕顯示

pygame.display.update()
def __event_handler(self):

"""事件監聽"""

for event in pygame.event.get():
if event.type == pygame.QUIT:
Plane_main.__game_over()
elif event.type == CREATE_ENEMY_EVENT:
self.enemy_group.add(Enemy())
elif event.type == HERO_FIRE_EVENT:
self.hero.fire()
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
print('向右移動')
self.hero.speed = 5
self.hero.speed1 = 0
elif keys[pygame.K_LEFT]:
print('向左移動')
self.hero.speed = -5
self.hero.speed1 = 0
elif keys[pygame.K_UP]:
print('向上移動')
self.hero.speed1 = -5
self.hero.speed = 0
elif keys[pygame.K_DOWN]:
print('向下移動')
self.hero.speed1= 5
self.hero.speed = 0
else:
self.hero.speed = 0
self.hero.speed1 = 0
if event.type == HERO_FIRE_EVENT:
self.hero.fire()
print('發射子彈!!!!!')

def __check_collide(self):

"""碰撞檢測"""
# 1. 子彈摧毀敵機
enemy_down = pygame.sprite.groupcollide(self.enemy_group, self.hero.bullets,True, True)
enemy1_down_group.add(enemy_down)
# 2. 敵機撞毀英雄
enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
# 判斷列表時候有內容`
if len(enemies) > 0:
sleep(3)
# # 讓英雄犧牲
self.hero.kill()
# 結束游戲
Plane_main.__game_over()
def __update_sprites(self):
self.back_group.update()
self.back_group.draw(self.screen)
self.enemy_group.update()
self.enemy_group.draw(self.screen)
self.hero_group.update()
self.hero_group.draw(self.screen)
self.hero.bullets.update()
self.hero.bullets.draw(self.screen)
self.drawText(str(self.score),SCREEN_RECT.width - 50,50)
for enemy1_down in enemy1_down_group:
self.screen.blit(enemy1_down_surface[enemy1_down.down_index],enemy1_down.rect)
if self.count % 5 ==0:
enemy1_down.down_index += 1
if enemy1_down.down_index ==3:
# pygame.mixer.music.load("./baozha.ogg")
# pygame.mixer.music.play()
self.score+=5
enemy1_down_group.remove(enemy1_down)
print(self.score)

#更新精靈組
# for group in [self.back_group, self.enemy_group, self.hero_group,self.hero.bullets]:
# group.update()
# group.draw(self.screen)
@staticmethod
def __game_over():
"""游戲結束"""
print("游戲結束")
pygame.quit()
exit()
def drawText(self,text,posx,posy,textHeight=48,fontColor=(0,0,0),backgroundColor=(111,225,1)):
fontObj = pygame.font.Font('freesansbold.ttf',textHeight)
textSurfaceObj = fontObj.render(text,13,fontColor,backgroundColor)
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (posx,posy)
self.screen.blit(textSurfaceObj,textRectObj)

#判斷當前執行的文件是否是主文件
#如果不是 則if下的代碼不會執行
if __name__ == '__main__':
game = Plane_main()
game.start_game()

Ⅱ 怎麼用python學飛機大戰

1、我方飛機模塊

import pygame

class MyPlane(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)

self.image1 = pygame.image.load(r'E:\learn pygame\image\me1.png').convert_alpha()
self.image2 = pygame.image.load(r'E:\learn pygame\image\me2.png').convert_alpha()
self.image3 = pygame.image.load(r'E:\learn pygame\image\me_protect.png').convert_alpha()
self.destroy_images = []
self.destroy_images.extend([\
pygame.image.load(r'E:\learn pygame\image\me_destroy_1.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\me_destroy_2.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\me_destroy_3.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\me_destroy_4.png').convert_alpha() \
])
self.rect = self.image1.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 60
self.speed = 10
self.active = True
self.invincible = False
self.mask = pygame.mask.from_surface(self.image1)

def moveUp(self):
if self.rect.top > 0:
self.rect.top -= self.speed
else:
self.rect.top = 0

def moveDown(self):
if self.rect.bottom < self.height - 30:
self.rect.top += self.speed
else:
self.rect.bottom = self.height - 30

def moveLeft(self):
if self.rect.left > 0:
self.rect.left -= self.speed
else:
self.rect.left = 0

def moveRight(self):
if self.rect.right < self.width:
self.rect.left += self.speed
else:
self.rect.right = self.width

def reset(self):
self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 30
self.active = True
self.invincible = True

2、敵機與背景動圖模塊
import pygame
from random import *

class SmallEnemy(pygame.sprite.Sprite):
energy = 1
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\enemy1.png').convert_alpha()
self.destroy_images = []
self.destroy_images.extend([\
pygame.image.load(r'E:\learn pygame\image\enemy1_down1.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy1_down2.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy1_down3.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy1_down4.png').convert_alpha() \
])
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.speed = 2
self.active = True
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-5 * self.height, 0)
self.mask = pygame.mask.from_surface(self.image)
self.energy = SmallEnemy.energy
self.hit = False

def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()

def reset(self):
self.active = True
self.energy = SmallEnemy.energy
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-5 * self.height, 0)

class MidEnemy(pygame.sprite.Sprite):
energy = 4

閱讀全文

與航班排序python相關的資料

熱點內容
安卓手機拍攝慢動作怎麼設置 瀏覽:480
中國程序員加油 瀏覽:172
python去哪個城市比較多 瀏覽:759
閃迪u盤加密初始密碼 瀏覽:773
房屋辦理解壓需要契稅和發票嗎 瀏覽:888
麗江易學java高級程序員 瀏覽:661
程序員木蘭教程 瀏覽:665
pythontkinter按鈕 瀏覽:439
如何快捷錄音安卓 瀏覽:7
sd播放音樂需要哪些文件夾 瀏覽:839
華為平板m3怎麼升級到安卓11 瀏覽:532
聯通app排隊號怎麼看 瀏覽:647
怎麼不越獄安裝app 瀏覽:183
python怎麼用鏈表 瀏覽:851
8k程序員面試題 瀏覽:541
貴州交警app怎麼下載 瀏覽:414
解壓縮安裝包怎麼安裝 瀏覽:44
壓縮機系統與裝置 瀏覽:677
上海大眾app怎麼查保養記錄 瀏覽:464
抖音網紅一手資源解壓密碼 瀏覽:543