赞
踩
#!/usr/bin/python # -*- coding: UTF-8 -*- import pyglet # 设置棋盘大小 width = 530 heigh = 720 # 棋盘起始位置左下角的x、y start_x = 15 start_y = 110 # 每行的块数 num = 4 #总宽度和每块的宽度 all_width = width - 2*start_x dan_width = all_width/num # 数字和背景设置不同颜色 label_color = (119,110,101,255) bc_color = (250,248,239,255) class Window(pyglet.window.Window): # 构造可视化界面,首先要实例化出一个窗体对象 def __int__(self,*args,**kwargs): super().__init__(*args,**kwargs) #在类的继承中,如果你想要重写父类的方法而不是覆盖的父类方法,这个时候我们可以使用super()方法来实现 self.game_init() def game_init(self): self.main_batch = pyglet.graphics.Batch() self.data = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] #背景 back_img = pyglet.image.SolidColorImagePattern(color=bc_color) self.back = pyglet.sprite.Sprite( back_img.create_image(width,heigh), 0,0) #title self.title_label = pyglet.text.Label(text='2048', bold = True, color = label_color, x = start_x, y= all_width+start_y-30, font_size=36, batch=self.main_batch) # score self.score = 0 self.score_label = pyglet.text.Label(text='score = %d'%(self.score), bold = True, color = label_color, x = 200, y= all_width+start_y-30, font_size=36, batch=self.main_batch) # help self.help_label = pyglet.text.Label(text='please use up,down,->,<-,to play!', bold = True, color = label_color, x = start_x, y= start_y-30, font_size=18, batch=self.main_batch) def on_draw(self): self.clear() self.score_label.text = 'score = %d'%(self.score) self.back.draw() self.main_batch.draw() win = Window(width,heigh) icon = pyglet.image.load('icon.ico') win.set_icon(icon) pyglet.app.run()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。