import pygame,sys
pygame.init()
desk_width = pygame.display.get_desktop_sizes()[0][0]
desk_height = pygame.display.get_desktop_sizes()[0][1]
WIN_WIDTH=1440
WIN_HEIGHT=800
window=pygame.display.set_mode((desk_width,desk_height))
pygame.display.set_caption('The Cemetroy')
window.fill((0,0,0))
FPS=60
image_start=r'C:/Users/26896/Desktop/THE CEMETERY/start.jpg'
image_game0=r"C:\Users\26896\Desktop\THE CEMETERY\Cemetery-dusk.png"
image_game1=r"C:\Users\26896\Desktop\THE CEMETERY\Cemetery-dusk(2).png"
textbottom=r"C:/Users/26896/Desktop/THE CEMETERY/HYShangWeiHuangYeW.ttf"
elder_left = pygame.image.load(r"C:\Users\26896\Desktop\THE CEMETERY\elder_left.png", )
elder_right = pygame.image.load(r"C:\Users\26896\Desktop\THE CEMETERY\elder_right.png")
def textbox( content,rfiles, color, size, x,y):
font=pygame.font.Font(rfiles,size)
text=font.render('content',True,color)
window.blit(text,(x,y))
pygame.display.update()
def imagebox(rfiles,angle=0,scale=1):
image=pygame.image.load(rfiles)
w,h=image.get_size()
window.blit(image,(desk_width/2-w/2,desk_height/2-h/2))
pygame.display.flip()
image=pygame.transform.rotozoom(image,angle,scale)
window.blit(image,(desk_width/2-w/2,desk_height/2-h/2))
pygame.display.update()
#按钮
def bottombox(text_rfiles,text_size,text_color,text_content,rectangle_color,rx,ry,rw,rh,ractangles_width=0,border_radius=20,border_top_left_radius=-1,border_top_right_radius=-1,border_bottom_left_radius=-1,border_bottom_right_radius=-1,rectangle_color_beneath=(50,35,35)):
#中心坐标
font=pygame.font.Font(text_rfiles,text_size)
pygame.draw.rect(window, rectangle_color_beneath, (rx-rw*3/5,ry-rh*5/8,rw*6/5,rh*5/4),ractangles_width,border_radius,border_top_left_radius,border_top_right_radius,border_bottom_left_radius,border_bottom_right_radius)
pygame.draw.rect(window, rectangle_color, (rx-rw/2,ry-rh/2,rw,rh),ractangles_width,border_radius,border_top_left_radius,border_top_right_radius,border_bottom_left_radius,border_bottom_right_radius)
text=font.render(text_content,True,text_color)
tw,th = text.get_size()
window.blit(text,(rx-tw/2, ry-th/2))
pygame.display.update()
for event in pygame.event.get():
#按下按钮动画
def mousebottomdown(rx,ry,rw,rh,text_rfiles,text_size,text_color,text_content,rectangle_color,rectangle_width=0,border_radius=20,border_top_left_radius=-1,border_top_right_radius=-1,border_bottom_left_radius=-1,border_bottom_right_radius=-1):
pygame.draw.rect(window,(100,100,100),(rx-rw/2,ry-rh/2,rw,rh),0,border_radius,border_top_left_radius,border_top_right_radius,border_bottom_left_radius,border_bottom_right_radius)
font=pygame.font.Font(text_rfiles,text_size)
text=font.render(text_content,True,(255,0,0))
tw,th = text.get_size()
window.blit(text,(rx-tw/2, ry-th/2))
pygame.display.update()
pygame.time.wait(300)
def mousebottomup(rx,ry,rw,rh,text_rfiles,text_size,text_color,text_content,rectangle_color,rectangle_width=0,border_radius=20,border_top_left_radius=-1,border_top_right_radius=-1,border_bottom_left_radius=-1,border_bottom_right_radius=-1):
pygame.draw.rect(window,rectangle_color,(rx-rw/2,ry-rh/2,rw,rh),0,border_radius,border_top_left_radius,border_top_right_radius,border_bottom_left_radius,border_bottom_right_radius)
font=pygame.font.Font(text_rfiles,text_size)
text=font.render(text_content,True,text_color)
tw,th = text.get_size()
window.blit(text,(rx-tw/2, ry-th/2))
pygame.display.update()
print("succeed")
pygame.time.wait(300)
#开始页
imagebox(image_start)
#开始,退出选项
bottombox(textbottom,70,(200,235,235),'start',(100,65,65),desk_width/2,desk_height/2,170,80)
bottombox(textbottom,70,(200,235,235),'exit',(100,65,65),desk_width/2,desk_height/2+WIN_HEIGHT/4,170,80)
def main():
clock=pygame.time.Clock()
#game loop
start = True
game = False
end = False
#print("start=",start)
while start:
clock.tick(FPS)
events=pygame.event.get()
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN :
mx,my = event.pos
if (desk_width/2-170/2<=mx<=desk_width/2+170/2 and desk_height/2-80/2<=my<=desk_height/2+80/2):
#print("1")
start=False
game=True
mousebottomdown(desk_width/2,desk_height/2,170,80,textbottom,70,(200,235,235),'start',(100,65,65))
#print("down1")
mousebottomup(desk_width/2,desk_height/2,170,80,textbottom,70,(200,235,235),'start',(100,65,65))
if (desk_width / 2 - 170 / 2 <= mx <= desk_width / 2 + 170 / 2 and desk_height / 2 + WIN_HEIGHT / 4 - 80 / 2 <= my <= desk_height / 2 + WIN_HEIGHT / 4 + 80 / 2):
mousebottomdown(desk_width / 2, desk_height / 2 + WIN_HEIGHT / 4, 170, 80, textbottom, 70,(200, 235, 235), 'exit', (100, 65, 65))
start = False
#print("2")
mousebottomup(desk_width/2,desk_height/2+WIN_HEIGHT/4,170,80,textbottom,70,(200,235,235),'exit',(100,65,65))
return
# goto.loop2
if event.type == pygame.MOUSEBUTTONUP :
#print("up")
pass
n=0
is_move = False
x_speed = 0
y_speed = 0
window.fill((0,0,0))
imagebox(image_game0)
pygame.display.update()
print("game",game)
while game:
pygame.event.pump()
clock.tick(FPS)
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
if n==0:
pygame.time.wait(1000)
imagebox(image_game1)
n=1
# 放置角色
elder = elder_right
elder_x, elder_y = desk_width/2-WIN_WIDTH/2+150, desk_height/2-WIN_HEIGHT/2+150
window.blit(elder, (elder_x, elder_y))
pygame.display.update()
else :pass
//以上运行都没问题
if event.type == pygame.KEYDOWN:
print("3",event) //从这里无法输出
if event.type == pygame.KEYDOWN:
char=chr(event.key)
print(char)
if char == 'w' and desk_width/2-WIN_WIDTH/2+555<=elder_x<=desk_width/2-WIN_WIDTH/2+565 and elder_y<=desk_height/2-WIN_HEIGHT/2+570:
is_move=True
imagebox(image_game1)
x_speed = 0
y_speed = -5
elder_x+=x_speed
elder_y+=y_speed
pygame.time.wait(300)
window.blit(elder,(elder_x,elder_y))
elif char == 'a' and (desk_width/2-WIN_WIDTH/2+100<=elder_x and (desk_height/2-WIN_HEIGHT/2+165<=elder_y<=desk_height/2-WIN_HEIGHT/2+175 or desk_height/2-WIN_HEIGHT/2+510<=elder_y<=desk_height/2-WIN_HEIGHT/2+570)) or (desk_width/2-WIN_WIDTH/2+555<=elder_x and desk_height/2-WIN_HEIGHT/2+165<=elder_y<=desk_height/2-WIN_HEIGHT/2+570):
is_move=True
imagebox(image_game1)
x_speed=-5
y_speed=0
elder_x += x_speed
elder_y += y_speed
elder=elder_left
window.blit(elder,(elder_x,elder_y))
elif char == 's' and desk_width/2-WIN_WIDTH/2+555<=elder_x<=desk_width/2-WIN_WIDTH/2+565 and desk_height/2-WIN_HEIGHT/2+170<=elder_y:
is_move=True
imagebox(image_game1)
x_speed=0
y_speed=5
elder_x += x_speed
elder_y += y_speed
window.blit(elder,(elder_x,elder_y))
elif char == 'd' and (elder_x<=desk_width/2+WIN_WIDTH/2-100 and (desk_height/2-WIN_HEIGHT/2+165<=elder_y<=desk_height/2-WIN_HEIGHT/2+175 or desk_height/2-WIN_HEIGHT/2+510<=elder_y<=desk_height/2-WIN_HEIGHT/2+570)) or (elder_x<=desk_width/2-WIN_WIDTH/2+565 and desk_height/2-WIN_HEIGHT/2+165<=elder_y<=desk_height/2-WIN_HEIGHT/2+570):
is_move=True
imagebox(image_game1)
x_speed=5
y_speed=0
elder_x += x_speed
elder_y += y_speed
elder=elder_right
window.blit(elder,(elder_x,elder_y))
pygame.display.update()
#键盘弹起
elif event.type == pygame.KEYUP:
is_move=False
pygame.display.update()
elif event.type == pygame.QUIT:
pygame.quit()
exit()
return
if __name__=="__main__":
main()
pygame.quit()
sys.exit()