|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 陈尚涵 于 2020-4-20 13:25 编辑
新手做了一个全原创的作品呀,谢谢鱼C!
废话不多说,进入正题。
在目录下创建一个文件叫main.py,代码如下:
- # coding:utf-8
- # 导入模块
- import pygame
- from sys import exit
- from pygame.freetype import Font
- # 初始化
- pygame.init()
- window_size = 600, 600
- window_color = 255, 255, 255
- window_caption = pygame.display.set_caption('你喜欢我吗?')
- window = pygame.display.set_mode(window_size)
- # 导入图片
- not_granted = pygame.image.load('not-granted.png')
- granted = pygame.image.load('granted.png')
- # 创建变量
- writer = Font('C://windows//Fonts//msyh.ttc', 36)
- blit_normal = 1
- MouseDown = False
- stop = False
- PressExit = False
- # 进入游戏的中心环节(死循环)
- while 1:
- # 不停的更新变量
- mouse_pos = pygame.mouse.get_pos()
- # 条件判断
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- PressExit = True
- else:
- PressExit = False
- if event.type == pygame.MOUSEBUTTONDOWN:
- MouseDown = True
- else:
- MouseDown = False
- if stop:
- if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
- exit()
- # 补充颜色
- window.fill(window_color)
- # 按照情况选择模式
- if not ((mouse_pos[0] > 325 and mouse_pos[0] < 595) and (mouse_pos[1] > 400 and mouse_pos[1] < 525) and not stop):
- blit_normal = 0
- else:
- blit_normal = 1
- # 对模式进行判断并处理
- if blit_normal:
- window.blit(granted, (325, 400))
- window.blit(not_granted, (25, 400))
- if (mouse_pos[0] > 325 and mouse_pos[0] < 595) and (mouse_pos[1] > 400 and mouse_pos[1] < 525) and MouseDown and not stop:
- stop = True
- else:
- window.blit(not_granted, (325, 400))
- window.blit(granted, (25, 400))
- if (mouse_pos[0] > 25 and mouse_pos[0] < 295) and (mouse_pos[1] > 400 and mouse_pos[1] < 525) and MouseDown and not stop:
- stop = True
- # 画一些图形,写一些文字
- pygame.draw.line(window, (0, 0, 0), (0, 300), (600, 300), 10)
- if not stop and not PressExit:
- writer.render_to(window, (150, 50), 'Hey, 你喜欢我吗?')
- elif PressExit and not stop:
- writer.render_to(window, (125, 50), '我才不会退出呢!')
- else:
- writer.render_to(window, (10, 50), '我就知道你会喜欢我^o^(现在按下Esc退出)', size=30)
- # 更新显示
- pygame.display.update()
- # 游戏结束
复制代码
 欢迎留言 
图片: |
-
图片2
-
图片1
|