Java教程

test

本文主要是介绍test,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import pygame,random,os,sys,time
from pygame.locals import *
pygame.init()
canvas=pygame.display.set_mode((1200,715))
canvas.fill((255,255,255))
pygame.display.set_caption("飞机大战")
e1=[]
e2=[]
e3=[]
b=[]
h=[]
e1.append(pygame.image.load("images/ll/enemy1.png"))
e1.append(pygame.image.load("images/enemy1_down1.png"))
e1.append(pygame.image.load("images/enemy1_down2.png"))
e1.append(pygame.image.load("images/enemy1_down3.png"))
e1.append(pygame.image.load("images/enemy1_down4.png"))
e1.append(pygame.image.load("images/enemy1_down5.png"))
e2.append(pygame.image.load("images/enemy2.png"))
e2.append(pygame.image.load("images/enemy2_down1.png"))
e2.append(pygame.image.load("images/enemy2_down2.png"))
e2.append(pygame.image.load("images/enemy2_down3.png"))
e2.append(pygame.image.load("images/enemy2_down4.png"))
e2.append(pygame.image.load("images/enemy2_down5.png"))
e3.append(pygame.image.load("images/enemy3_1.png"))
e3.append(pygame.image.load("images/enemy3_2.png"))
e3.append(pygame.image.load("images/enemy3_down1.png"))
e3.append(pygame.image.load("images/enemy3_down2.png"))
e3.append(pygame.image.load("images/enemy3_down3.png"))
e3.append(pygame.image.load("images/enemy3_down4.png"))
e3.append(pygame.image.load("images/enemy3_down5.png"))
e3.append(pygame.image.load("images/enemy3_down6.png"))
e3.append(pygame.image.load("images/enemy3_down7.png"))
h.append(pygame.image.load("images/hero.png"))
h.append(pygame.image.load("images/hero_down1.png"))
h.append(pygame.image.load("images/hero_down2.png"))
h.append(pygame.image.load("images/hero_down3.png"))
h.append(pygame.image.load("images/hero_down4.png"))
b.append(pygame.image.load("images/bullet1.png"))
bg = pygame.image.load("images/bg4.png")
startgame = pygame.image.load("images/startGame.png")
logo = pygame.image.load("images/LOGO.png")
pause = pygame.image.load("images/game_pause_nor.png")
score = pygame.image.load("images/score.png")
over = pygame.image.load("images/over.png")
bgg = pygame.image.load("images/bg235.jpg")
def myevent():
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
        if event.type==pygame.MOUSEMOTION:
            x,y=event.pos[0],event.pos[1]
            if Game.state=="runing":
                Game.hero.x=x-Game.hero.w
                Game.hero.y=y-Game.hero.h/2
            if ismouseout(x,y):
                if Game.state=="runing":
                    Game.state="pause"
            if not ismouseout(x,y):
                if Game.state=="pause":
                    Game.state="runing"
        if event.type==pygame.MOUSEBUTTONDOWN and event.button==1:
                if Game.state=="start":
                    Game.state="runing"
        if event.type==KEYDOWN and event.key==k_r:
            if Game.state=="gameover":
                Game.score=0
                Game.heros=6
                Game.enemies=[]
                Game.state="start"
def ismouseout(x,y):
    return x==0 or x>=1190 or y==0 or y>=700
def draw(img,pos):
    canvas.blit(img,pos);
def isactiontime(last,interval):
    return time.time()-last>=interval
def filltext(text,pos):
    draw(pygame.font.Font("my_font/font1.ttf",30).\
         render(text,True,(255,255,255)),pos)
def componententer():
    if not isactiontime(Game.elasttime,Game.einterval):
        return 
    Game.elasttime=time.time()
    x=random.randint(1,10)
    if x<5:
        Game.enemies.append(Enemy(0,0,57,45,1,1,1,e1))
    if x>=6 and x<=8:
        Game.enemies.append(Enemy(0,0,50,68,2,5,3,e2))
    if x==9:
        Game.enemies.append(Enemy(0,0,196,258,3,10,10,e3))
def paintcomponent():
    if not isactiontime(Game.painttime,Game.paintinterval):
        return
    Game.painttime=time.time()
    Game.sky.paint()
    Game.hero.paint()
    for enemy in Game.enemies:
        enemy.paint()
    for bullet in Game.bullets:
        bullet.paint()
    draw(score, (910, 10))
    filltext(str(Game.score), (780 + 305, 25))
    filltext(str(Game.heroes), (780 + 305, 58))
def stepcomponent():
    Game.sky.step()
    for enemy in Game.enemies:
        enemy.step()
    for bullet in Game.bullets:
        bullet.step()
