鱼C论坛

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

[技术交流] 找不同颜色的小游戏

[复制链接]
发表于 2020-1-30 01:49:48 | 显示全部楼层 |阅读模式

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

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

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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-10 10:47:32 | 显示全部楼层
我发现你的算法中,不同的颜色永远都是深一点哎,因为随机的新颜色都是rgb - 某个随机数
还有我玩到46分实在是玩不下去了.....
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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('按回车键退出')
写了个自动找不同颜色的脚本
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-21 08:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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