ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

test

2022-01-10 23:04:28  阅读:135  来源: 互联网

标签:load image images pygame test self png


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)
    

  

标签:load,image,images,pygame,test,self,png
来源: https://www.cnblogs.com/limoyun/p/15786372.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有