用户:sevenyXwC查看:0 回复:1 评论:0 创建时间:2022-03-09T18:18:56
【作品展示】

【作品介绍】
A键左转D键右转
空格发射
【作品源代码】
#coding:utf-8
import pygame,sys,random,time,turtle
from pygame.locals import*
#初始化pygame环境
pygame.init()
x = 80
y = 27
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x, y)
#创建一个长宽分别为480/650窗口
canvas = pygame.display.set_mode((1500, 600))
canvas.fill((255,255,255))
#设置窗口标题
pygame.display.set_caption("抗击疫情")
bg=pygame.image.load("img/bg.png")
h = pygame.image.load("img/hero.png")
b = pygame.image.load("img/b.png")
h1 = pygame.image.load("img/hero1.png")
b1 = pygame.image.load("img/b1.png")
v = pygame.image.load("img/v.png")
vb = pygame.image.load("img/vb.png")
d = pygame.image.load("img/d.png")
def handleEvent():
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == KEYDOWN and event.key == K_SPACE:
pygame.key.set_repeat(10, 10)
GameVar.h.shoot()
if event.type == KEYDOWN and event.key == K_a:
GameVar.h.x -= 10
GameVar.h.img= h1
if event.type == KEYDOWN and event.key == K_d:
GameVar.h.x += 10
GameVar.h.img= h
def renderText(text, position, view=canvas):
# 设置字体样式和大小
my_font = pygame.font.Font("font3.ttf", 100)
# 渲染文字
text = my_font.render(text, True, (0, 0, 0))
view.blit(text, position)
def isActionTime(lastTime, interval):
if lastTime == 0:
return True
currentTime = time.time()
return currentTime - lastTime >= interval
class Enemy():
def __init__(self,x):
self.x = x
self.y = 500
self.width = 100
self.height = 100
self.life = 200
self.atracct = 10
self.img = v
self.canDelete = False
def paint(self):
canvas.blit(self.img,(self.x, self.y))
def move(self):
if self.x > GameVar.h.x:
self.x -= 3
else:
self.x += 3
def hit(self, c):
return c.x > self.x - c.width and c.x < self.x + self.width and c.y > self.y - c.height and c.y < self.y + self.height
# 处理碰撞发生后要做的事
def 喵(self):
for i in GameVar.bullets:
self.life -= i.atracct
if self.life <= 0:
self.canDelete = True
class Hero():
def __init__(self):
self.x = 750
self.y = 420
self.width = 156
self.height = 180
self.life = 800
self.img = h
self.canDelete = False
def paint(self):
canvas.blit(self.img, (self.x, self.y))
def shoot(self):
if self.img == h1 :
GameVar.bullets.append(Bullet(self.x + self.width / 2, self.y+100,b1,-15))
else:
GameVar.bullets.append(Bullet(self.x + self.width / 2, self.y+100,b,15))
def hit(self, c):
return c.x > self.x - c.width and c.x < self.x + self.width and c.y > self.y - c.height and c.y < self.y + self.height
# 处理碰撞发生后要做的事
def 喵(self):
for i in GameVar.enemies:
self.life -= 20
if self.life == 0:
# 生命值为0时将down置为True
self.canDelete = True
class Bullet():
def __init__(self,x,y,img,speed):
self.x = x
self.y = y
self.width = 41
self.height = 30
self.img = img
self.speed = speed
self.canDelete = False
self.life = 1
self.atracct = 20
def paint(self):
canvas.blit(self.img, (self.x, self.y))
def move(self):
self.x+=self.speed
def hit(self, c):
return c.x >= self.x - c.width and c.x < self.x + self.width and c.y > self.y - c.height and c.y < self.y + self.height
# 处理碰撞发生后要做的事
def 喵(self):
self.life -= 1
if self.life == 0:
# 生命值为0时将down置为True
self.canDelete = True
def componentEnter():
if not isActionTime(GameVar.lastTime, GameVar.interval):
return
GameVar.lastTime = time.time()
x = random.randint(1,2)
if x==1:
x=-200
else:
x = 1700
GameVar.enemies.append(Enemy(x))
def compaint():
canvas.blit(bg,(0,0))
for enemy in GameVar.enemies:
enemy.paint()
GameVar.h.paint()
for bullet in GameVar.bullets:
bullet.paint()
# renderText('score'+str(GameVar.score), (780 + 305, 25))
# renderText('life'+str(GameVar.h.life), (780 + 305, 58))
def commove():
for enemy in GameVar.enemies:
enemy.move()
for b in GameVar.bullets:
b.move()
def componentStep():
# 调用sky对象的step方法
GameVar.sky.step()
for enemy in GameVar.enemies:
enemy.step()
# 子弹移动
for bullet in GameVar.bullets:
bullet.step()
# 检测组件碰撞
def checkHit():
# 判断敌飞机是否和英雄机相撞
for enemy in GameVar.enemies:
# 如果当前飞机已经喵亡则换下一架飞机
if enemy.canDelete == True:
GameVar.enemies.remove(enemy)
GameVar.score +=5
if GameVar.h.hit(enemy):
enemy.life = 0
enemy.喵()
GameVar.h.喵()
for bullet in GameVar.bullets:
# 如果当前子弹是无效的子弹则换下一颗子弹
if bullet.canDelete == True:
GameVar.bullets.remove(bullet)
if enemy.hit(bullet):
enemy.喵()
bullet.喵()
class GameVar(object):
enemies = []
bullets = []
lastTime = 0
interval = 1
h = Hero()
score = 0
while True:
if GameVar.h.life>0:
componentEnter()
compaint()
commove()
checkHit()
else:
canvas.fill((255,255,255))
renderText('游戏结束,你最终的得分为'+str(GameVar.score), (100, 250))
#处理关闭游戏
handleEvent()
#刷新屏幕
pygame.display.update()
#延迟处理
pygame.time.delay(15)
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn