鱼C论坛

 找回密码
 立即注册
查看: 4056|回复: 22

[作品展示] pygame- 找不同颜色

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

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

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

x
本帖最后由 shigure_takimi 于 2017-5-21 08:28 编辑

  1. ##  基本上来说,到了SUPERHARD模式就过不去了。

  2. import pygame
  3. from pygame.locals import *
  4. import random

  5. SIZE =30
  6. SCALE = 10
  7. MARGIN = 1
  8. WIDTH, HEIGHT = SIZE*SCALE+MARGIN*(SCALE+1), SIZE*SCALE+MARGIN*(SCALE+1)+30
  9. BLACK = [0, 0, 0]
  10. RED = [255, 0, 0]
  11. YELLOW = [255, 255, 0]
  12. BLUE = [0, 0, 255]

  13. pygame.init()
  14. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  15. subScreen = screen.subsurface((0, HEIGHT- 30, WIDTH, 30))
  16. pygame.display.set_caption('Find Different Color')
  17. font = pygame.font.SysFont('Arial', 50, True)
  18. fontSmall = pygame.font.SysFont('Arial', 16, True)


  19. def printTextCenter(text):
  20.     textImg = font.render(text, True, YELLOW, BLACK)
  21.     textImgRect = textImg.get_rect()
  22.     textImgRect.center = WIDTH//2, HEIGHT//2
  23.     screen.blit(textImg, textImgRect)

  24. def printTextOnSubScreen(text, x, font = fontSmall):
  25.     textImg = font.render(text, True, YELLOW)
  26.     textImgRect = textImg.get_rect()
  27.     textImgRect.centery = subScreen.get_rect().centery
  28.     textImgRect.x = x
  29.     subScreen.blit(textImg, textImgRect)
  30.    
  31. def generateColors(difficuty):
  32.     colorDifference = {'EASY':(20, 30), 'NORMAL':(15,20), \
  33.                   'HARD':(10,15), 'VERYHARD':(5, 10), 'SUPERHARD':(2, 5)}
  34.     randomPos = (random.randrange(0, SCALE), random.randrange(0, SCALE))
  35.     randomColor = (random.randint(30, 255), random.randint(30, 255), \
  36.                    random.randint(30, 255))
  37.     specialColor = (randomColor[0]-random.randint(colorDifference[difficuty][0], colorDifference[difficuty][1]), \
  38.                     randomColor[1]-random.randint(colorDifference[difficuty][0], colorDifference[difficuty][1]), \
  39.                     randomColor[2]-random.randint(colorDifference[difficuty][0], colorDifference[difficuty][1]))
  40.     return randomColor, specialColor, randomPos

  41. def generateGame(randomColor, specialColor, randomPos):
  42.     for i in range(SCALE):
  43.         for j in range(SCALE):           
  44.             x = j*SIZE+(j+1)*MARGIN
  45.             y = i*SIZE+(i+1)*MARGIN
  46.             if not (i == randomPos[0] and j == randomPos[1]):
  47.                 pygame.draw.rect(screen, randomColor, (x, y, SIZE, SIZE))
  48.             else:
  49.                 pygame.draw.rect(screen, specialColor, (x, y, SIZE, SIZE))

  50. def checkReuslt(randomPos):
  51.     global score
  52.     mousePos = pygame.mouse.get_pos()
  53.     i = randomPos[0]
  54.     j = randomPos[1]
  55.     x = j*SIZE+(j+1)*MARGIN
  56.     y = i*SIZE+(i+1)*MARGIN
  57.     rect = Rect(x, y, SIZE, SIZE)
  58.     subScreenRect = Rect((0, HEIGHT- 30, WIDTH, 30))
  59.     if rect.collidepoint(mousePos):
  60.         score += 1
  61.         return True
  62.     elif subScreenRect.collidepoint(mousePos):
  63.         pass
  64.     else:
  65.         return False

  66. def resetColor():
  67.     global randomColor, specialColor, randomPos, result, difficuty
  68.     randomColor, specialColor, randomPos = generateColors(difficuty)
  69.     result = None
  70.    

  71. result = None
  72. score = 0
  73. difficuty = 'EASY'
  74. randomColor, specialColor, randomPos = generateColors(difficuty)

  75. done = False
  76. while not done:
  77.     for event in pygame.event.get():
  78.         if event.type == QUIT:
  79.             done = True
  80.         if event.type == MOUSEBUTTONDOWN and event.button == 1:
  81.             if result == None:
  82.                 result = checkReuslt(randomPos)
  83.             elif result == True:
  84.                 resetColor()
  85.             else:
  86.                 difficuty = 'EASY'
  87.                 resetColor()
  88.                 score = 0
  89.                
  90.             
  91.     screen.fill(BLACK)
  92.     subScreen.fill(BLUE)
  93.     pygame.draw.rect(screen, RED, (0, HEIGHT-30, WIDTH, 30), 2)
  94.     generateGame(randomColor, specialColor, randomPos)
  95.     printTextOnSubScreen('SCORE: '+str(score), WIDTH - 100)
  96.     printTextOnSubScreen(difficuty, 20)
  97.     if result == True:
  98.         printTextCenter('Correct!')
  99.     elif result == False:
  100.         printTextCenter('Game Over!')
  101.     if score >= 40:
  102.         difficuty = 'SUPERHARD'
  103.     elif score >= 30:
  104.         difficuty = 'VERYHARD'
  105.     elif score >= 20:
  106.         difficuty = 'HARD'
  107.     elif score >= 10:
  108.         difficuty = 'NORMAL'
  109.     else:
  110.         difficuty = 'EASY'
  111.     pygame.display.flip()

  112. pygame.quit()
