|
楼主 |
发表于 2017-5-20 18:02:06
|
显示全部楼层
- ## 增加了“超困难“和”究极困难“模式下显示正确位置的功能。
- import pygame
- from pygame.locals import *
- import random
- SIZE =30
- SCALE = 10
- MARGIN = 2
- WIDTH, HEIGHT = SIZE*SCALE+MARGIN*(SCALE+1), SIZE*SCALE+MARGIN*(SCALE+1)+30
- BLACK = [0, 0, 0]
- RED = [255, 0, 0]
- YELLOW = [255, 255, 0]
- BLUE = [0, 0, 255]
- pygame.init()
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- subScreen = screen.subsurface((0, HEIGHT- 30, WIDTH, 30))
- pygame.display.set_caption('Find Different Color')
- font = pygame.font.SysFont('msgothicmsuigothicmspgothic', 50)
- fontSmall = pygame.font.SysFont('msgothicmsuigothicmspgothic', 16, True)
- def printTextCenter(text):
- textImg = font.render(text, True, YELLOW, BLACK)
- textImgRect = textImg.get_rect()
- textImgRect.center = WIDTH//2, HEIGHT//2
- screen.blit(textImg, textImgRect)
- def printTextOnSubScreen(text, x, font = fontSmall):
- textImg = font.render(text, True, YELLOW)
- textImgRect = textImg.get_rect()
- textImgRect.centery = subScreen.get_rect().centery
- textImgRect.x = x
- subScreen.blit(textImg, textImgRect)
- def printTextOnSubScreenCenter(text, font = fontSmall):
- textImg = font.render(text, True, YELLOW)
- textImgRect = textImg.get_rect()
- textImgRect.center = subScreen.get_rect().center
- subScreen.blit(textImg, textImgRect)
-
- def generateColors(difficuty):
- colorDifference = {'簡単':(20, 30), '普通':(15,20), \
- '困難':(10,15), '超困難':(5, 10), '究極困難':(2, 5)}
- randomPos = (random.randrange(0, SCALE), random.randrange(0, SCALE))
- randomColor = (random.randint(30, 255), random.randint(30, 255), \
- random.randint(30, 255))
- specialColor = (randomColor[0]-random.randint(colorDifference[difficuty][0], colorDifference[difficuty][1]), \
- randomColor[1]-random.randint(colorDifference[difficuty][0], colorDifference[difficuty][1]), \
- randomColor[2]-random.randint(colorDifference[difficuty][0], colorDifference[difficuty][1]))
- return randomColor, specialColor, randomPos
- def generateGame(randomColor, specialColor, randomPos):
- for i in range(SCALE):
- for j in range(SCALE):
- x = j*SIZE+(j+1)*MARGIN
- y = i*SIZE+(i+1)*MARGIN
- if not (i == randomPos[0] and j == randomPos[1]):
- pygame.draw.rect(screen, randomColor, (x, y, SIZE, SIZE))
- else:
- pygame.draw.rect(screen, specialColor, (x, y, SIZE, SIZE))
- def checkReuslt(randomPos):
- global score, showPos
- mousePos = pygame.mouse.get_pos()
- i = randomPos[0]
- j = randomPos[1]
- x = j*SIZE+(j+1)*MARGIN
- y = i*SIZE+(i+1)*MARGIN
- rect = Rect(x, y, SIZE, SIZE)
- subScreenRect = Rect((0, HEIGHT- 30, WIDTH, 30))
- if rect.collidepoint(mousePos):
- score += 1
- return True
- elif subScreenRect.collidepoint(mousePos):
- showPos = True
- else:
- return False
- def resetColor():
- global randomColor, specialColor, randomPos, result, difficuty
- randomColor, specialColor, randomPos = generateColors(difficuty)
- result = None
-
- result = None
- score = 0
- difficuty = '簡単'
- randomColor, specialColor, randomPos = generateColors(difficuty)
- showPos = False
- done = False
- while not done:
- for event in pygame.event.get():
- if event.type == QUIT:
- done = True
- if event.type == MOUSEBUTTONDOWN and event.button == 1:
- if result == None:
- result = checkReuslt(randomPos)
- elif result == True:
- resetColor()
- showPos = False
- else:
- difficuty = '簡単'
- resetColor()
- score = 0
- showPos = False
-
-
- screen.fill(BLACK)
- subScreen.fill(BLUE)
- pygame.draw.rect(screen, RED, (0, HEIGHT-30, WIDTH, 30), 2)
- generateGame(randomColor, specialColor, randomPos)
- printTextOnSubScreen('得点: '+str(score), WIDTH - 80)
- printTextOnSubScreen(difficuty, 20)
- if result == True:
- printTextCenter('正解!')
- elif result == False:
- printTextCenter('ゲーム終了')
- if score >= 40:
- difficuty = '究極困難'
- elif score >= 30:
- difficuty = '超困難'
- elif score >= 20:
- difficuty = '困難'
- elif score >= 10:
- difficuty = '普通'
- else:
- difficuty = '簡単'
- if difficuty in ['超困難', '究極困難']:
- if not showPos:
- printTextOnSubScreenCenter('正解表示')
- else:
- printTextOnSubScreenCenter(str((randomPos[0]+1, randomPos[1]+1)))
- pygame.display.flip()
- pygame.quit()
复制代码 |
|