Pygame 井字游戏逻辑?我该怎么做 [英] Pygame Tic Tak Toe Logic? How Would I Do It

查看:50
本文介绍了Pygame 井字游戏逻辑?我该怎么做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 pygame 制作一个井字游戏,我想知道我将如何处理这里的逻辑,这就是我目前所拥有的.视频我只有当我点击中间按钮时它才会在屏幕上显示播放器 2 x 然后悬停在我鼠标上的图像将变成 O 以便播放器 1 转去 但是我的问题如果玩家 1 连续获得 3 个或玩家 2 连续获得 3 个,我将如何执行逻辑 我真的很困惑,我需要有人陪我扔它>

我现在的井字游戏代码

导入pygame,随机pygame.init()# 绘制我们的窗口window = pygame.display.set_mode((500,540),pygame.NOFRAME)pygame.display.set_caption(井字游戏")MANUAL_CURSOR = pygame.image.load('nw.png').convert_alpha()MANUAL_CURSOR2 = pygame.image.load('nOW.png').convert_alpha()bg = pygame.image.load("ticO.png")每秒帧数 = 40时钟 = pygame.time.Clock()类按钮():def __init__(self, color, x,y,width,height, text=''):self.color = 颜色自我.x = x自我.y = yself.width = 宽度self.height = 高度self.text = 文本self.over = 错误def draw(self,window,outline=None):#调用该方法在屏幕上绘制按钮如果大纲:pygame.draw.rect(窗口, 轮廓, (self.x-2,self.y-2,self.width+4,self.height+4),0)pygame.draw.rect(窗口,self.color,(self.x,self.y,self.width,self.height),0)如果 self.text != '':font = pygame.font.SysFont('comicsans', 60)text = font.render(self.text, 1, (0,0,0))window.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))def isOver(self, pos):#Pos 是鼠标位置或 (x,y) 坐标元组如果 pos[0] >self.x 和 pos[0] self.y 和 pos[1] = 0:window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))pygame.mouse.set_visible(假)如果分数 >= 1:window.blit(MANUAL_CURSOR2, MANUAL_CURSOR2.get_rect(center = pygame.mouse.get_pos()))如果分数 >= 2:window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))如果分数 >= 3:window.blit(MANUAL_CURSOR2, MANUAL_CURSOR2.get_rect(center = pygame.mouse.get_pos()))如果分数 >= 4:window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))如果分数 >= 5:window.blit(MANUAL_CURSOR2, MANUAL_CURSOR2.get_rect(center = pygame.mouse.get_pos()))如果分数 >= 6:window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))如果分数 >= 7:window.blit(MANUAL_CURSOR2, MANUAL_CURSOR2.get_rect(center = pygame.mouse.get_pos()))如果分数 >= 8:window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))# 我们的主循环跑步游戏 = 真运行游戏时:时钟滴答(fps)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:跑步游戏 = 错误如果 event.type == pygame.MOUSEBUTTONDOWN:pos = pygame.mouse.get_pos()如果 greenbutton2.isOver(pos):得分 += 1cointext = font.render("" + str(score), True, (255,255,255))coinrect.center = ((100,50))对于范围内的 x(4):粒子.附加(粒子(MANUAL_CURSOR.get_rect(中心= pygame.mouse.get_pos())))重绘()pygame.display.update()pygame.quit()

我正在使用的资产请!在测试代​​码时使用 so

解决方案

类名称通常应使用 CapWords 约定.类的名称应该是 Button 而不是 button.实例变量应该是小写的.因此,典型的 Button 对象的名称应该是 button.
此外使用 pygame.Rect 对象和 collidepoint.请参阅