复制代码
1.png
2.png

点评

我很赞同!: 5.0
我很赞同!: 5
支持原创~  发表于 2017-5-19 16:10

评分

参与人数 2荣誉 +13 鱼币 +13 贡献 +8 收起 理由
Yolanda小点 + 5 + 5 + 3 好厉害,什么时候我也能写这个么小游戏!!.
小甲鱼 + 8 + 8 + 5 支持楼主!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-19 15:52:08 | 显示全部楼层
这类小游戏很容易上瘾的说~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-5-19 16:25:11 | 显示全部楼层
小甲鱼 发表于 2017-5-19 15:52
这类小游戏很容易上瘾的说~

是视频里的小甲鱼老师吗?
竟然眷顾我的狗屁程序,三生有幸了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-19 16:51:31 | 显示全部楼层
shigure_takimi 发表于 2017-5-19 16:25
是视频里的小甲鱼老师吗?
竟然眷顾我的狗屁程序,三生有幸了。

继续加油哇~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-19 17:14:47 From FishC Mobile | 显示全部楼层
小甲鱼 发表于 2017-5-19 16:51
继续加油哇~

31了。没机会做程序员了,只能自己玩了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-19 22:17:10 | 显示全部楼层
shigure_takimi 发表于 2017-5-19 16:25
是视频里的小甲鱼老师吗?
竟然眷顾我的狗屁程序,三生有幸了。

我不觉得程序员要吃年轻饭,程序员是需要活到老学到老的职业代表,因此经验,技术,知识的累积比年轻更重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-19 23:09:51 | 显示全部楼层
小甲鱼 发表于 2017-5-19 22:17
我不觉得程序员要吃年轻饭,程序员是需要活到老学到老的职业代表,因此经验,技术,知识的累积比年轻更重 ...

道理虽如此,但我这种以前没有编程经验,没有计算机和软件基础的人,一般的公司连机会都不会给我。只能自己小打小闹到老了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-19 23:35:39 | 显示全部楼层
shigure_takimi 发表于 2017-5-19 23:09
道理虽如此,但我这种以前没有编程经验,没有计算机和软件基础的人,一般的公司连机会都不会给我。只能自 ...

不要这么消极啦,只要坚持每天学习,不说卓越,三五年达到行业平均水平以上肯定没问题的!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-20 07:40:45 From FishC Mobile | 显示全部楼层
小甲鱼 发表于 2017-5-19 23:35
不要这么消极啦,只要坚持每天学习,不说卓越,三五年达到行业平均水平以上肯定没问题的!

