import pygame
import sys
import random
import feiji
import diji
def key_kong ( hero) :
for shi_jian in pygame. event. get( ) :
if shi_jian. type == pygame. QUIT:
pygame. quit( )
sys. exit( )
elif shi_jian. type == pygame. KEYDOWN:
if shi_jian. key == pygame. K_a:
hero. move_left( )
elif shi_jian. key == pygame. K_d:
hero. move_right( )
elif shi_jian. key == pygame. K_w:
hero. move_up( )
elif shi_jian. key == pygame. K_s:
hero. move_down( )
elif shi_jian. key == pygame. K_j:
hero. fashe( )
elif shi_jian. key == pygame. K_b:
hero. zhanji_boom( )
def zhu ( ) :
pygame. init( )
chuangkou = pygame. display. set_mode( ( 400 , 700 ) )
pygame. display. set_caption( "飞机大战" )
bei_jing = pygame. image. load( "图片/background.png" )
feiji1 = feiji. feiji( chuangkou)
diji_ku = [ ]
pygame. key. set_repeat( 1 , 1 )
while True :
chuangkou. blit( bei_jing, ( 0 , 0 ) )
zidan_x, zidan_y = feiji1. show( )
b = random. randint( 1 , 300 )
if b == 5 :
diji_ku. append( diji. diji( chuangkou) )
for diji1 in diji_ku:
diji1. show( )
diji1. move( )
diji1. fashe( )
if diji1. yuejie( ) :
diji_ku. remove( diji1)
for diji2 in diji_ku:
if feiji1. x <= diji2. x <= feiji1. x + 100 and feiji1. y <= diji2. y <= feiji1. y + 124 :
feiji1. zhanji_boom( )
key_kong( feiji1)
print ( "---" )
pygame. display. update( )
if __name__ == '__main__' :
zhu( )
import pygame
import zidan
import time
import sys
class feiji ( ) :
def __init__ ( self, ck) :
self. name = "hero"
self. x = 200
self. y = 400
self. chuangkou = ck
self. picture = pygame. image. load( "图片/hero1.png" )
self. zidan_ku = [ ]
self. boom = False
self. boom_picture = [ ]
self. jiaru_boom_picture( )
self. boom_picture_num = 0
self. image_index = 0
def jiaru_boom_picture ( self) :
self. boom_picture. append( pygame. image. load( "图片/hero_blowup_n1.png" ) )
self. boom_picture. append( pygame. image. load( "图片/hero_blowup_n2.png" ) )
self. boom_picture. append( pygame. image. load( "图片/hero_blowup_n3.png" ) )
self. boom_picture. append( pygame. image. load( "图片/hero_blowup_n4.png" ) )
def show ( self) :
if self. boom == True :
for i in range ( 4 ) :
self. chuangkou. blit( pygame. image. load( "图片/background.png" ) , ( 0 , 0 ) )
self. chuangkou. blit( self. boom_picture[ i] , ( self. x, self. y) )
time. sleep( 0.1 )
pygame. display. update( )
pygame. quit( )
sys. exit( )
else :
self. chuangkou. blit( self. picture, ( self. x, self. y) )
zidan_x = [ ]
zidan_y = [ ]
for dan_ge_zidan in self. zidan_ku:
zidan_x. append( dan_ge_zidan. x)
zidan_y. append( dan_ge_zidan. y)
dan_ge_zidan. show( )
dan_ge_zidan. move__fd( )
if dan_ge_zidan. yuejie( ) :
self. zidan_ku. remove( dan_ge_zidan)
return zidan_x, zidan_y
def move_left ( self) :
self. x -= 1
if self. x < - 25 :
self. x = 5
def move_right ( self) :
self. x += 1
if self. x >= 330 :
self. x = 330
def move_up ( self) :
self. y -= 1
if self. y < - 120 :
self. y = 700
def move_down ( self) :
self. y += 1
if self. y >= 700 :
self. y = - 120
def fashe ( self) :
self. zidan_ku. append( zidan. zidan( self. chuangkou, self. x + 48 , self. y - 10 , "图片/bullet.png" ) )
self. zidan_ku. append( zidan. zidan( self. chuangkou, self. x + 14 , self. y + 25 , "图片/bullet1.png" ) )
self. zidan_ku. append( zidan. zidan( self. chuangkou, self. x + 80 , self. y + 25 , "图片/bullet2.png" ) )
def zhanji_boom ( self) :
self. boom = True