我使用了键盘事件,如何当我按向上键的时候,产生的线条能长时间停留在画面上
现在他只是闪一下线条,我想让他能一直出现
import pygame
import sys
def Morse_code(x):
'''
function:返回对应的摩斯码
'''
dic = {'A': '._', 'B': '_...', 'C': '_._.', 'D': '_..', 'E': '.', 'F': '.._.',
'G': '__.', 'H': '....', 'I': '..', 'J': '.___', 'K': '_._', 'L': '._..',
'M': '__', 'N': '_.', 'O': '___', 'P': '.__.', 'Q': '__._', 'R': '._.',
'S': '...', 'T': '_', 'U': '.._', 'V': '..._', 'W': '.__', 'X': '_.._',
'Y': '_.__', 'Z': '__..',
'1': '.____', '2': '..___', '3': '...__', '4': '...._', '5': '.....',
'6': '_....', '7': '__...', '8': '___..', '9': '____.', '0': '_____',
' ': ' '}
if x in dic:
return dic[x]
def Turn_Morse_code(s):
'''
function:将字符串转为摩斯密码串
'''
s = s.upper() # 将小写字母转换为大写字母
num = len(s)
Morsecode = ''
i = 0
for i in range(num):
Morsecode += str(Morse_code(s[i]))
return Morsecode
s:str = "ILOVEYOU"
Morse = Turn_Morse_code(s)
pygame.init()
pygame.mixer.init()
ck = pygame.display.set_mode((800,500)) # 游戏窗口
pygame.display.set_caption("发报机") # 给窗口取个名
clock = pygame.time.Clock() # 游戏刷新速度
start_ck = pygame.Surface(ck.get_size()) # 充当开始界面的画布
start_ck2 = pygame.Surface(ck.get_size()) # 充当第一关的画布界面暂时占位
start_ck3 = pygame.Surface(ck.get_size())
start_ck = start_ck.convert()
start_ck2 = start_ck2.convert()
start_ck.fill((255,255,255)) # 白色画布1(开始界面用的)
start_ck2.fill((255,255,255))
start_ck3.fill((255,255,255))
# 加载各个素材 并且赋予变量名
track = pygame.mixer.music.load("./sound/sound.mp3")
i1 = pygame.image.load("./s1.png")
i1.convert()
i11 = pygame.image.load("./s2.png")
i11.convert()
i2 = pygame.image.load("./n2.png")
i2.convert()
i21 = pygame.image.load("./n1.png")
i21.convert()
bg = pygame.image.load('./bi.jpg')
bg.convert()
f = pygame.font.Font('./WETPET.TTF',50)
pygame.mixer.music.play()
# 以下为选择开始界面鼠标检测结构。
n1 = True
while n1:
clock.tick(30)
buttons = pygame.mouse.get_pressed()
x1, y1 = pygame.mouse.get_pos()
if x1 >= 227 and x1 <= 555 and y1 >= 141 and y1 <=207:
start_ck.blit(i11, (200, 120))
if buttons[0]:
n1 = False
elif x1 >= 227 and x1 <= 555 and y1 >= 271 and y1 <=337:
start_ck.blit(i21, (200, 230))
if buttons[0]:
pygame.quit()
exit()
else:
start_ck.blit(i1, (200, 120))
start_ck.blit(i2, (200, 230))
ck.blit(start_ck,(0,0))
pygame.display.update()
# 下面是监听退出动作
# 监听事件
for event in pygame.event.get():
# 判断事件类型是否是退出事件
if event.type == pygame.QUIT:
print("游戏退出...")
# quit 卸载所有的模块
pygame.quit()
# exit() 直接终止当前正在执行的程序
sys.exit()
ck.blit(start_ck2,(0,0))
deviation = 0
pos = 800
deviation1 = 0
pos1 = 0
n2 = True
while n2:
clock.tick(30)
ck.blit(start_ck2, (0, 0))
start_ck2.blit(bg, (0, 0))
# render(text, antialias, color, background=None) -> Surface
text = f.render("I LOVE YOU", True, (255, 50, 50), (255, 255, 255))
# 获得显示对象的 rect区域大小
textRect = text.get_rect()
# 设置显示对象居中
textRect.center = (400, 100)
start_ck2.blit(text, textRect)
pygame.display.update()
for i in Morse:
if i == '.':
pygame.draw.line(start_ck2, (125, 20, 220), (pos - deviation, 200), (pos + 10 - deviation, 200), 70)
pos += 20
if i == '_':
pygame.draw.line(start_ck2, (220, 150, 20), (pos - deviation, 200), (pos + 30 - deviation, 200), 70)
pos += 40
pos = 800
deviation += 1
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
pygame.draw.line(start_ck2, (125, 20, 220), (pos1 + deviation1, 400), (pos1 + 10 + deviation1, 400), 70)
pos1 += 20
if event.key == pygame.K_DOWN:
pygame.draw.line(start_ck2, (220, 150, 20), (pos1 + deviation1, 400), (pos1 + 30 + deviation1, 400), 70)
pos1 += 40
pos = 800
deviation += 1
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
如果要移动就deviation+=1,不然就deviation+=0