猫史档案馆


【Python作品分享】index【作业帖】

用户:少年编程狂少年编程狂查看:0 回复:0 评论:0 创建时间:2019-09-17T21:01:36


【作品展示】

center_image

 

【作品介绍】

1

 

【作品源代码】

#coding:utf-8
import pygame,sys,random,time
from pygame.locals import *
#初始化pygame环境
pygame.init()

#创建一个长宽分别为480/650窗口
canvas = pygame.display.set_mode((480, 650))
canvas.fi喵))

#设置窗口标题
pygame.display.set_caption("飞机大战")
bg=pygame.image.load("images/bg1.png")
enemy1 = pygame.image.load("images/enemy1.png")
enemy2 = pygame.image.load("images/enemy2.png")
enemy3 = pygame.image.load("images/enemy3.png")
b=pygame.image.load("images/bullet1.png")
h=pygame.image.load("images/hero.png")

#添加时间间隔的方法
def isActionTime(lastTime, interval):
    if lastTime == 0:
        return True
    currentTime = time.time()
    return currentTime - lastTime >= interval
              
#定义Sky类
class Sky():
    def __init__(self):
        self.width = 480
        self.height = 852
        self.img = bg
        self.x1 = 0
        self.y1 = 0
        self.x2 = 0
        self.y2 = -self.height
    #创建paint方法
    def paint(self):
        canvas.blit(self.img,(self.x1,self.y1))
        canvas.blit(self.img,(self.x2,self.y2))
    #创建step方法
    def step(self):
        self.y1 = self.y1 + 1
        self.y2 = self.y2 + 1
        if self.y1 > self.height:
            self.y1 = -self.height
        if self.y2 > self.height:
            self.y2 = -self.height
            
#定义父类FlyingObject
class FlyingObject(object):
    def __init__(self,x,y,width,height,life,img):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.life = life
        self.img = img
        #敌飞机移动的时间间隔
        self.lastTime = 0
        self.interval = 0.01
    #定义paint方法
    def paint(self):
        canvas.blit(self.img,(self.x,self.y))
    #定义step方法
    def step(self):
        #判断是否到了移动的时间间隔
        if not isActionTime(self.lastTime,self.interval):
            return
        self.lastTime = time.time()
        #控制移动速度
        self.y = self.y + 2
    #定义hit方法判断两个对象之间是否发生碰撞
    def hit(self,component):
        c = component
        return c.x>self.x-c.width and c.xself.y-c.height and c.y

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页