简体   繁体   中英

8 Queens (pyglet-python)

I'm trying to make 8 queens game on pyglet. I have succesfully generated board.png on window. Now when I paste queen.png image on it, I want it to show only queen on it not the white part. I removed white part using photoshop, but as I call it on board.png in pyglet it again shows that white part please help.

import pyglet
from pyglet.window import Window, mouse, gl

# Display an image in the application window
image = pyglet.image.Texture.create(800,800)
board = pyglet.image.load('resources/Board.png')
queen = pyglet.image.load('resources/QUEEN.png')
image.blit_into(board,0,0,0)
image.blit_into(queen,128,0,0)

# creating a window
width = board.width
height = board.height
mygame = Window(width, height,
                resizable=False,
                caption="8 Queens",
                config=pyglet.gl.Config(double_buffer=True),
                vsync=False)

# Making list of tiles
print("Height: ", board.height, "\nWidth: ", board.width)


@mygame.event
def on_draw():
    mygame.clear()
    image.blit(0, 0)


def updated(dt):
    on_draw()


pyglet.clock.schedule_interval(updated, 1 / 60)

# Launch the application
pyglet.app.run()

These are the images:

queen.png

board.png

Your image is a rectangle. So necessarily, you will have a white space around your queen whatever you do.

I would recommend a bit of hacking (it's not very beautiful) and create two queen versions: queen_yellow and queen_black. Whenever the queen is standing on a yellow tile, display queen_yellow, and otherwise display queen_black.

To find out whether a tile is a yellow tile (using a matrix with x and y coordinates, where the top value for y is 0 and the very left value for x is 0):

if tile_y%2=0: #is it an even row?
    if tile_x%2=0: #is it an even column?
        queentype = queen_yellow
    else:
        queentype = queen_black
else: #is it an uneven row?
    if tile_x%2!=0: #is it an uneven column?
        queentype = queen_yellow
    else: queentype = queen_black

Hope that helped, Narusan

First of all, please verify that there is no background (you can use GIMP for that). Once that is done go ahead with this:

Since it is a PNG image, you can't just put it there on the window as it will lose its transparency. You need to import the PNGImageDecoder from pyglet like

from pyglet.image.codecs.png import PNGImageDecoder

then use it for loading the PNG image like

kitten = pyglet.image.load('kitten.png', decoder=PNGImageDecoder())

and finally draw it on the window by using

kitten.draw() , after specifying the x and y coordinates where you would like to have them.

The document for the above can be found here .

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM