python 오목 미니 게임 실현

9466 단어 python오목
본 논문 의 사례 는 python 이 오목 게임 을 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
여름 방학 에 python 을 십 여 일 배 운 후에 pygame 모듈 로 오목 의 작은 게임 을 썼 습 니 다.코드 는 인연 이 있 는 사람과 공유 합 니 다.

import numpy as np
import pygame
import sys
import traceback
import copy
from pygame.locals import *


pygame.init()
pygame.mixer.init()

#  
background=(201,202,187)
checkerboard=(80,80,80)
button=(52,53,44)



#  
play_chess_sound = pygame.mixer.Sound("music/play_chess.wav")
play_chess_sound.set_volume(0.2)
button_sound = pygame.mixer.Sound("music/button.wav")
button_sound.set_volume(0.2)
victor_sound = pygame.mixer.Sound("music/victory.wav")
victor_sound.set_volume(0.2)

#    
def Draw_a_chessboard(screen): 
 #     
 screen.fill(background)
 Background=pygame.image.load("background.jpg").convert_alpha()
 screen.blit(Background,(0,0))
 #   
 for i in range(21):
 pygame.draw.line(screen, checkerboard, (40*i+3, 3), (40*i+3, 803)) 
 pygame.draw.line(screen, checkerboard, (3, 40*i+3), (803, 40*i+3))
 #   
 pygame.draw.line(screen, checkerboard, (3, 3), (803, 3),5) 
 pygame.draw.line(screen, checkerboard, (3, 3), (3, 803),5) 
 pygame.draw.line(screen, checkerboard, (803, 3), (803, 803),5) 
 pygame.draw.line(screen, checkerboard, (3, 803), (803, 803),5) 
 
 #    
 pygame.draw.circle(screen, checkerboard, (163, 163), 6) 
 pygame.draw.circle(screen, checkerboard, (163, 643), 6) 
 pygame.draw.circle(screen, checkerboard, (643, 163), 6) 
 pygame.draw.circle(screen, checkerboard, (643, 643), 6) 
 pygame.draw.circle(screen, checkerboard, (403, 403), 6) 
 
 # ‘  '‘    ' ‘  '  
 pygame.draw.rect(screen,button,[900,350,120,100],5)
 pygame.draw.rect(screen,button,[900,500,200,100],5)
 pygame.draw.rect(screen,button,[900,650,200,100],5)
 s_font=pygame.font.Font('font.ttf',40)
 text1=s_font.render("  ",True,button)
 text2=s_font.render("    ",True,button)
 text3=s_font.render("    ",True,button)
 screen.blit(text1,(920,370))
 screen.blit(text2,(920,520))
 screen.blit(text3,(920,670))

#    (   ,   ,  ,    (1   ,2   ))
def Draw_a_chessman(x,y,screen,color): 
 if color==1: 
 Black_chess=pygame.image.load("Black_chess.png").convert_alpha()
 screen.blit(Black_chess,(40*x+3-15,40*y+3-15))
 if color==2:
 White_chess=pygame.image.load("White_chess.png").convert_alpha()
 screen.blit(White_chess,(40*x+3-15,40*y+3-15))

#         
def Draw_a_chessboard_with_chessman(map,screen): 
 screen.fill(background)
 Draw_a_chessboard(screen)
 for i in range(24):
 for j in range(24):
 Draw_a_chessman(i+1,j+1,screen,map[i][j])



#         ,
#   24 24                   19
#               
map=[]
for i in range(24):
 map.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])

#  map  
def clear():
 global map
 for i in range(24):
 for j in range(24):
 map[i][j]=0

