[PYTHON] Become Santa with Tkinter

The first year of Reiwa is about to end. The marriage rush of celebrities was conspicuous this year. The other day my best friend suddenly reported my marriage. ~~ Until recently, I was saying that Rokka-chan's legs should be thick ... ~~ When I complained on Twitter, a foreigner who didn't know comforted me. I'm glad that my study abroad experience was alive.

And the world is Christmas. Many dads and moms will be Santa. Those who got married this year should become Santa if they have children in the future.

However, after raising a child only once in life, many newlyweds may be wondering how to become Santa. For such people, I made a game where you can practice Santa's rehearsal.

So, I will write the article for the 22nd day of Make IT Advent Calendar 2019.

Introduction

I used Tkinter to create the game this time. The basic code is Python game programming class from scratch starting from 12 years old Since it is listed, I will mainly describe the added elements. The good thing about this book is that you can realize that you are under 12 years old when you get stuck in understanding. You should be young.

title screen

image.png

There is nothing wrong with the title. It's a story about a man living in Detroit becoming Santa. I'm looking forward to this year's Frostbite.

Santagame.py


 
#Title creation
frame = tkinter.Frame(width=960, height=640)
frame.place(x=10, y=10)
title = tkinter.Canvas(frame, width=960, height=640)
title.place(x=0, y=0)
title.create_rectangle(0, 0, 960, 640, fill="black")
title_label_up = tkinter.Label(frame, width=0, height=0, text=" ", font=("Arial Black", 70),                           fg="white", bg="black")
title_label_down = tkinter.Label(frame, width=0, height=0, text=" ", font=("Arial", 20),                                fg="white", bg="black")
press_any_key = tkinter.Label(frame, width=0, height=0, text=" ", font=("Arial", 25),                              fg="white", bg="black")
title_label_up["text"] = "DETROIT"
title_label_down["text"] = "B   E   C   O   M   E       S   A   N   T   A"
press_any_key["text"] = "-  Press any key to start  -"
title_label_up.place(x=240, y=220)
title_label_down.place(x=230, y=340)
press_any_key.place(x=280, y=480)

#Erase title
def forget_title(event):
    frame.destroy()

#title erase
root.bind("<Any-KeyPress>", forget_title)


Regarding the code, it is the basic form of creating Frame, Canvas and Label. Use key bindings and press any key to turn it off.

Play screen

image.png

Operate Santa with the arrow keys.

The basic rule is to get a toy and an ax and head home. You can get toys by going to a toy store with an ax or money. If you have only an ax, you will be treated as a robber and the attack power of some enemies will increase, so be careful. However, holding an ax also increases your own attack power.

The appearance of swinging an ax on the snow is just like Kamado Shrine. Do not give the right to kill or take a living. It's a game that can teach children the harshness of society, where they kill before they are killed.

Battle screen

image.png

Detroit, where violence and madness swirl, the enemy is state power and antisocial forces. Before becoming Santa, the main character was just a suspicious person in red clothes. It can't be helped if you are asked a job question. The world view has collapsed just because I was particular about free materials. But reincarnated in another world is probably like this.

Yakuza, the natural enemy of the yakuza, will get angry if they have money. Kill before you get killed. Police officers fire after the robbery. Kill before you are killed.

If you have an ax, blood stains will remain after the battle.

image.png

If you lose the battle, two types of bad end screens, Western and Japanese, will appear at random.

Functions that could not be implemented

Animated gif of Santa image

If you use gif as it is in Tkinter, only the first frame will be displayed, so you can loop it, use after (), and try and error, but it will sink. As a compromise, the image changes every time you walk.

SantaGame.py


global santa_i
canvas.create_image(santa_x * 64 + 31, santa_y * 64 + 31,
                        image=santaImages[santa_direction][santa_i], tag="santa")
santa_i += 1
if santa_i > 3:
    santa_i = 0

Playing videos in Tkinter windows

This is also used as after (), but it sinks. A compromise is to play in a separate window and close the window when finished.

SantaGame.py


fps = 250

if cap.isOpened() == False:
    print("Error!")

while cap.isOpened():

    ret, ending = cap.read()

    if ret == True:

        time.sleep(1/fps)
        cv2.imshow('ending', ending)

        if cv2.waitKey(1) & 0xFF == ord('q'):

             break

        else:
            break

cap.release()
cv2.destroyAllWindows()

Unbind key in battle

At first, I used to click the arrow button to move Santa, but it was too clumsy, so I decided to key-bind it. However, if you unbind during the battle, you cannot find the timing to bind again by the end of the battle ...

As a result of worrying about whether to take stylish (provisional) or functionality, I chose to coexist with bugs. So to speak, this bug is like Mameko. ~~ If you can't tell the difference between good and bad bugs, stop being an engineer ~~ So please don't press the arrow keys during the battle.

in conclusion

If you want to play, please download from here and start SantaGame.py.

Tomorrow @fumihumi will be in charge.

Recommended Posts

Become Santa with Tkinter
MVC with Tkinter
Programming with Python and Tkinter
Getting Started with Tkinter 2: Buttons
Working with tkinter and mouse
Screen switching / screen transition with Tkinter
Create Image Viewer with Tkinter
Let's make dice with tkinter
Run Label with tkinter [Python]
I measured BMI with tkinter
Easy GUI app with Tkinter Text
Async / await with Kivy and tkinter
[Tkinter] Control threads with Event object
[Python] Creating multiple windows with Tkinter
Play video with sound with python !! (tkinter / imageio)
Creating an image splitting app with Tkinter
Make GUI apps super easy with tkinter
Create a GUI app with Python's Tkinter
Develop Windows apps with Python 3 + Tkinter (Preparation)