day4-2048

2018-05-16  本文已影响0人  deeann1993

今天通过视频学习,练习2048游戏代码,熟悉pyglet库。今天主要完成了创建窗口和生成文字标签两项。

代码如下:

import pyglet
Win_width = 530
Win_height = 720
#棋盘起始位置左下角的x,y
start_x = 15
start_y = 110
#每行的块数
window_block_num =4
#总宽度和每块的高度
board_width = (Win_width -2 *start_x)
block_windth = board_width/window_block_num

label_color =(119,110,101,255)
bg_color = (250,248,239,255)

class Window(pyglet.window.Window):
    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        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]]
        #背景spirite
        background_img = 
              pyglet.image.SolidColorImagePattern(color=bg_color)
              self.background = pyglet.sprite.Sprite(
              background_img.create_image(Win_width,Win_height),0,0)
        #Title
        self.title_label = pyglet.text.Label(text = '2048',bold = True,
             color = label_color,x=start_x,y=board_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=board_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.background.draw()
        self.main_batch.draw()

创建窗口

 win = Window(Win_width,Win_height)

设置图标

icon = pyglet.image.load('icon.ico')
win.set_icon(icon)

pyglet.app.run()
上一篇下一篇

猜你喜欢

热点阅读