def checkhit():
    for enemy in Game.enemies:
        if enemy.down == True:
            continue
        if Game.hero.hit(enemy):
            enemy.bang()
            Game.hero.bang()
        for bullet in Game.bullets:
            if bullet.down == True:
                continue
            if enemy.hit(bullet):
                enemy.bang()
                bullet.bang()
def deletecomponent():
    for i in range(0,len(Game.enemies)-1):
        x = Game.enemies[i]
        if x.delete or x.outofbounds():
            Game.enemies.remove(x)
    print(len(Game.bullets))
    for i in range(len(Game.bullets)-1,-1,-1):
        x = Game.bullets[i]
        if x.delete or x.outofbounds():
            Game.bullets.remove(x)
    if Game.hero.delete == True:
        Game.heroes -= 1
        if Game.heroes == 0:
            Game.state = "gameover"
        else:
            Game.hero = Hero(0, 0, 60, 75, 1, h)
def componentanimation():
    for enemy in Game.enemies:
        enemy.animation()
    for bullet in Game.bullets:
        bullet.animation()
    Game.hero.animation()
class Sky():
    def __init__(self):
        self.w=480
        self.h=650
        self.img=bgg
        self.x=0
        self.y1=0
        self.y2=-self.h
    def paint(self):
        draw(self.img,(self.x,self.y1))
        draw(self.img,(self.x,self.y2))
    def step(self):
        self.y1+=1
        self.y2+=1
        if self.y1>self.h:
            self.y1=-self.h
        if self.y2>self.h:
            self.y2=-self.h
class Flying(object):
    def __init__(self,x,y,h,w,life,frame):
        self.x=x
        self.y=y
        self.h=h
        self.w=w
        self.life=life
        self.down=False
        self.delete=False
        self.last=0
        self.interval=0
        self.frameindex=0
        self.frame=frame
        self.img=frame[0]
    def paint(self):
        draw(self.img, (self.x,self.y))
    def step(self):
        if not isactiontime(self.last,self.interval):
            return
        self.last=time.time()
        self.y+=2
    def hit(self,c):
        return c.x>self.x-c.w and c.x<self.x+self.w \
           and c.y>self.y-c.h  and c.y<self.y+self.h
    def bang(self):
        self.life-=1
        if self.life==0:
            self.down=True
            if hasattr(self,"score"):
                Game.score+=self.score
    def outofbounds(self):
        return self.y>650 or self.y<0
    def animation(self):
        if self.down:
            if len(self.frame)==self.frameindex:
                self.delete=True
            else:
                self.img=self.frame[self.frameindex]
                self.frameindex+=1
class Enemy(Flying):
    def __init__(self,x,y,w,h,type,score,life,frame):
        Flying.__init__(self,x,y,h,w,life,frame)
        self.x=random.randint(0,1200-self.w)
        self.type=type
        self.score=score
class Hero(Flying):
    def __init__(self,x,y,w,h,life,frame):
        Flying.__init__(self,x,y,h,w,life,frame)
        self.w=w
        self.h=h
        self.shoottime=0
        self.shootinterval=0
    def shoot(self):
        if not isactiontime(self.shoottime,self.shootinterval):
            return 
        self.shoottime=time.time()
        Game.bullets.append(Bullet(self.x+self.w/2,self.y))
class Bullet(Flying):
    def __init__(self,x,y):
        Flying.__init__(self, x, y, 21, 9, 1, b)
    def step(self):
        self.y-=5    
    def outofbounds(self):
        return self.y<0
class Game:
    enemies=[]
    bullets=[]
    score=0;
    heroes=20;
    sky=None
    hero=None
    elasttime=0
    einterval=0.5
    painttime=0
    paintinterval=0.01
    state="start"
Game.sky=Sky()
Game.hero=Hero(0,0,60,75,1,h)
def start():
    if Game.state=="start":
        Game.sky.paint()
        Game.sky.step()
        draw(logo,(200,200))
        draw(startgame,(460,450))
    if Game.state=="runing":
        componententer()
        paintcomponent()
        stepcomponent()
        componentanimation()
        Game.hero.shoot()
        checkhit()
        deletecomponent()
    if Game.state=="pause":
        paintcomponent()
        Game.sky.step()
        draw(pause,(400,500))
    if Game.state=="gameover":
        paintcomponent()
        Game.sky.step()
        Game.bullets=[]
        Game.enemies=[]
        draw(over,(400,500))   
while 1:
    start()
    pygame.display.update()
    myevent()
    pygame.time.delay(15)
    

  

这篇关于test的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!