import pygame,randompygame.init()类按钮():def __init__(self, color, x, y, width, height, text=''):self.color = 颜色self.rect = pygame.Rect(x, y, 宽度, 高度)self.text = 文本self.over = 错误def draw(self, window, outline=None):pygame.draw.rect(窗口,self.color,self.rect)如果大纲:pygame.draw.rect(窗口,轮廓,self.rect.inflate(-4,-4),3)def isOver(self, pos):返回 self.rect.collidepoint(pos)类粒子:def __init__(self, image):self.pos = (0, 0)self.image = 图像def set_pos(self, pos):self.pos = posdef draw(self, window):window.blit(self.image, self.image.get_rect(center = self.pos))def new_particle(turn):图像 = MANUAL_CURSOR 如果转 % 2 == 0 否则 MANUAL_CURSOR2返回粒子(图像)def has_won(pg):pg =particle_grid对于范围内的 i (3):如果 pg[i][0] 和 pg[i][1] 和 pg[i][2]:如果 pg[i][0].image == pg[i][1].image == pg[i][2].image:返回 pg[i][0].image对于范围内的 j(3):如果 pg[0][j] 和 pg[1][j] 和 pg[2][j]:如果 pg[0][j].image == pg[1][j].image == pg[2][j].image:返回 pg[0][j].image如果 pg[0][0] 和 pg[1][1] 和 pg[2][2]:如果 pg[0][0].image == pg[1][1].image == pg[2][2].image:返回 pg[0][0].image如果 pg[0][2] 和 pg[1][1] 和 pg[2][0]:如果 pg[0][2].image == pg[1][1].image == pg[2][0].image:返回 pg[0][2].image返回无def grid_is_full(pg):返回所有(行中单元格的 pg 中的行单元格)定义重绘():window.fill((32, 32, 32))对于我在范围内(len(button_grid)):对于范围内的 j(len(button_grid[0])):b = button_grid[i][j]p =particle_grid[i][j]is_over = p == None 和 b.isOver(pygame.mouse.get_pos())b.draw(window, (255, 255, 255) if is_over else None)如果 p:p.draw(窗口)窗口 = pygame.display.set_mode((500,540))pygame.display.set_caption(井字游戏")每秒帧数 = 40时钟 = pygame.time.Clock()font = pygame.font.SysFont(None, 50)#MANUAL_CURSOR = pygame.image.load('nw.png').convert_alpha()#MANUAL_CURSOR2 = pygame.image.load('nOW.png').convert_alpha()大小 = (60, 60)MANUAL_CURSOR = pygame.Surface(大小)MANUAL_CURSOR.fill((127, 127, 127))pygame.draw.line(MANUAL_CURSOR, (0, 127, 255), (5, 5), (size[0]-5, size[1]-5), 3)pygame.draw.line(MANUAL_CURSOR, (0, 127, 255), (size[0]-5, 5), (5, size[1]-5), 3)MANUAL_CURSOR2 = pygame.Surface(大小)MANUAL_CURSOR2.fill((127, 127, 127))pygame.draw.circle(MANUAL_CURSOR2, (127, 0, 255), (size[0]//2, size[1]//2), size[0]//2-5, 3)分数 1 = 0分数 2 = 0button_grid = [[Button((0, 128, 0), 70+i*120, 90+j*120, 100, 100, '') for i in range(3)] for j in range(3)]particle_grid = [[范围内的 i 无(3)] 范围内的 j(3)]转 = 0粒子 = new_particle(turn)跑步游戏 = 真运行游戏时:时钟滴答(fps)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:跑步游戏 = 错误如果 event.type == pygame.MOUSEBUTTONDOWN:对于我在范围内(len(button_grid)):对于范围内的 j(len(button_grid[0])):b = button_grid[i][j]如果 b.isOver(event.pos) 和particle_grid[i][j] == None:粒子.set_pos(b.rect.center)particle_grid[i][j] = 粒子转 += 1粒子 = new_particle(turn)赢了 = has_won(particle_grid)如果赢了:如果赢了 == MANUAL_CURSOR:得分 1 += 1别的:分数 2 += 1打印(分数1,分数2)particle_grid = [[范围内的 i 无(3)] 范围内的 j(3)]如果 grid_is_full(particle_grid):particle_grid = [[范围内的 i 无(3)] 范围内的 j(3)]粒子.set_pos(pygame.mouse.get_pos())scoreText1 = font.render("Player 1 Score: " + str(score1), True, (128, 0, 0))scoreText2 = font.render("Player 2 Score: " + str(score2), True, (128, 0, 0))重绘()window.blit(scoreText1, (70, 30))window.blit(scoreText2, (70, 460))粒子绘制(窗口)pygame.display.update()pygame.quit()出口()

I am trying to make a tic tak toe game with pygame and I was wondering how would I do the logic here is what I have so far. VIDEO < I only have it when I click on the middle button it will display the player 2 x on the screen and then the image that is hovering over my mouse will turn into O for player 1 turn to go BUT my question is how would I do the logic like if player 1 gets 3 in a row or player 2 gets 3 across in a row I am really confused on this and I need someone to walk me throw it

my tic tak toe game right now CODE

import pygame,random
pygame.init()

# draw our window
window = pygame.display.set_mode((500,540),pygame.NOFRAME)
pygame.display.set_caption("Tic Tac TOE")



MANUAL_CURSOR = pygame.image.load('nw.png').convert_alpha()


MANUAL_CURSOR2 = pygame.image.load('nOW.png').convert_alpha()



bg = pygame.image.load("ticO.png")
fps = 40

clock = pygame.time.Clock()

class button():
    def __init__(self, color, x,y,width,height, text=''):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text
        self.over = False

    def draw(self,window,outline=None):
                #Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(window, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
                    
        pygame.draw.rect(window, self.color, (self.x,self.y,self.width,self.height),0)
                
        if self.text != '':
            font = pygame.font.SysFont('comicsans', 60)
            text = font.render(self.text, 1, (0,0,0))
            window.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))

    def isOver(self, pos):
                #Pos is the mouse position or a tuple of (x,y) coordinates
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True
                    
        return False

    def playSoundIfMouseIsOver(self, pos, sound):
        if self.isOver(pos):            
            if not self.over:
                beepsound.play()
                self.over = True
        else:
            self.over = False



