鱼C论坛

 找回密码
 立即注册
查看: 2772|回复: 2

[技术交流] 用pygame写的猜拳游戏(见笑了)

[复制链接]
发表于 2017-4-5 15:22:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 shigure_takimi 于 2017-4-13 08:18 编辑
  1. import pygame, random
  2. from pygame.locals import *

  3. pygame.init()
  4. screen = pygame.display.set_mode((800,600))
  5. pygame.display.set_caption("Jan-Ken-Pon")

  6. jan = pygame.image.load("1.jpg").convert()
  7. ken = pygame.image.load("2.jpg").convert()
  8. pon = pygame.image.load("3.jpg").convert()
  9. jan1 = pygame.transform.smoothscale(jan,(jan.get_width()//2, jan.get_height()//2))
  10. jan2 = pygame.transform.scale2x(jan)
  11. jan2_com = pygame.transform.flip(jan2, True, False)
  12. ken1 = pygame.transform.smoothscale(ken,(ken.get_width()//2, ken.get_height()//2))
  13. ken2 = pygame.transform.scale2x(ken)
  14. ken2_com = pygame.transform.flip(ken2, True, False)
  15. pon1 = pygame.transform.smoothscale(pon,(pon.get_width()//2, pon.get_height()//2))
  16. pon2 = pygame.transform.scale2x(pon)
  17. pon2_com = pygame.transform.flip(pon2, True, False)

  18. font1 = pygame.font.SysFont(None, 80,bold = True)
  19. font2 = pygame.font.SysFont(None, 40,bold = True)
  20. font3 = pygame.font.SysFont(None, 25,bold = True)

  21. text1 = font1.render("JAN-KEN-PON",True,(255,255,0))
  22. text2 = font2.render("Click to Select",True,(255,255,0))
  23. text3 = font2.render("VS",True,(255,255,0))
  24. text4 = font2.render("Enter to Reset",True,(255,255,0))
  25. text_per = font3.render("PERSON",True,(255,255,0))
  26. text_com = font3.render("COMPUTER",True,(255,255,0))

  27. com_selected = [jan2_com,ken2_com,pon2_com]
  28. selected = [jan2,ken2,pon2]
  29. clicked = False
  30. # 勝負の条件
  31. win = [(jan2,ken2_com),(ken2,pon2_com),(pon2,jan2_com)]
  32. lost = [(ken2,jan2_com),(pon2,ken2_com),(jan2,pon2_com)]
  33. draw = [(jan2,jan2_com),(ken2,ken2_com),(pon2,pon2_com)]
  34. per = None
  35. com = None
  36. result = 'RESULT'
  37. clock = pygame.time.Clock()

  38. done = False
  39. while not done:
  40.     pos = pygame.mouse.get_pos()
  41.     for event in pygame.event.get():
  42.         if event.type == QUIT:
  43.             done = True
  44.         if not clicked and event.type == MOUSEBUTTONDOWN and 520 < pos[1] < 585:
  45.             if 250 < pos[0] < 315:
  46.                 per = selected[0]
  47.                 clicked = True
  48.             elif 367 < pos[0] < 432:
  49.                 per = selected[1]
  50.                 clicked = True
  51.             elif 485 < pos[0] < 550:
  52.                 per = selected[2]
  53.                 clicked = True
  54.     pressed_keys = pygame.key.get_pressed()
  55.     if clicked and pressed_keys[K_RETURN]:
  56.         clicked = False
  57.         result = 'RESULT'
  58.    
  59.     screen.fill((0,120,255))
  60.     screen.blit(text1,(400 - text1.get_width()//2,30))
  61.     screen.blit(text2,(400 - text2.get_width()//2,460))
  62.     screen.blit(text3,(400 - text3.get_width()//2,300))
  63.     screen.blit(text4,(400 - text4.get_width()//2,485))
  64.     screen.blit(text_com,(230 - text_com.get_width()//2,140))
  65.     screen.blit(text_per,(570 - text_per.get_width()//2,140))
  66.     result_img = font2.render(result,True,(255,0,0))
  67.     screen.blit(result_img,(400 - result_img.get_width()//2,100))
  68.     screen.blit(jan1,(250,520))
  69.     screen.blit(ken1,(367,520))
  70.     screen.blit(pon1,(485,520))

  71.     # ジャンケンポン
  72.     if not clicked:
  73.         com = random.choice(com_selected)
  74.         screen.blit(random.choice(com_selected),(100,170))
  75.         screen.blit(random.choice(selected),(440,170))
  76.     else:
  77.         screen.blit(com,(100,170))
  78.         screen.blit(per,(440,170))

  79.     # 勝負を判断
  80.     if clicked:
  81.         if (per,com) in win:
  82.             result = 'You Win!!'
  83.         if (per,com) in lost:
  84.             result = 'You Lost!!'
  85.         if (per,com) in draw:
  86.             result = 'Draw Game!!'  
  87.     clock.tick(60)
  88.     pygame.display.flip()

  89. pygame.quit()
复制代码
1.JPG
2.jpg
3.JPG
1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-4-5 15:52:13 | 显示全部楼层
不错,完美运行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-16 09:44:19 | 显示全部楼层
  1. def f(n):
  2.     if n < 1:
  3.         return None
  4.     else:
  5.         fenzi = 2
  6.         fenmu = 1
  7.         s = []
  8.         total = 0
  9.         for i in range(n):
  10.             total += fenzi/fenmu
  11.             s.append(str(fenzi)+'/'+str(fenmu))
  12.             fenzi, fenmu = fenzi+fenmu, fenzi
  13.         return '+'.join(s)+'='+str(total)

  14. print(f(1))
  15. print(f(20))

  16. #  2/1=2.0
  17. #  2/1+3/2+5/3+8/5+13/8+21/13+34/21+55/34+89/55+144/89+233/144+377/233+610/377+987/610+1597/987+2584/1597+4181/2584+6765/4181+10946/6765+17711/10946=32.66026079864164
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 01:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表