马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我看完pygame的前几集后,尝试着写一个代码素材是《植物大战僵尸》里的小鬼僵尸,使其可以完成走和吃两个动作
但是在吃的时候并不会卡顿,在走路的时候如果把代码编为按一下按键一直走也不会卡顿,
但如果把代码编为只有按着按键才会走路就会卡顿
以下是代码以及注释import pygame#引用模块
import sys
from pygame import *
class imp(pygame.sprite.Sprite):#定义一个小鬼僵尸的类
def __init__ (self,imp_image,position,speed,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = imp_image
self.position = position
self.speed = speed
self.width,self.height = bg_size[0],bg_size[1]
self.eat = []
for each in range(33,60):
self.eat.append(self.image[each])
self.walk = []
for each in range(0,33):
self.walk.append(self.image[each])
def stand(self):
self.rect = self.image[33].get_rect()
self.rect.left = self.position[0]
self.rect.top = self.position[1]
def eat(self,i):
pass
def walkleft(self,i):#传入用于控制的参数i
self.speed = -4
self.position[0] = self.position[0]+self.speed
self.rect = self.walk[i].get_rect()
self.rect.left = self.position[0]
self.rect.top = self.position[1]
def walkright(self,i):
self.speed = 4
self.position[0] = self.position[0]+self.speed
self.rect = self.walk[i].get_rect()
self.rect.left = self.position[0]
self.rect.top = self.position[1]
def main():
pygame.init()#初始化
bg_size = width,height = 800,600#屏幕大小
bg_color = (255,255,255)#屏幕背景颜色
running = True#在初始化完毕后如果running为真则程序继续运行
screen = pygame.display.set_mode(bg_size)#设置窗口
pygame.display.set_caption('imp - Star_with_Dream Demo')#窗口标题
position = [width-100,height-100]#初始化速度位置
speed = -4#小鬼僵尸左右行走速度
imp_image = []#导入图片
for each in range(1,34):#依次导入小鬼僵尸的每帧图片总共有83张图
if each < 10:
imp_image.append(\
pygame.image.load('./image/Walk/imp000'+str(each)+'.png').convert_alpha())
else:
imp_image.append(\
pygame.image.load('./image/Walk/imp00'+str(each)+'.png').convert_alpha())
for each in range(34,61):
imp_image.append(\
pygame.image.load('./image/Eat/imp00'+str(each)+'.png').convert_alpha())
for each in range(61,83):
imp_image.append(\
pygame.image.load('./image/Die/imp00'+str(each)+'.png').convert_alpha())
clock = pygame.time.Clock()#帧率控制
imp1 = imp(imp_image,position,speed,bg_size)#僵尸一号
i=0#用于控制播放动画的帧的序号
action = 'stand'#用于控制小鬼僵尸的行为
Detect_key_release = False#用于判断按键的按下与松开也就是KEYDOWN和KEYUP,False为按键未按下
j = True#用于判断小鬼僵尸是否处于吃这个状态False为处于
while running:
for event in pygame.event.get():#当用户退出
if event.type == QUIT:
sys.exit()
if event.type == KEYDOWN:
k = event.key
if k == K_a:#当按下A键
action = 'walkleft'
Detect_key_release = True
elif k == K_d:#当按下D键
action = 'walkright'
Detect_key_release = True
if not Detect_key_release:
if k == K_e and j: #当按下E键且小鬼未处于吃时
j = not j
action = 'eat'
elif k == K_e and not j:#当按下E键且小鬼处于吃时则停止吃
action = 'stand'
j = not j
if event.type == KEYUP:#当松开A D键
k = event.key
if k == K_a:
action = 'stand'
Detect_key_release = False
elif k == K_d:
action = 'rstand'
Detect_key_release = False
screen.fill(bg_color)#用白色填满背景
if action == 'stand':#stand为默认状态,只放出一张图片
imp1.stand()
screen.blit(imp1.image[33],imp1.rect)
elif action == 'rstand':
imp1.stand()
screen.blit(pygame.transform.flip(imp1.image[33],True,False),imp1.rect)
elif action == 'eat':#eat状态是依次播放有关eat的图片序列
if i < 27:#防止报错超出列表范围
imp1.eat[i]
screen.blit(imp1.eat[i],imp1.rect)
i+=1
else:#让图片序列循环播放
i=3
imp1.eat[i]
screen.blit(imp1.eat[i],imp1.rect)
elif action == 'walkleft':#和eat同理
if i < 33:
imp1.walkleft(i)
imp1.walk[i]
screen.blit(imp1.walk[i],imp1.rect)
i+=1
else:
i=0
imp1.walk[i]
screen.blit(imp1.walk[i],imp1.rect)
elif action == 'walkright':
if i < 33:
imp1.walkright(i)
imp1.walk[i]
screen.blit(pygame.transform.flip(imp1.walk[i],True,False),imp1.rect)
i+=1
else:
i=0
imp1.walk[i]
screen.blit(pygame.transform.flip(imp1.walk[i],True,False),imp1.rect)
pygame.display.flip()
clock.tick(25)
if __name__ == '__main__':
main()
由于新手上路,所以有写的不好的地方轻喷,我想知道如何既让他按下按键并不松开才走路,又不会卡顿
但我的等级上传不了附件,我只能上传到百度网盘
链接是:链接:https://pan.baidu.com/s/1LgyoMLQe6SuIILIT2WaPYw?pwd=00FC ,
谢谢 |