import pygame
import sys,time,random
from pygame.locals import *
pygame.init()
# 设置按下鼠标的时候一直触发
pygame.key.set_repeat(10, 10)
# 加载背景图片
bg = pygame.image.load('./img/bg.png')
# 加载左方向行走和站立图片
heroLStand = pygame.image.load('img/heroLstand.png')
heroL1 = pygame.image.load('img/heroL1.png')
heroL2 = pygame.image.load('img/heroL2.png')
heroL3 = pygame.image.load('img/heroL3.png')
# 加载右方向行走和站立图片
heroRStand = pygame.image.load('img/heroRstand.png')
heroR1 = pygame.image.load('img/heroR1.png')
heroR2 = pygame.image.load('img/heroR2.png')
heroR3 = pygame.image.load('img/heroR3.png')
# 加载障碍物
zaw1 = pygame.image.load('img/zaw1.png')
zaw2 = pygame.image.load('img/zaw2.png')
zaw3 = pygame.image.load('img/zaw3.png')
# 加载武器图片
wq = pygame.image.load('img/b.png')
effer = pygame.image.load('img/effer.png')
clock = pygame.time.Clock()
# 创建一个画布
canvas = pygame.display.set_mode((1200,600))
# 初始数据
# 设置跳跃高度
jumpHeight = 40
# 设置移动速度
moveSpeed = 10
#------------------创建武器类----------------
class Wq():#武器的属性有:皮肤,坐标以及宽高,以及方向
def __init__(self,skin,x,y,width,height,direction):
self.skin = skin
self.x = x
self.y = y
self.width = width
self.height = height
self.direction = direction
# 武器的移动
def shoot(self):
#如果方向是往右则x值增加
if self.direction == "right":
for i in range(10):
self.x = self.x+2
else:# 否则就是往左移动
for i in range(10):
self.x = self.x-2
#------------------创建英雄类----------------
# 英雄类:属性, 皮肤skin,坐标,x,y 宽高width,height
n = 0# 实现动态切换不同的图片
class Hero():
def __init__(self,skin,x,y,width,height,direction="right"):
self.skin = skin
self.x = x
self.y = y
self.width = width
self.height = height
self.direction = direction
# 往左移动的方法
def goLeft(self):
self.x = self.x-moveSpeed
# 往右移动的方法
def goRight(self):
self.x = self.x+moveSpeed
# 往左移动的时候,图片的切换(英雄人物的切换)
def leftChange(self):
global n
if n%3 == 0:# 如果对3取余结果等于0,使用第一张图片
self.skin = heroL1
elif n%3 == 1:
self.skin = heroL2
else:
self.skin = heroL3
n = n+1
# 往右移动的时候,图片的切换(英雄人物的切换)
def rightChange(self):
global n
if n%3 == 0:# 如果对3取余结果等于0,使用第一张图片
self.skin = heroR1
elif n%3 == 1:
self.skin = heroR2
else:
self.skin = heroR3
n = n+1
#-----------------创建英雄对象-------------
# heroRStand,10,400,135,200
hero = Hero(heroRStand,10,400,135,200)
#----------------创建障碍物类---------------------
# 障碍物的属性:x,y,skin,width,height
# 障碍物的方法: 往左移动的方法
class Zaw():# 障碍物类
def __init__(self,x,y,skin,width,height):
self.x = x
self.y = y
self.skin = skin
self.width = width
self.height = height
def walk(self):# 设置往左移动的方法
self.x = self.x - 3
# 创建列表存储同时存储多个障碍物对象
zawList = []
createTime = 0# 用来存储创建障碍物对象的时间
# 创建一个方法:批量的生成障碍物对象
def createZaw():
# 因为每次创建障碍物对象的时候,都需要看间隔时间,时间符合要求则创建
# 更新时间
global createTime
# 检测时间间隔
if time.time()-createTime > 3:
# 更新一下创建障碍物的时间
createTime = time.time()
type = random.randint(1,3)
if type == 1:
# 如果type的值是1就要创建第一种障碍物
zawenemy = Zaw(1200,500,zaw1,46,46)
elif type == 2:
# 如果type的值是2就要创建第2种障碍物
zawenemy = Zaw(1200,500,zaw2,142,103)
else:# 创建第3种障碍物
zawenemy = Zaw(1200,450,zaw3,180,180)
# 将障碍物追加列表
zawList.append(zawenemy)
#--------------批量创建障碍物对象---------------------
#-----------------创建传送到画布方法-----------
# 让背景往右移动
bginit1 = -1200
bginit2 = 0
def draw():
global bginit1,bginit2
canvas.blit(bg,(bginit1,0))
canvas.blit(bg,(bginit2,0))
bginit1 = bginit1+1# 因为整体往右移动故2张背景图片x的值全部都在增加
bginit2 = bginit2+1
if bginit1>1200:
bginit1 = -1200
if bginit2>1200:
bginit2 = -1200
canvas.blit(hero.skin,(hero.x,hero.y))
# 将障碍物列表中的所有障碍物传送到画布上
for e in zawList:
if e.x < -200:# 说明往左移动的到最左边出画布
zawList.remove(e)# 从障碍物列表中移出该障碍物
else:
canvas.blit(e.skin,(e.x,e.y))
e.walk()
# 将武器绘制到画布上
for w in wqList:
if w.x<-200 or w.x>1200:
wqList.remove(w)
else:
canvas.blit(w.skin,(w.x,w.y))
w.shoot()
def hit():
# 武器和障碍物碰撞
for w in wqList:
for e in zawList:
if w.x+w.width >= e.x and w.x<=e.x+e.width\
and w.y+150>=e.y:
for i in range(4):
canvas.blit(effer,(e.x,e.y))
wqList.remove(w)
zawList.remove(e)
# 事件监听方法
isJump = False# 默认False没有跳跃
wqList = []# 武器库
# 设置发射时间间隔
shootTime = 0
def eventDeal():
global isJump
if hero.direction == "right":
hero.skin = heroRStand# 默认向右站立
elif hero.direction == "left":
hero.skin = heroLStand
# 遍历所有的事件
for e in pygame.event.get():
# 点击关闭按钮的时候,关闭窗口处理
if e.type == QUIT:
pygame.quit()# pygame退出相当于窗口关闭
sys.exit()# 系统退出
#---------------上下左右键监听处理-------------------
if e.type == KEYDOWN:# 当检测按下按键的时候
# 判断按的左键
if e.key == K_LEFT:
print('您刚才按下了左键')
hero.goLeft()# 往左移动
hero.leftChange()# 调用往左移动切换图片的效果
hero.direction = "left"
elif e.key == K_RIGHT:
print('您刚才按下了右键')
hero.direction = "right"
hero.goRight()
hero.rightChange()
elif e.key == K_SPACE:# 检测到空格
print('您刚才按下课空格键')
isJump = True# 当点击空格的时候跳跃状态切换成True
elif e.key == K_UP:
global shootTime
if time.time()-shootTime > 1:
shootTime = time.time()
# 通过武器类创建武器库
w = Wq(wq,hero.x,hero.y+hero.height/8,20,\
56,hero.direction)
# 将武器追加到武器库
wqList.append(w)
while True:
clock.tick(15)
if isJump:
hero.y = hero.y-jumpHei
没有合适的资源?快使用搜索试试~ 我知道了~
python实现跳跃发射子弹打怪基础功能
共16个文件
png:14个
py:2个
需积分: 1 0 下载量 160 浏览量
2023-09-29
20:10:24
上传
评论
收藏 1.05MB RAR 举报
温馨提示
图片和python代码
资源推荐
资源详情
资源评论
收起资源包目录
跳跃行走项目.rar (16个子文件)
跳跃行走项目
index.py 6KB
img
heroR3.png 23KB
heroLstand.png 29KB
zaw2.png 35KB
heroRstand.png 29KB
heroL1.png 27KB
zaw1.png 2KB
heroL2.png 23KB
zaw3.png 41KB
heroR2.png 27KB
bg.png 722KB
heroL3.png 25KB
effer.png 25KB
heroR1.png 26KB
b.png 72KB
index2.py 8KB
共 16 条
- 1
资源评论
Gjanuary
- 粉丝: 125
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功