import cocos
from cocos.scene import Scene
from cocos.director import director
from cocos.menu import *
from pyglet.gl import *
import random
import pyglet
list1=[(-1,-1, 0)]
qizi= []
class OptionMenu(Menu):
def __init__(self):
super().__init__()
self.font_item["font_name"] = "kaiti"
self.font_item["font_size"] = 32
self.font_item["color"] = (160, 16, 0, 255)
self.font_item_selected["font_name"] = "kaiti"
self.font_item_selected["font_size"] = 40
self.font_item_selected["color"] = (0, 128, 0, 255)
items = []
items.append(MenuItem("悔棋", self.on_huiqi))
items.append(MenuItem("帮助", self.on_help))
self.create_menu(items, shake(), shake_back(),
layout_strategy=fixedPositionMenuLayout([(760, 240), (760, 300)]))
def on_huiqi(self):
bo = qizi[-1]
GameLayer.remove(bo)
del list1[-1]
bo = qizi[-1]
GameLayer.remove(bo)
del list1[-1]
def on_help(self):
pass
class GameLayer(cocos.layer.ColorLayer):
global list1, qizi
def __init__(self):
super().__init__(22, 142, 202, 128)
cocos.layer.Layer.is_event_handler = True
bg = cocos.sprite.Sprite('bc55.jpg')
bg.position = (350, 350)
self.add(bg)
def on_mouse_press(self, x, y, button, modifiers):
if button == pyglet.window.mouse.LEFT:
x = round((x - 35) / 45)
y = round((y - 35) / 45)
if (x, y, 1) in list1:
return None
elif (x, y, 2) in list1:
return None
elif x < 0 or y < 0 or x > 14 or y > 14:
return None
blk0 = cocos.sprite.Sprite('3.png')
blk0.position = (x * 45 + 35, y * 45 + 35)
self.add(blk0)
qizi.append(blk0)
list1.append((x, y, 1))
self.computer_move()
def computer_move(self):
x = random.randint(0,14)
y = random.randint(0, 14)
if (x, y, 1) in list1:
return None
elif (x, y, 2) in list1:
return None
wht0 = cocos.sprite.Sprite('4.png')
wht0.position = (x * 45 + 35, y * 45 + 35)
self.add(wht0)
qizi.append(wht0)
list1.append((x, y, 2))
wht0.do(cocos.actions.Blink(2, 2))
def create_game_scene():
scene = Scene()
scene.add(GameLayer(), z=1)
scene.add(OptionMenu(), z=2)
return scene
def main():
director.init(850, 700,resizable=True, caption="五子棋")
director.run(create_game_scene())
if __name__ == "__main__":
main()