我正在尝试制作 pygame 井字游戏,但它不起作用
I am trying to make pygame tic-tac-toe but it doesn't work
我正在尝试在 pygame 中制作井字棋。当我 运行 它时,它正在工作,但是当我稍微移动鼠标时,X 图像消失了。我的代码有问题吗?
我的代码:
import pygame
import time
from pygame import surface
pygame.init()
(width,height) = (400,600)
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("PUBG M")
clock = pygame.time.Clock()
test_font = pygame.font.Font('game/cool/pixeled/pixeled.ttf', 20)
back_surface = pygame.image.load('game/cool/XO.png')
back_surface = pygame.transform.scale(back_surface, (400,600))
sccor_font = test_font.render('sccore : ',False, 'white')
f11 = pygame.draw.rect(screen, ('white'), (5,5,110,110))
f12 = pygame.draw.rect(screen, ('white'), (145,5,110,110))
f13 = pygame.draw.rect(screen, ('white'), (280,5,115,110))
f21 = pygame.draw.rect(screen, ('white'), (5,140,110,110))
f22 = pygame.draw.rect(screen, ('white'), (145,140,110,110))
f23 = pygame.draw.rect(screen, ('white'), (280,140,115,110))
f31 = pygame.draw.rect(screen, ('white'), (5,275,110,110))
f32 = pygame.draw.rect(screen, ('white'), (145,275,110,110))
f33 = pygame.draw.rect(screen, ('white'), (280,275,115,110))
running = True
while running:
screen.blit(back_surface,(0,0))
screen.blit(sccor_font,(20,450))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
if f11.collidepoint(pos):
xf11 = pygame.image.load('game/cool/XX.png')
xf11 = pygame.transform.scale(xf11, (100,100))
screen.blit(xf11,(f11))
if f12.collidepoint(pos):
print("rrrrrrr")
if f13.collidepoint(pos):
print("sssssss")
pygame.display.update()
clock.tick(30)
我不确定我做错了什么。
你必须每帧重绘整个场景。将点击的位置添加到一个列表中,在应用循环中在保存在列表中的位置绘制图像。
管理列表中的9个字段,并循环检查其中一个字段是否被点击
import pygame
pygame.init()
(width,height) = (400,600)
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("PUBG M")
clock = pygame.time.Clock()
test_font = pygame.font.SysFont('game/cool/pixeled/pixeled.ttf', 20)
back_surface = pygame.image.load('game/cool/XO.png')
back_surface = pygame.transform.scale(back_surface, (400,600))
xf11 = pygame.image.load('game/cool/XX.png')
xf11 = pygame.transform.scale(xf11, (100,100))
fields = [pygame.Rect(5+140*x, 5+135*y, 110, 110) for x in range(3) for y in range(3)]
xf11_list = []
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONUP:
for rect in fields:
if rect.collidepoint(event.pos):
xf11_list.append(rect)
screen.blit(back_surface,(0,0))
screen.blit(sccor_font,(20,450))
for rect in fields:
pygame.draw.rect(screen, 'white', rect)
for pos in xf11_list:
screen.blit(xf11, pos)
pygame.display.update()
clock.tick(30)
pygame.quit()
exit()
我正在尝试在 pygame 中制作井字棋。当我 运行 它时,它正在工作,但是当我稍微移动鼠标时,X 图像消失了。我的代码有问题吗?
我的代码:
import pygame
import time
from pygame import surface
pygame.init()
(width,height) = (400,600)
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("PUBG M")
clock = pygame.time.Clock()
test_font = pygame.font.Font('game/cool/pixeled/pixeled.ttf', 20)
back_surface = pygame.image.load('game/cool/XO.png')
back_surface = pygame.transform.scale(back_surface, (400,600))
sccor_font = test_font.render('sccore : ',False, 'white')
f11 = pygame.draw.rect(screen, ('white'), (5,5,110,110))
f12 = pygame.draw.rect(screen, ('white'), (145,5,110,110))
f13 = pygame.draw.rect(screen, ('white'), (280,5,115,110))
f21 = pygame.draw.rect(screen, ('white'), (5,140,110,110))
f22 = pygame.draw.rect(screen, ('white'), (145,140,110,110))
f23 = pygame.draw.rect(screen, ('white'), (280,140,115,110))
f31 = pygame.draw.rect(screen, ('white'), (5,275,110,110))
f32 = pygame.draw.rect(screen, ('white'), (145,275,110,110))
f33 = pygame.draw.rect(screen, ('white'), (280,275,115,110))
running = True
while running:
screen.blit(back_surface,(0,0))
screen.blit(sccor_font,(20,450))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
if f11.collidepoint(pos):
xf11 = pygame.image.load('game/cool/XX.png')
xf11 = pygame.transform.scale(xf11, (100,100))
screen.blit(xf11,(f11))
if f12.collidepoint(pos):
print("rrrrrrr")
if f13.collidepoint(pos):
print("sssssss")
pygame.display.update()
clock.tick(30)
我不确定我做错了什么。
你必须每帧重绘整个场景。将点击的位置添加到一个列表中,在应用循环中在保存在列表中的位置绘制图像。
管理列表中的9个字段,并循环检查其中一个字段是否被点击
import pygame
pygame.init()
(width,height) = (400,600)
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("PUBG M")
clock = pygame.time.Clock()
test_font = pygame.font.SysFont('game/cool/pixeled/pixeled.ttf', 20)
back_surface = pygame.image.load('game/cool/XO.png')
back_surface = pygame.transform.scale(back_surface, (400,600))
xf11 = pygame.image.load('game/cool/XX.png')
xf11 = pygame.transform.scale(xf11, (100,100))
fields = [pygame.Rect(5+140*x, 5+135*y, 110, 110) for x in range(3) for y in range(3)]
xf11_list = []
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONUP:
for rect in fields:
if rect.collidepoint(event.pos):
xf11_list.append(rect)
screen.blit(back_surface,(0,0))
screen.blit(sccor_font,(20,450))
for rect in fields:
pygame.draw.rect(screen, 'white', rect)
for pos in xf11_list:
screen.blit(xf11, pos)
pygame.display.update()
clock.tick(30)
pygame.quit()
exit()