ひよこクリッカー

scratchで人気のクリッカーゲームですね。クリックするたびにひよこのクローンが誕生しています。scratchのクローンブロックを空っぽのリストとappend()、for文を使って上手に再現できていますね。次はscratchで作っていたジャガイモクリッカーをPythonで再現してみてください。プログラミングスキルがさらに上がると思います。


Pythonで記述したコード
import tkinter
import random
import pygame

cx = 50
cy = 500
n = 0
a = 0
c = 0
score=0

ひよこ追加 = []

pygame.init()

def 前進(e):
    global cx, ひよこ追加, n, a,score
    cx=0
    ひよこ追加.append(canvas.create_image(cx, cy, image=ひよこ1, tag="ひよこ"+str(n)))
    n = n + 1
    score=score+1

def main():
    global cx, cy, n, a, c,score
    c = c + 1
    canvas.delete("score")
    canvas.create_text(150,30,text="ひよこの数:"+str(score),tag="score",font=("メイリオ",30,"bold"),fill="orange")

    for i in ひよこ追加:
        a = random.randint(-20, 20)
        canvas.move(i, 10, a)
    cx += 30

    if cx > 600:
        cx = 0

    # コスチュームの変更
    for i in ひよこ追加:
        canvas.itemconfig(i, image=ひよこリスト[c%3])

    root.after(200, main)

root = tkinter.Tk()
root.title("ひよこの行進")
root.geometry("800x600")
root.resizable(False, False)
root.bind("", 前進)

背景 = tkinter.PhotoImage(file="image\背景.png")
ひよこ1 = tkinter.PhotoImage(file="image\ひよこ1.png")
ひよこ2 = tkinter.PhotoImage(file="image\ひよこ2.png")
ひよこ3 = tkinter.PhotoImage(file="image\ひよこ3.png")
ひよこリスト=[ひよこ1, ひよこ2, ひよこ3]

BGM=pygame.mixer.Sound("sound\BGM.wav")
BGM.play(-1)
canvas = tkinter.Canvas(root, width=800, height=600)
canvas.pack()
canvas.create_image(400, 300, image=背景)

main()

root.mainloop()