3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Pygame】Pythonでオセロを作ってみた

Posted at

Pygameでオセロを作ってみた

 当初外に出すこと考えてなかったんで申し訳ないんですが、めちゃくちゃ見にくいです……。

othello.py
import pygame
from pygame.locals import *
import sys

class App():
    def __init__(self):
        self.p=50
        self.r=self.p//3
        self.screen=pygame.display.set_mode((self.p*10,self.p*10))
        pygame.display.set_caption("オセロ")
        self.clock=pygame.time.Clock()
        self.mp=[[-1 for i in range(8)] for i in range(8)]
        self.mp[3][3]=1
        self.mp[3][4]=0
        self.mp[4][3]=0
        self.mp[4][4]=1
        self.fps=15
        self.player=0
        self.font=pygame.font.SysFont("ヒラキノ角コシックw5",20)
        self.f_s=pygame.font.SysFont("ヒラキノ角コシックw8",30)
        self.running=True
        self.text3=None
        self.run()
        
    def run(self):
        while self.running:
            self.clock.tick(self.fps)
            for event in pygame.event.get():
                if event.type==QUIT:
                    self.running=False
                    pygame.quit()
                    sys.exit()
                elif event.type==KEYDOWN:
                    if pygame.key.name(event.key)=="space" and self.text3==None:
                        self.player=(self.player+1)%2
                elif event.type==MOUSEBUTTONDOWN:
                    btn=event.button
                    x, y=event.pos
                    if 50<=x and x<=450 and 50<=y and y<=450 and btn==1 and self.text3==None:
                        x-=50
                        y-=50
                        x//=50
                        y//=50
                        if self.mp[y][x]==-1:
                            en=(self.player+1)%2
                            pxpy=[0,0,0,0,0,0,0,0]#0:→ 1:↘ 2:↑ 3:↗ 4:← 5:↖︎ 6:↓ 7:↙
                            for fy in range(-1,2):
                                for fx in range(-1,2):
                                    if y+fy>=0 and y+fy<=7 and x+fx>=0 and x+fx<=7:
                                        if self.mp[y+fy][x+fx]==en:
                                            if fx==0:
                                                if fy<0:pxpy[2]=fy
                                                else:pxpy[6]=fy
                                            elif fy==0:
                                                if fx>0:pxpy[0]=fx
                                                else:pxpy[4]=fx
                                            elif fy==-fx:
                                                if fx>0:pxpy[3]=fx
                                                else:pxpy[7]=fx
                                            elif fy==fx:
                                                if fx>0:pxpy[1]=fx
                                                else:pxpy[5]=fx
                            en_t=0
                            for i in pxpy:
                                if i!=0:en_t=1
                            if en_t==1:
                                test=0
                                for i in range(8):
                                    pl=0
                                    if pxpy[i]!=0:
                                        buf=pxpy[i]
                                        if i>=4:buf=-buf
                                        h=pxpy[i]//buf
                                        yy=y
                                        xx=x
                                        if i==0 or i==4:
                                            xx+=h
                                        elif i==1 or i==5:
                                            xx+=h
                                            yy+=h
                                        elif i==2 or i==6:
                                            yy-=h
                                        else:
                                            xx+=h
                                            yy-=h
                                        renzoku=0
                                        while xx>=0 and xx<=7 and yy>=0 and yy<=7:
                                            if self.mp[yy][xx]==-1:
                                                break
                                            elif self.mp[yy][xx]==en:
                                                renzoku+=1
                                            elif self.mp[yy][xx]==self.player:
                                                pl=1
                                                test=1
                                                break
                                            if i==0 or i==4:
                                                xx+=h
                                            elif i==1 or i==5:
                                                xx+=h
                                                yy+=h
                                            elif i==2 or i==6:
                                                yy-=h
                                            else:
                                                xx+=h
                                                yy-=h
                                        renzoku*=pl
                                        pxpy[i]*=renzoku
                                if test==1:
                                    self.mp[y][x]=self.player
                                    for i in range(8):
                                        buf=pxpy[i]
                                        if buf<0:buf=-buf
                                        try:h=pxpy[i]//buf
                                        except:pass
                                        vy=y
                                        vx=x
                                        for j in range(1,buf+1):
                                            if i==0 or i==4:
                                                vx+=h
                                            elif i==1 or i==5:
                                                vx+=h
                                                vy+=h
                                            elif i==2 or i==6:
                                                vy+=h
                                            elif i==3 or i==7:
                                                vx+=h
                                                vy-=h
                                            self.mp[vy][vx]=self.player
                                    zero=0
                                    res=[0,0]
                                    for i in self.mp:
                                        for j in i:
                                            if j==-1:zero=1
                                            elif j==0:res[0]+=1
                                            else:res[1]+=1
                                    if zero==1:self.player=en
                                    else:
                                        if res[0]>res[1]:
                                            text="プレイヤー1(黒)の勝ち"
                                        elif res[1]>res[0]:
                                            text="プレイヤー2(白)の勝ち"
                                        else:
                                            text="引き分け"
                                        self.text3=self.f_s.render(text,True,(255,0,0))
                                        self.text4=self.f_s.render("%d"%(res[0]),True,(0,200,0),(0,0,0))
                                        self.text5=self.font.render("VS",True,(255,0,0))
                                        self.text6=self.f_s.render("%d"%(res[1]),True,(0,200,0),(255,255,255))
            cl=self.player*255
            if self.player==0:
                cl_s=""
            else:cl_s=""
            self.text1=self.font.render("プレイヤー%d("%(self.player+1)+cl_s+")の番です。",True,(cl,cl,cl))
            self.text2=self.font.render("左クリック:石を置く  スペース:パス",True,(0,0,0))
            if self.text3!=None:
                self.screen.blit(self.text3,(100,230))
                self.screen.blit(self.text4,(190,170))
                self.screen.blit(self.text5,(240,180))
                self.screen.blit(self.text6,(275,170))
            self.screen.blit(self.text1,(50,460))
            self.screen.blit(self.text2,(50,10))
            pygame.display.update()
            self.screen.fill((0,200,0))
            for i in range(1,8):
                pygame.draw.line(self.screen,(0,0,0),(self.p*(i+1),self.p),(self.p*(i+1),self.p*9))
                pygame.draw.line(self.screen,(0,0,0),(self.p,self.p*(i+1)),(self.p*9,self.p*(i+1)))
            pygame.draw.rect(self.screen,(0,0,0),Rect(self.p,self.p,self.p*8,self.p*8),1)
            for i in range(0,8):
                for j in range(0,8):
                    if self.mp[i][j]!=-1:
                        cl=255*self.mp[i][j]
                        pygame.draw.circle(self.screen,(cl,cl,cl),((j+2)*self.p-self.p//2,(i+2)*self.p-self.p//2),self.r,0)

if __name__=="__main__":
    pygame.init()
    main=App()

雑記

 友人にこれを見せたら褒められたので投稿してみっか〜と投稿してみましたが、これ本当に見づらいですね。Pygame歴はかなり浅いので非効率なところだらけだとは思いますが、参考になれば幸いです。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?