谢谢小甲鱼。
我会坚持下去。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-20 12:59:24 | 显示全部楼层
把代码设置为 回复可见
效果更好~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-20 15:25:42 From FishC Mobile | 显示全部楼层
新手·ing 发表于 2017-5-20 12:59
把代码设置为 回复可见
效果更好~

我都没注意到默认是回复可见。回去研究研究怎么设置。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-20 18:02:06 | 显示全部楼层

  1. ##  增加了“超困难“和”究极困难“模式下显示正确位置的功能。

  2. import pygame
  3. from pygame.locals import *
  4. import random

  5. SIZE =30
  6. SCALE = 10
  7. MARGIN = 2
  8. WIDTH, HEIGHT = SIZE*SCALE+MARGIN*(SCALE+1), SIZE*SCALE+MARGIN*(SCALE+1)+30
  9. BLACK = [0, 0, 0]
  10. RED = [255, 0, 0]
  11. YELLOW = [255, 255, 0]
  12. BLUE = [0, 0, 255]



  13. pygame.init()
  14. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  15. subScreen = screen.subsurface((0, HEIGHT- 30, WIDTH, 30))
  16. pygame.display.set_caption('Find Different Color')
  17. font = pygame.font.SysFont('msgothicmsuigothicmspgothic', 50)
  18. fontSmall = pygame.font.SysFont('msgothicmsuigothicmspgothic', 16, True)


  19. def printTextCenter(text):
  20.     textImg = font.render(text, True, YELLOW, BLACK)
  21.     textImgRect = textImg.get_rect()
  22.     textImgRect.center = WIDTH//2, HEIGHT//2
  23.     screen.blit(textImg, textImgRect)

  24. def printTextOnSubScreen(text, x, font = fontSmall):
  25.     textImg = font.render(text, True, YELLOW)
  26.     textImgRect = textImg.get_rect()
  27.     textImgRect.centery = subScreen.get_rect().centery
  28.     textImgRect.x = x
  29.     subScreen.blit(textImg, textImgRect)
  30. def printTextOnSubScreenCenter(text, font = fontSmall):
  31.     textImg = font.render(text, True, YELLOW)
  32.     textImgRect = textImg.get_rect()
  33.     textImgRect.center = subScreen.get_rect().center
  34.     subScreen.blit(textImg, textImgRect)
  35.    
  36. def generateColors(difficuty):
  37.     colorDifference = {'簡単':(20, 30), '普通':(15,20), \
  38.                   '困難':(10,15), '超困難':(5, 10), '究極困難':(2, 5)}
  39.     randomPos = (random.randrange(0, SCALE), random.randrange(0, SCALE))
  40.     randomColor = (random.randint(30, 255), random.randint(30, 255), \
  41.                    random.randint(30, 255))
  42.     specialColor = (randomColor[0]-random.randint(colorDifference[difficuty][0], colorDifference[difficuty][1]), \
  43.                     randomColor[1]-random.randint(colorDifference[difficuty][0], colorDifference[difficuty][1]), \
  44.                     randomColor[2]-random.randint(colorDifference[difficuty][0], colorDifference[difficuty][1]))
  45.     return randomColor, specialColor, randomPos

  46. def generateGame(randomColor, specialColor, randomPos):
  47.     for i in range(SCALE):
  48.         for j in range(SCALE):           
  49.             x = j*SIZE+(j+1)*MARGIN
  50.             y = i*SIZE+(i+1)*MARGIN
  51.             if not (i == randomPos[0] and j == randomPos[1]):
  52.                 pygame.draw.rect(screen, randomColor, (x, y, SIZE, SIZE))
  53.             else:
  54.                 pygame.draw.rect(screen, specialColor, (x, y, SIZE, SIZE))

  55. def checkReuslt(randomPos):
  56.     global score, showPos
  57.     mousePos = pygame.mouse.get_pos()
  58.     i = randomPos[0]
  59.     j = randomPos[1]
  60.     x = j*SIZE+(j+1)*MARGIN
  61.     y = i*SIZE+(i+1)*MARGIN
  62.     rect = Rect(x, y, SIZE, SIZE)
  63.     subScreenRect = Rect((0, HEIGHT- 30, WIDTH, 30))
  64.     if rect.collidepoint(mousePos):
  65.         score += 1
  66.         return True
  67.     elif subScreenRect.collidepoint(mousePos):
  68.         showPos = True
  69.     else:
  70.         return False

  71. def resetColor():
  72.     global randomColor, specialColor, randomPos, result, difficuty
  73.     randomColor, specialColor, randomPos = generateColors(difficuty)
  74.     result = None
  75.    

  76. result = None
  77. score = 0
  78. difficuty = '簡単'
  79. randomColor, specialColor, randomPos = generateColors(difficuty)
  80. showPos = False

  81. done = False
  82. while not done:
  83.     for event in pygame.event.get():
  84.         if event.type == QUIT:
  85.             done = True
  86.         if event.type == MOUSEBUTTONDOWN and event.button == 1:
  87.             if result == None:
  88.                 result = checkReuslt(randomPos)
  89.             elif result == True:
  90.                 resetColor()
  91.                 showPos = False
  92.             else:
  93.                 difficuty = '簡単'
  94.                 resetColor()
  95.                 score = 0
  96.                 showPos = False
  97.                
  98.             
  99.     screen.fill(BLACK)
  100.     subScreen.fill(BLUE)
  101.     pygame.draw.rect(screen, RED, (0, HEIGHT-30, WIDTH, 30), 2)
  102.     generateGame(randomColor, specialColor, randomPos)
  103.     printTextOnSubScreen('得点: '+str(score), WIDTH - 80)
  104.     printTextOnSubScreen(difficuty, 20)
  105.     if result == True:
  106.         printTextCenter('正解!')
  107.     elif result == False:
  108.         printTextCenter('ゲーム終了')
  109.     if score >= 40:
  110.         difficuty = '究極困難'
  111.     elif score >= 30:
  112.         difficuty = '超困難'
  113.     elif score >= 20:
  114.         difficuty = '困難'
  115.     elif score >= 10:
  116.         difficuty = '普通'
  117.     else:
  118.         difficuty = '簡単'

  119.     if difficuty in ['超困難', '究極困難']:
  120.         if not showPos:
  121.             printTextOnSubScreenCenter('正解表示')
  122.         else:
  123.             printTextOnSubScreenCenter(str((randomPos[0]+1, randomPos[1]+1)))
  124.     pygame.display.flip()

  125. pygame.quit()