#      
def win(i, j):
 k = map[i][j]
 p=[]
 for a in range(20):
 p.append(0)
 for i3 in range(i-4,i+5):
 for j3 in range(j-4,j+5):
 if (map[i3][j3] == k and i3 - i == j3 - j and i3 <= i and j3 <= j):
 p[0]+=1
 if (map[i3][j3] == k and j3 == j and i3 <= i and j3 <= j):
 p[1]+=1
 if (map[i3][j3] == k and i3 == i and i3 <= i and j3 <= j):
 p[2]+=1
 if (map[i3][j3] == k and i3 - i == j3 - j and i3 >= i and j3 >= j):
 p[3]+=1
 if (map[i3][j3] == k and j3 == j and i3 >= i and j3 >= j):
 p[4]+=1
 if (map[i3][j3] == k and i3 == i and i3 >= i and j3 >= j):
 p[5]+=1
 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i and j3 >= j):
 p[6]+=1
 if (map[i3][j3] == k and i3 - i == j - j3 and i3 >= i and j3 <= j):
 p[7]+=1
 if (map[i3][j3] == k and j - j3 == i - i3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
 p[8]+=1
 if (map[i3][j3] == k and j == j3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
 p[9]+=1
 if (map[i3][j3] == k and i == i3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
 p[10]+=1
 if (map[i3][j3] == k and j - j3 == i - i3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
 p[11]+=1
 if (map[i3][j3] == k and j == j3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
 p[12]+=1
 if (map[i3][j3] == k and i == i3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
 p[13]+=1
 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i + 1 and i3 >= i - 3 and j3 >= j - 1 and j3 <= j + 3):
 p[14]+=1
 if (map[i3][j3] == k and i3 - i == j - j3 and i3 >= i - 1 and i3 <= i + 3 and j3 <= j + 1 and j3 >= j - 3):
 p[15]+=1
 if (map[i3][j3] == k and j - j3 == i - i3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
 p[16]+=1
 if (map[i3][j3] == k and j == j3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
 p[17]+=1
 if (map[i3][j3] == k and i == i3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
 p[18]+=1
 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
 p[19]+=1
 for b in range(20):
 if p[b]==5:
 return True
 return False

#     (  ,  ,   )
def text(s,screen,x):
 #               
 pygame.draw.rect(screen,background,[850,100,1200,100])
 #       
 s_font=pygame.font.Font('font.ttf',x)
 #    ,     ,  
 s_text=s_font.render(s,True,button)
 #          
 screen.blit(s_text,(880,100))
 pygame.display.flip()

#      
t=True

#           
running=True

#   
def main():
 #  t,map,running      
 global t,map,running,maps,r,h
 # map  
 clear()
 #             (    )
 map2=copy.deepcopy(map)
 maps=[map2]

 
 #    
 screen = pygame.display.set_mode([1200,806])
 
 #      
 pygame.display.set_caption("   ")
 
 #       ,       
 Draw_a_chessboard(screen)
 pygame.display.flip()
 clock=pygame.time.Clock()
 while True:
 #  running      ,               
 if running:
 if t:
 color=1
 text('    ',screen,54)
 else:
 color=2
 text('    ',screen,54)
 
 for event in pygame.event.get():
 #  x     
 if event.type ==pygame.QUIT:
 pygame.quit()
 sys.exit()
 
 #               
 elif event.type == MOUSEBUTTONDOWN:
 if event.button == 1:
 x,y=event.pos[0],event.pos[1]
 for i in range(19):
 for j in range(19):
 #        
 if i*40+3+20<x<i*40+3+60 and j*40+3+20<y<j*40+3+60 and not map[i][j] and running:
 #              
 Draw_a_chessman(i+1,j+1,screen,color)
 #    
 play_chess_sound.play(0)
 # map        
 map[i][j]=color

 # map  maps
 map3=copy.deepcopy(map)
 maps.append(map3)

 #            
 if win(i,j):
  if t:
  text('    ,     ',screen,30)
  else:
  text('    ,     ',screen,30)
  #    
  victor_sound.play(0)
  #        
  running=False
 pygame.display.flip()
 t=not t
 #    ‘    '
 if 900<x<1100 and 500<y<600:
 #    
 running=True
 #    
 button_sound.play(0)
 #    
 main()
 
 #  ‘    ',    
 elif 900<x<1100 and 650<y<750:
 #    
 button_sound.play(0)
 pygame.quit()
 sys.exit()
 
 #  ‘  '
 elif 900<x<1020 and 350<y<450 and len(maps)!=1:
 #    
 button_sound.play(0)
 #  maps       
 del maps[len(maps)-1] 
 #        copy map
 map=copy.deepcopy(maps[len(maps)-1])
 #    
 t=not t
 # map    
 Draw_a_chessboard_with_chessman(map,screen)
 #    ,      
 x,y=0,0
 clock.tick(60)
if __name__ == "__main__":
 try:
 main()
 except SystemExit:
 pass
 except:
 traceback.print_exc()
 pygame.quit()
 input()
실현 효과 도 는 다음 과 같다.

python 게임 에 관 한 더 많은 멋 진 글 은 다음 주 제 를 보 려 면 누 르 십시오.
python 러시아 블록 게임 집합
python 클래식 게임 모음
python 위 챗 점프 게임 집합
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기