下面这个是我要做的内容,我刚开始学,不是很懂,我把上个问题回答给的代码放进来,但是第二个画面是绿色的,是什么原因
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((0,255,0))
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))
pygame.display.update()
deviation = 0
pos = 100
while True:
for i in Morse:
if (i == '.'):
pygame.draw.line(start_ck2, (225, 0, 0), (pos - deviation, 30), (pos + 5 - deviation, 30), 5)
pos += 10
if (i == '-'):
pygame.draw.line(start_ck2, (225, 0, 0), (pos - deviation, 30), (pos + 20 - deviation, 30), 5)
pos += 25
pos = 100
deviation += 0.05
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.flip()