马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 小菜鸟FLY 于 2021-8-17 18:32 编辑
按住键盘上的A/D是左右移动,持续按住时会切换图片,以此来达到“走路”的动作,但是不管我怎么调帧数,还是会有闪烁,各位大神给支个招?import pygame
from pygame.locals import *
import sys
import traceback
pygame.init()
bg_size = width,height = 1000,400
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption('2021.8.17')
background_photo = pygame.image.load('bg_1000×400.png').convert()
class Man(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image1 = pygame.image.load('stand_100×200 .png').convert_alpha()
self.image2 = pygame.image.load('walk_1_100×200.png').convert_alpha()
self.image3 = pygame.image.load('walk_2_100×200.png').convert_alpha()
#获取插入图的限定矩形
self.rect = self.image1.get_rect()
#背景图的长和高
self.width,self.height = bg_size[0],bg_size[1]
#设置初始位置在左下角
self.rect.left,self.rect.top = (0,self.height - self.rect.height)
self.speed = 2
def walk_left(self):
if self.rect.left > 0:
self.rect.left -= self.speed
else:
self.rect.left = 0
def walk_right(self):
if self.rect.right < 1000:
self.rect.right += self.speed
def main():
#设置帧数
clock = pygame.time.Clock()
man = Man(bg_size)
delay = 100
switch_image = 2
r_head1 = man.image1
r_head2 = man.image2
r_head3 = man.image3
l_head1 = pygame.transform.flip(man.image1,True,False)
l_head2 = pygame.transform.flip(man.image2,True,False)
l_head3 = pygame.transform.flip(man.image3,True,False)
head1 = r_head1
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#绘制图片
screen.blit(background_photo,(0,0))
key_pressed = pygame.key.get_pressed()
if not (key_pressed[K_a] or key_pressed[K_d]):
screen.blit(head1,man.rect)
if key_pressed[K_a]:
man.walk_left()
head1 = l_head1
if switch_image == 2:
screen.blit(l_head2,man.rect)
elif switch_image == 1:
screen.blit(l_head3,man.rect)
else:
switch_image = 2
if key_pressed[K_d]:
man.walk_right()
head1 = r_head1
if switch_image == 2:
screen.blit(r_head2,man.rect)
elif switch_image == 1:
screen.blit(r_head3,man.rect)
else:
switch_image = 2
#切换图片
if not(delay % 5):
switch_image -= 1
delay -=1
if not delay:
delay = 20
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
try:
main()
except SystemExit: #正常退出,抛出异常,然后跳过
pass
except:
#在双击打开程序时,若有异常,打印异常,退出
traceback.print_exc()
pygame.quit()
input()
哎呀,还没权限,怎么上传图片文件呀?好烦那
|