复制代码
1.PNG
2.PNG
3.PNG
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-20 19:34:11 | 显示全部楼层
0.png

Bug:当点击不同点时,会计数,但是颜色不会变,需再用鼠标点一次才切换到一下关
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-20 20:44:23 | 显示全部楼层
零度非安全 发表于 2017-5-20 19:34
Bug:当点击不同点时,会计数,但是颜色不会变,需再用鼠标点一次才切换到一下关

你是自己敲了一遍代码吗?
哪里出错了吧。把代码粘上来看看如何。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-20 20:50:10 | 显示全部楼层
零度非安全 发表于 2017-5-20 19:34
Bug:当点击不同点时,会计数,但是颜色不会变,需再用鼠标点一次才切换到一下关
  1. def printTextCenter(text):
  2.     textImg = font.render(text, True, YELLOW, BLACK)
  3.     textImgRect = textImg.get_rect()
  4.     textImgRect.center = WIDTH//2, HEIGHT//2
  5.     screen.blit(textImg, textImgRect)

  6. def printTextOnSubScreen(text, x, font = fontSmall):
  7.     textImg = font.render(text, True, YELLOW)
  8.     textImgRect = textImg.get_rect()
  9.     textImgRect.centery = subScreen.get_rect().centery
  10.     textImgRect.x = x
  11.     subScreen.blit(textImg, textImgRect)
复制代码


这两个方法你的和我的不一样。
‘MODE’和‘SCORE’都跑到顶部去了。