class particle:
    def __init__(self,x):
        self.x = x
        self.partilight = pygame.image.load("noW.png")
    def draw(self,window):
        window.blit(self.partilight,(self.x))


                    
white = (250,250,250)
greenbutton2 = button((0,255,0),190,215,100,100, '')

greenbutton3 = button((0,255,0),335,215,100,100, '')

greenbutton4 = button((0,255,0),70,215,100,100, '')

greenbutton5 = button((0,255,0),70,350,100,100, '')

greenbutton6 = button((0,255,0),190,350,100,100, '')

greenbutton7 = button((0,255,0),335,350,100,100, '')


greenbutton8 = button((0,255,0),70,90,100,100, '')

greenbutton9 = button((0,255,0),190,90,100,100, '')

greenbutton10 = button((0,255,0),335,90,100,100, '')



            
font = pygame.font.Font("fos.ttf", 60)
score = 1
cointext = font.render("" + str(score), True, (255,255,255))
coinrect = cointext.get_rect()
coinrect.center = ((100,50))

particles = []


pos = pygame.mouse.get_pos()
def redraw():
    window.fill((174, 214, 241))
    window.blit(bg,(0,0))
    greenbutton2.draw(window)
    greenbutton3.draw(window)
    greenbutton4.draw(window)
    greenbutton5.draw(window)
    greenbutton6.draw(window)
    greenbutton7.draw(window)

    greenbutton8.draw(window)
    greenbutton9.draw(window)
    greenbutton10.draw(window)


    for particle in particles:
        particle.draw(window)
        
    pos = pygame.mouse.get_pos()
    if score >= 0:
        window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))
        pygame.mouse.set_visible(False)

        
    if score >= 1:
        window.blit(MANUAL_CURSOR2, MANUAL_CURSOR2.get_rect(center = pygame.mouse.get_pos()))


    if score >= 2:
        window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))


    if score >= 3:
        window.blit(MANUAL_CURSOR2, MANUAL_CURSOR2.get_rect(center = pygame.mouse.get_pos()))

        

    if score >= 4:
        window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))


    if score >= 5:
        window.blit(MANUAL_CURSOR2, MANUAL_CURSOR2.get_rect(center = pygame.mouse.get_pos()))


    if score >= 6:
        window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))



    if score >= 7:
        window.blit(MANUAL_CURSOR2, MANUAL_CURSOR2.get_rect(center = pygame.mouse.get_pos()))



    if score >= 8:
        window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))


        
# our main loop
runninggame = True
while runninggame:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type ==  pygame.QUIT:
            runninggame = False


        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            if greenbutton2.isOver(pos):
                score += 1
                cointext = font.render("" + str(score), True, (255,255,255))
                coinrect.center = ((100,50))
                for x in range(4):
                    particles.append(particle(MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos())))



                    
    redraw()


    pygame.display.update()

pygame.quit()

    



the assets I am using Please! use so when your testing out the code

解决方案

Class names should normally use the CapWords convention. The name of the class should be Button rather than button. Instance Variables should be lowercase. Hence the name of an object of the typ Button should be button.
Furthermore use a pygame.Rect object and collidepoint. See How do I detect if the mouse is hovering over a button? PyGame button class is not displaying the text or changing colour on hover.
For Instance:

class Button():
    def __init__(self, color, x, y, width, height, text=''):
        self.color = color
        self.rect = pygame.Rect(x, y, width, height)
        self.text = text
        self.over = False

    def draw(self, window, outline=None):
        pygame.draw.rect(window, self.color, self.rect)
        if outline:
            pygame.draw.rect(window, outline, self.rect.inflate(-4, -4), 3)

    def isOver(self, pos):
        return self.rect.collidepoint(pos)

