shigure-takimi 发表于 2020-1-30 01:49:48

找不同颜色的小游戏

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 =
RED =
YELLOW =
BLUE =

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-random.randint(colorDifference, colorDifference), randomColor-random.randint(colorDifference, colorDifference), randomColor-random.randint(colorDifference, colorDifference))
    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 and j == randomPos):
                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
    j = randomPos
    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()

mal 发表于 2020-2-10 10:47:32

我发现你的算法中,不同的颜色永远都是深一点哎,因为随机的新颜色都是rgb - 某个随机数
还有我玩到46分实在是玩不下去了.....

ZhKQYu 发表于 2020-5-16 10:36:58

from PIL import Image, ImageGrab
import pyautogui as gui
import random
import time
import os



def screenshot(start_x, start_y, end_x, end_y):
    image = ImageGrab.grab((start_x, start_y, end_x, end_y))
    image.save('C:\\Users\\DELL\\Desktop\\游戏截图.png')
    img = Image.open('C:\\Users\\DELL\\Desktop\\游戏截图.png')
    img = img.convert('RGB')
    print('截图准备就绪...')
    return img


def img_rgb(x, y, image):
    add_colour = []
    for w in range(10):
      y += 38.8*w
      for l in range(10):
            x += 38.8*l
            rgb_colour = image.getpixel((x, y))
            add_colour.append(rgb_colour)
            x = 19.4
      x = 19.4
      y = 19.4
    print('找到全部RGB')
    print('共计 %d 个' % len(add_colour))
    return add_colour


def search_diff(all_colour):
    random_colour = random.choice(all_colour)
    for each in all_colour:
      if each != random_colour:
            break
    colour_index = all_colour.index(each)
    print('找到目标位置RGB...')
    return colour_index
                        

def colour_posi(index):
    remainder = index % 10
    multiple = index // 10
    x = 19.4 + remainder*38.8
    y = 58.2+ multiple*38.8

    print('找到目标位置,准备点击...')
   
    return x,y


def main(start_x=0, start_y=40, end_x=388, end_y=428, x=19.4, y=19.4):
    image = screenshot(start_x, start_y, end_x, end_y) #首先对指定目标截图
    all_colour = img_rgb(x, y, image) #对所有RGB进行测度,放入一个列表中
    index_colour = search_diff(all_colour) #找到不同的RGB
    position_x,position_y = colour_posi(index_colour) #找到颜色不同的坐标
    gui.click(position_x, position_y)#实现点击
    time.sleep(0.25)
    gui.click(position_x, position_y)



print('外挂开启中.......请稍后...')
time.sleep(2)
count = 0
while count != 60:
    time.sleep(0.5)
    count += 1
    if __name__ == '__main__':
      main()
    print('第 %d 轮已完成...' % count)
    print('\n\n')
    if count != 60:
      print('第 %d 轮准备开始... ' % (count+1))
    if count == 60:
      print('外挂结束...')
os.remove('C:\\Users\\DELL\\Desktop\\游戏截图.png')
input('按回车键退出')


写了个自动找不同颜色的脚本{:10_256:}
页: [1]
查看完整版本: 找不同颜色的小游戏