This week I was supposed to calculate the height and width of each tile and store them to use later. As I was working on this calculation, I just got an idea why not try to give this 8 queens a little 3D look. So I changed the board and did some other changes to the code, but if you want to continue to make this simple 8 queens game you can follow my friend’s blog.
I also changed my compiler from PyCharm to Visual Studio 2015, but it doesn’t make any change in the code. So now if you are still following me, here is the board I’m going to use now. You should save this board with name “chess-boards.jpg”.
I made queen with transparent background but whenever I placed that queen on board, pyglet replaced that transparency with white pixels. So I did something, which I think is not a good programming practice. But as soon as I will know a code to remove those extra pixels I will update this post again. For now I used Photoshop (CS6) to build 64 queens, with different background. Save all of these queens in resources folder with the name given like; “q0.png” , “q1.png”…
It might look stupid right now, but once the game will be complete it will look really nice. previously I was loading images using :
pic = pyglet.image.load("FILE NAME")
But now, I’m planning to use sprite with this load command cause it let you play with the visibility of the loaded image. I’m not going to upload the code yet, cause I’m still calculating the (x, y) coordinates for each tile. Once calculated, I will place all the tiles on board using:
myimage = pyglet.sprite.Sprite(pic, x, y) myimage.visible = False
When we set the visible to False, the image is hidden vice versa for True. Here is the sample code for one queen only:
#_author_ = "Abdul Junaid" import pyglet from pyglet.window import Window, mouse, gl # Display an image in the application window board = pyglet.image.load('Resources/chess-boards.jpg') #loading board image q1 = pyglet.image.load('Resources/q0.png') #loading queen image queen = pyglet.sprite.Sprite(q1,97,72) #Seleting the image and placeing it on x, y queen.visible = False #Hidding the image # creating a window width = board.width height = board.height mygame = Window(width, height, resizable=True, caption="8 Queens", config=pyglet.gl.Config(double_buffer=True), vsync=False) @mygame.event def on_draw(): mygame.clear() #Clearing the screen board.blit(0,0) #Drawing board queen.draw() #Drawing queen on board @mygame.event def on_mouse_press(x,y,button,modifiers): #automatically initiates when mouse button pressed if button == mouse.LEFT: #checking if left mouse button was pressed if x > 97 and x < 300: #Giving the width of first tile if y > 72 and y < 120: #Giving height of first tile queen.visible = True #changing visiblity of tile def updated(dt): on_draw() pyglet.clock.schedule_interval(updated, 1 / 60) # Launch the application pyglet.app.run()