Create a 3x3 grid of Buttons and an 3x3 grid for the Particle objects. The initial value of the particle grid is None:

button_grid = [[Button((0, 128, 0), 70+i*120, 90+j*120, 100, 100, '') for i in range(3)] for j in range(3)]
particle_grid = [[None for i in range(3)] for j in range(3)]

Draw the grids in redraw

def redraw():
    window.fill((32, 32, 32))

    for i in range(len(button_grid)):
        for j in range(len(button_grid[0])):
            b = button_grid[i][j]
            p = particle_grid[i][j]

            is_over = p == None and b.isOver(pygame.mouse.get_pos())
            b.draw(window, (255, 255, 255) if is_over else None)
            if p:
                p.draw(window)

The particle image ahs to be an argument of the constructor of the class Particle. Add a method to the class to set the position:

class Particle:
    def __init__(self, image):
        self.pos = (0, 0)
        self.image = image
    def set_pos(self, pos):
        self.pos = pos
    def draw(self, window):
        window.blit(self.image, self.image.get_rect(center = self.pos))

Create a method that creates a new particle dependent on the current turn:

def new_particle(turn):
    image = MANUAL_CURSOR if turn % 2 == 0 else MANUAL_CURSOR2
    return Particle(image)

Add a variable for the current turn and create an initial Particle object:

turn = 0
particle = new_particle(turn)

Set the position of the Particle object using the current mouse position and draw the object in the application loop:

runninggame = True
while runninggame:
    # [...]

    particle.set_pos(pygame.mouse.get_pos())

    redraw()
    particle.draw(window)
    pygame.display.update()

When you click a field, check that the corresponding field in the particle grid is empty. Copy the center of the field to the position of the particle. Assign the particle to the grid. Increment the turn and create a new particle:

while runninggame:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type ==  pygame.QUIT:
            runninggame = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            for i in range(len(button_grid)):
                for j in range(len(button_grid[0])):
                    b = button_grid[i][j]
                    if b.isOver(event.pos) and particle_grid[i][j] == None:
                        particle.set_pos(b.rect.center)
                        particle_grid[i][j] = particle
                        turn += 1
                        particle = new_particle(turn)

Add a score for both players:

score1 = 0
score2 = 0

Add a function that evaluates whether there are 3 identical images in a row:

def has_won(pg):
    pg = particle_grid
    for i in range(3):
        if pg[i][0] and pg[i][1] and pg[i][2]:
            if pg[i][0].image == pg[i][1].image == pg[i][2].image:
                return pg[i][0].image
    for j in range(3):
        if pg[0][j] and pg[1][j] and pg[2][j]:
            if pg[0][j].image == pg[1][j].image == pg[2][j].image:
                return pg[0][j].image
    if pg[0][0] and pg[1][1] and pg[2][2]:
        if pg[0][0].image == pg[1][1].image == pg[2][2].image:
            return pg[0][0].image
    if pg[0][2] and pg[1][1] and pg[2][0]:
        if pg[0][2].image == pg[1][1].image == pg[2][0].image:
            return pg[0][2].image
    return None

Test to see if a player has won after clicking a button. When a player wins, increase the appropriate score and reset the particle grid:

while runninggame:
    clock.tick(fps)
    for event in pygame.event.get():
        # [...]

        if event.type == pygame.MOUSEBUTTONDOWN:
            [...]

            won = has_won(particle_grid)
            if won:
                if won == MANUAL_CURSOR:
                    score1 += 1
                else:
                    score2 += 1
                print(score1, score2)
                particle_grid = [[None for i in range(3)] for j in range(3)]

Add a function that evaluates whether the grid is full:

def grid_is_full(pg):
    return all(cell for row in pg for cell in row)

Clear the grid when it's full:

while runninggame:
    clock.tick(fps)
    for event in pygame.event.get():
        # [...]

        if event.type == pygame.MOUSEBUTTONDOWN:
            [...]

            if grid_is_full(particle_grid):
                particle_grid = [[None for i in range(3)] for j in range(3)]


Minimal example:

import pygame,random
pygame.init()

