python小项目分享
# python 小项目-飞机大战分享### 1.plane_main.py
```
importpygame
import ceshi
from plane_sprit import *
pygame.init()
pygame.mixer.music.load("背景 音乐2.mp3")
pygame.mixer.music.play()
class planegame(object):
def __init__(self):
self.sceen=pygame.display.set_mode(SCREEN_RECT.size)
self.clock=pygame.time.Clock()
self.__creatsprit()
pygame.time.set_timer(ENNY_PLANE,1000)
pygame.time.set_timer(HEARO_FIR, 500)
pygame.time.set_timer(ENNY_PLANE2,1500)
def __creatsprit(self):
bg1=background("./素材图片/背景图片4.jpg")
bg2=background("./素材图片/背景图片4.jpg")
bg2.rect.y=-bg2.rect.height
self.hearo=hearo()
self.back_group=pygame.sprite.Group(bg1,bg2)
self.enemy_group=pygame.sprite.Group()
self.hearo_group=pygame.sprite.Group(self.hearo)
self.enemy_group2=pygame.sprite.Group()
def startgame(self):
while True:
self.clock.tick(60)
self.__event_hear()
self.__check_hit()
self.__update_display()
pygame.display.update()
def __event_hear(self):
for event in pygame.event.get():
if event.type==pygame.QUIT:
self.__game_over()
elif event.type==ENNY_PLANE:
enemy=Enemy()
self.enemy_group.add(enemy)
elif event.type== ENNY_PLANE2:
enemy2=Enemy2()
self.enemy_group2.add(enemy2)
elif event.type == HEARO_FIR:
self.hearo.fire()
key_reponders=pygame.key.get_pressed()
if key_reponders:
self.hearo.speed=2
elif key_reponders:
self.hearo.speed=-2
else:
self.hearo.speed=0
def __check_hit(self):
pygame.sprite.groupcollide(self.hearo.bulluts,self.enemy_group,True,True)
pygame.sprite.groupcollide(self.hearo.bulluts,self.enemy_group2,True,True)
enems=pygame.sprite.spritecollide(self.hearo,self.enemy_group,True)
if len(enems)>0:
self.hearo.kill()
self.__game_over()
def __update_display(self):
self.back_group.update()
self.back_group.draw(self.sceen)
self.enemy_group.update()
self.enemy_group.draw(self.sceen)
self.enemy_group2.update()
self.enemy_group2.draw(self.sceen)
self.hearo_group.update()
self.hearo_group.draw(self.sceen)
self.hearo.bulluts.update()
self.hearo.bulluts.draw(self.sceen)
@staticmethod
def __game_over():
pygame.mixer.music.stop()
screen = pygame.display.set_mode((450, 700))
clock = pygame.time.Clock()
gameover = pygame.image.load("./素材图片/游戏结束1.jpg")
screen.blit(gameover, (0, 0))
f = 0
pygame.display.update()
while True:
f += 1
clock.tick(1)
if f == 3:
break
ceshi.shipin()
exit()
if __name__ == '__main__':
while True:
bg = pygame.image.load("./素材图片/界面.jpg")
screen_1 = pygame.display.set_mode((450, 700))
screen_1.blit(bg, (0, 0))
pygame.display.update()
for eventS in pygame.event.get():
if eventS.type==pygame.MOUSEBUTTONDOWN:
game = planegame()
game.startgame()
```
### 2.plane_sprit.py
```
import random
importpygame
SCREEN_RECT = pygame.Rect(0, 0, 450, 700)
ENNY_PLANE=pygame.USEREVENT
HEARO_FIR=pygame.USEREVENT+1
ENNY_PLANE2=pygame.USEREVENT+2
class sprit(pygame.sprite.Sprite):
def __init__(self,name,speed=1):
super().__init__()
self.image=pygame.image.load(name)
self.rect=self.image.get_rect()
self.speed=speed
def update(self):
self.rect.y+=self.speed
class background(sprit):
# def __init__(self, is_alt=False):
#
# image_name = "./素材图片/背景图片1.jpg"
# super().__init__(image_name)
#
#
# if is_alt:
# self.rect.y = -self.rect.height
def update(self):
super().update()
if self.rect.y>=SCREEN_RECT.height:
self.rect.y = -self.rect.height
class Enemy(sprit):
def __init__(self):
super().__init__("./素材图片/敌机.png")
self.speed=random.randint(1,3)
self.rect.bottom=0
max_X=SCREEN_RECT.width-self.rect.width
self.rect.x=random.randint(0,max_X)
def update(self):
super().update()
if self.rect.y>=SCREEN_RECT.height:
self.kill()
class Enemy2(sprit):
def __init__(self):
super().__init__("./素材图片/敌机2.png")
self.speed = random.randint(1, 3)
self.rect.bottom = 0
max_X = SCREEN_RECT.width - self.rect.width
self.rect.x = random.randint(0, max_X)
def update(self):
super().update()
if self.rect.y >= SCREEN_RECT.height:
self.kill()
class hearo(sprit):
def __init__(self):
super().__init__("./素材图片/主战机.png",0)
self.rect.centerx=SCREEN_RECT.centerx
self.rect.bottom=SCREEN_RECT.bottom-120
self.bulluts=pygame.sprite.Group()
def update(self):
self.rect.x+=self.speed
if self.rect.x<0:
self.rect.x=0
elif self.rect.right>SCREEN_RECT.right:
self.rect.right=SCREEN_RECT.right
def fire(self):
for i in (0,1):
bullet_s=bullet()
bullet_s.rect.bottom=self.rect.y-i*20
bullet_s.rect.centerx=self.rect.centerx
self.bulluts.add(bullet_s)
class bullet(sprit):
def __init__(self):
super().__init__("./素材图片/子弹.png",-2)
def update(self):
super().update()
if self.rect.bottom<0:
self.kill()
```
### 3.ceshi.py
```
import numpy as np
import cv2
import pygame
def shipin():
pygame.init()
pygame.mixer.music.load("sucai2.mp3")
cap = cv2.VideoCapture('./sucai.flv')
pygame.mixer.music.play()
while (cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(36) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
``` hello world小白一脸懵逼 这是飞机大战么。。。。 谢谢分享 感谢分享,楼主Python进阶怎么学啊 不会是 《Python 基础到入门》 的项目吧? 感谢分享 感谢大佬分享 正在学习Python中,向您学习了