|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import pygame
from pygame.locals import *
import random
SIZE = 30
SCALE = 10
MARGIN = 1
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('Arial', 50, True)
fontSmall = pygame.font.SysFont('Arial', 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 generateColors(difficulty):
colorDifference = {'EASY':(20, 30), 'NORMAL':(15,20), 'HARD':(10,15), 'VERYHARD':(5, 10), 'SUPERHARD':(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[difficulty][0], colorDifference[difficulty][1]), randomColor[1]-random.randint(colorDifference[difficulty][0], colorDifference[difficulty][1]), randomColor[2]-random.randint(colorDifference[difficulty][0], colorDifference[difficulty][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
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):
pass
else:
return False
def resetColor():
global randomColor, specialColor, randomPos, result, difficuty
randomColor, specialColor, randomPos = generateColors(difficulty)
result = None
result = None
score = 0
difficulty = 'EASY'
randomColor, specialColor, randomPos = generateColors(difficulty)
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()
else:
difficuty = 'EASY'
resetColor()
score = 0
screen.fill(BLACK)
subScreen.fill(BLUE)
pygame.draw.rect(screen, RED, (0, HEIGHT-30, WIDTH, 30), 2)
generateGame(randomColor, specialColor, randomPos)
printTextOnSubScreen('SCORE: '+str(score), WIDTH - 100)
printTextOnSubScreen(difficulty, 20)
if result == True:
printTextCenter('Correct!')
elif result == False:
printTextCenter('Game Over!')
if score >= 40:
difficulty = 'SUPERHARD'
elif score >= 30:
difficulty = 'VERYHARD'
elif score >= 20:
difficulty = 'HARD'
elif score >= 10:
difficulty = 'NORMAL'
else:
difficulty = 'EASY'
if score >= 60:
done = True
pygame.display.flip()
pygame.quit() |
|