另外,点颜色之后,不管对不对都会显示结果‘Correct’或者‘Game over’,然后再点击之后继续下一组颜色,开始就是想这么做的,我认为不是bug。如果想点对了就直接换颜色,也简单,不过不想改了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-20 21:11:02 | 显示全部楼层
本帖最后由 零度非安全 于 2017-5-20 21:12 编辑
shigure_takimi 发表于 2017-5-20 20:50
这两个方法你的和我的不一样。
‘MODE’和‘SCORE’都跑到顶部去了。


0.png

是我敲错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-25 21:45:37 | 显示全部楼层
都好厉害啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-31 19:57:17 | 显示全部楼层

没有啦。
还差得远呢。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-4 17:05:53 | 显示全部楼层
  1. import pygame, random
  2. from pygame.locals import *

  3. def getGame(m, n, k): # m排n列,k个炸弹(k<m*n)
  4.     game = [['0' for i in range(n)] for j in range(m)]
  5.     a = list(range(m))
  6.     b = list(range(n))
  7.     bombsPos = []
  8.     for i in a:
  9.         for j in b:
  10.             bombsPos.append((i,j))
  11.     random.shuffle(bombsPos)
  12.     for i in range(k):
  13.         game[bombsPos[i][0]][bombsPos[i][1]] = 'b'
  14.     for i in range(m):
  15.         game[i].insert(0, '0')
  16.         game[i].append('0')
  17.     game.insert(0, ['0' for i in range(n+2)])
  18.     game.append(['0' for i in range(n+2)])
  19.     for i in range(1, m+1):
  20.         for j in range(1, n+1):
  21.             if game[i][j] != 'b':
  22.                 around = [game[i-1][j-1], game[i-1][j], game[i-1][j+1],\
  23.                                game[i][j-1], game[i][j+1],\
  24.                                game[i+1][j-1], game[i+1][j], game[i+1][j+1]]
  25.                 countsOfBombs = around.count('b')
  26.                 game[i][j] = str(countsOfBombs)
  27.     game.pop(0)
  28.     game.pop()
  29.     for i in range(m):
  30.         game[i].pop()
  31.         game[i].pop(0)
  32.     return game

  33. size = 30, 16
  34. bombsCount = 99
  35. game = getGame(size[1], size[0], bombsCount)
  36. screen = pygame.display.set_mode((size[0]*16, size[1]*16))
  37. pygame.display.set_caption('bombsCount = '+str(bombsCount))


  38. pics = {'start':'startImg.png',
  39.         '0':'0.png',
  40.         '1':'1.png',
  41.         '2':'2.png',
  42.         '3':'3.png',
  43.         '4':'4.png',
  44.         '5':'5.png',
  45.         '6':'6.png',
  46.         '7':'7.png',
  47.         'b':'bomb.png'}

  48. done = False
  49. while not done:
  50.     for event in pygame.event.get():
  51.         if event.type == QUIT:
  52.             done = True
  53.         if event.type == MOUSEBUTTONDOWN and event.button == 1:
  54.             pass
  55.     for i in range(size[1]):
  56.         for j in range(size[0]):
  57.             img = pygame.image.load(pics[game[i][j]]).convert()
  58.             screen.blit(img, (j*16, i*16))
  59.     for i in range(1, size[1]):
  60.         pygame.draw.line(screen, [125,125,125], (0, i*16),(size[0]*16, i*16))
  61.     for i in range(1, size[0]):
  62.         pygame.draw.line(screen, [125,125,125], (i*16, 0 ),(i*16, size[1]*16))
  63.         
  64.    
  65.     pygame.display.flip()

  66. pygame.quit()

  67. # 暂时是静态的。有时间改成可以动态扫雷的。
复制代码
wxid_xb93vztvd94422_1515056698060_68.png

扫雷图片.rar

3.67 KB, 下载次数: 0

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-3 19:50:59 From FishC Mobile | 显示全部楼层
本帖最后由 shigure_takimi 于 2018-3-9 11:52 编辑
  1. #!/usr/bin/python
  2. def f(n):
  3.         s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  4.         width = 2 * n- 1
  5.         for i in range(n):
  6.                 a = (i//26)*s+s[:i%26+1]
  7.                 a = a + a[::-1][1:]
  8.                 print(a.center(width))
  9. f(3)
  10. f(5)
  11. f(28)
复制代码
1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 06:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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