class Button():
    def __init__(self, color, x, y, width, height, text=''):
        self.color = color
        self.rect = pygame.Rect(x, y, width, height)
        self.text = text
        self.over = False

    def draw(self, window, outline=None):
        pygame.draw.rect(window, self.color, self.rect)
        if outline:
            pygame.draw.rect(window, outline, self.rect.inflate(-4, -4), 3)

    def isOver(self, pos):
        return self.rect.collidepoint(pos)

class Particle:
    def __init__(self, image):
        self.pos = (0, 0)
        self.image = image
    def set_pos(self, pos):
        self.pos = pos
    def draw(self, window):
        window.blit(self.image, self.image.get_rect(center = self.pos))

def new_particle(turn):
    image = MANUAL_CURSOR if turn % 2 == 0 else MANUAL_CURSOR2
    return Particle(image)

def has_won(pg):
    pg = particle_grid
    for i in range(3):
        if pg[i][0] and pg[i][1] and pg[i][2]:
            if pg[i][0].image == pg[i][1].image == pg[i][2].image:
                return pg[i][0].image
    for j in range(3):
        if pg[0][j] and pg[1][j] and pg[2][j]:
            if pg[0][j].image == pg[1][j].image == pg[2][j].image:
                return pg[0][j].image
    if pg[0][0] and pg[1][1] and pg[2][2]:
        if pg[0][0].image == pg[1][1].image == pg[2][2].image:
            return pg[0][0].image
    if pg[0][2] and pg[1][1] and pg[2][0]:
        if pg[0][2].image == pg[1][1].image == pg[2][0].image:
            return pg[0][2].image
    return None

def grid_is_full(pg):
    return all(cell for row in pg for cell in row)

def redraw():
    window.fill((32, 32, 32))
    
    for i in range(len(button_grid)):
        for j in range(len(button_grid[0])):
            b = button_grid[i][j]
            p = particle_grid[i][j]
            
            is_over = p == None and b.isOver(pygame.mouse.get_pos())
            b.draw(window, (255, 255, 255) if is_over else None)
            if p:
                p.draw(window)
        
window = pygame.display.set_mode((500,540))
pygame.display.set_caption("Tic Tac TOE")
fps = 40
clock = pygame.time.Clock()
font =  pygame.font.SysFont(None, 50)

#MANUAL_CURSOR = pygame.image.load('nw.png').convert_alpha()
#MANUAL_CURSOR2 = pygame.image.load('nOW.png').convert_alpha()
size = (60, 60)
MANUAL_CURSOR = pygame.Surface(size)
MANUAL_CURSOR.fill((127, 127, 127))
pygame.draw.line(MANUAL_CURSOR, (0, 127, 255), (5, 5), (size[0]-5, size[1]-5), 3)
pygame.draw.line(MANUAL_CURSOR, (0, 127, 255), (size[0]-5, 5), (5, size[1]-5), 3)
MANUAL_CURSOR2 = pygame.Surface(size)
MANUAL_CURSOR2.fill((127, 127, 127))
pygame.draw.circle(MANUAL_CURSOR2, (127, 0, 255), (size[0]//2, size[1]//2), size[0]//2-5, 3)

score1 = 0
score2 = 0

button_grid = [[Button((0, 128, 0), 70+i*120, 90+j*120, 100, 100, '') for i in range(3)] for j in range(3)]
particle_grid = [[None for i in range(3)] for j in range(3)]
turn = 0
particle = new_particle(turn)
      
runninggame = True
while runninggame:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            for i in range(len(button_grid)):
                for j in range(len(button_grid[0])):
                    b = button_grid[i][j]
                    if b.isOver(event.pos) and particle_grid[i][j] == None:
                        particle.set_pos(b.rect.center)
                        particle_grid[i][j] = particle
                        turn += 1
                        particle = new_particle(turn)
        
            won = has_won(particle_grid)
            if won:
                if won == MANUAL_CURSOR:
                    score1 += 1
                else:
                    score2 += 1
                print(score1, score2)
                particle_grid = [[None for i in range(3)] for j in range(3)]

            if grid_is_full(particle_grid):
                particle_grid = [[None for i in range(3)] for j in range(3)]

    particle.set_pos(pygame.mouse.get_pos())
    scoreText1 =  font.render("Player 1   Score: " + str(score1), True, (128, 0, 0))
    scoreText2 =  font.render("Player 2   Score: " + str(score2), True, (128, 0, 0))

    redraw()
    window.blit(scoreText1, (70, 30))
    window.blit(scoreText2, (70, 460))
    particle.draw(window)
    pygame.display.update()

pygame.quit()
exit()

这篇关于Pygame 井字游戏逻辑?我该怎么做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