0

I run the codes below. The computer always print "win" if (500 < mouse_x < 700) and (100 < mouse_y < 211) (player = 2) and "lose" if (100 < mouse_x < 300) and (100 < mouse_y < 221) (player = 1). I had checked the "player" and it works fine, but the "computer" always print value "2". If I change "if computer <= 10" to "else": "computer" always print value "1"

This is my codes:

import pygame
from random import randint
def draw_floor():
    screen.blit(floor, (floorrunning, 453))
    screen.blit(floor, (floorrunning + 800, 453))
                
pygame.init()
bg = pygame.image.load("background.png")

screen = pygame.display.set_mode((800,500))

GREY = (120, 120, 120)
WHITE = (255, 255, 255)
BLANK = (0, 0, 0)

floor = pygame.image.load("quangcaobanthan.png")
floorrunning = 0

xucxac1 = pygame.image.load("nan1.png")
xucxac2 = pygame.image.load("nan1_2.png")
xucxac3 = pygame.image.load("nan1_3.png")
xucxac1_rect = (141, 300)
xucxac2_rect = (341, 300)
xucxac3_rect = (541, 300)

youlose = pygame.image.load("youlose.png")
youlose_rect = youlose.get_rect(center = (400, 250))
youwin = pygame.image.load("youwin.png")
youwin_rect = youwin.get_rect(center = (400, 250))

running = True
replay = True

ketqua1 = randint(1,6)
ketqua2 = randint(1,6)
ketqua3 = randint(1,6)

computer = ketqua1 + ketqua2 + ketqua3

game_font = pygame.font.SysFont("04B_19.ttf", 40)
player = 0

def ketqua():
    ketqua1_infor = game_font.render(str(ketqua1), True, (0,0,0))
    ketqua1_rect = (190, 334)
    screen.blit(ketqua1_infor, ketqua1_rect)
    ketqua2_infor = game_font.render(str(ketqua2), True, (0,0,0))
    ketqua2_rect = (390, 334)
    screen.blit(ketqua2_infor, ketqua2_rect)
    ketqua3_infor = game_font.render(str(ketqua3), True, (0,0,0))
    ketqua3_rect = (590, 334)
    screen.blit(ketqua3_infor, ketqua3_rect)

while running:

    screen.blit(bg,(0,0))
    mouse_x, mouse_y = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and replay == False:
                replay = True
                xucxac1_rect = (141, 300)
                xucxac2_rect = (341, 300)
                xucxac3_rect = (541, 300)
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if (100 < mouse_x < 300) and (100 < mouse_y < 221):
                    player = 1
                if (500 < mouse_x < 700) and (100 < mouse_y < 211):
                    player = 2  

    if 10 < computer <= 18:
        computer = 1
    if computer <= 10:
        computer = 2
    
    if xucxac1_rect == (41, 300) and xucxac2_rect == (241, 300) and xucxac3_rect == (441, 300):
        if computer == player:
            print("win")
        else:
            print("lose")

    pygame.draw.rect(screen, WHITE, (100, 100, 200, 121))
    pygame.draw.rect(screen, WHITE, (500, 100, 200, 111))
    pygame.draw.rect(screen, WHITE, (277, 0, 240, 100))

    screen.blit(pygame.image.load("tai.png"), (100,100))
    screen.blit(pygame.image.load("xiu.png"), (500,100))
    screen.blit(pygame.image.load("start.png"), (275, -60))

    draw_floor()
    floorrunning -= 0.5
    if floorrunning <= -800:
        floorrunning = 0
    if replay == False:
        ketqua1 = randint(1,6)
        ketqua2 = randint(1,6)
        ketqua3 = randint(1,6)

    if replay:
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if (141 < mouse_x < 241) and (300 < mouse_y < 400):
                    xucxac1_rect = (41, 300)
                if (341 < mouse_x < 441) and (300 < mouse_y < 400):
                    xucxac2_rect = (241, 300)
                if (541 < mouse_x < 641) and (300 < mouse_y < 400):
                    xucxac3_rect = (441, 300)
                if (277 < mouse_x < 527) and (0 < mouse_y < 79):
                    replay = False
        pygame.draw.rect(screen, GREY, (50, 290, 700, 120))
        pygame.draw.circle(screen, GREY, (195, 350), 48)
        pygame.draw.circle(screen, GREY, (395, 350), 48)
        pygame.draw.circle(screen , GREY,(595, 350), 48)

        screen.blit(xucxac1, xucxac1_rect)
        screen.blit(xucxac2, xucxac2_rect)
        screen.blit(xucxac3, xucxac3_rect)
    else:
        pygame.draw.rect(screen, BLANK, (50, 290, 700, 120))


    ketqua()
    pygame.display.flip()

pygame.quit()

0 Answers0