フルーツキャッチゲーム

フルーツキャッチゲーム(中1女子/作)


講師から一言
二次元リストを活用してデータを管理している所がいいですね!

以下のコードでネコが自動でフルーツを拾いに行く点がよく工夫されています。

def move_cat():
global cat_x,cat_d,器_x
if cat_x < fruit_x: cat_d=1 cat_x += cat_d

move_cat()関数でcat_x += cat_dとすることでネコが1歩づつフルーツを拾いにいくようになっています。

cat_x =fruit_xとすると一瞬でフルーツの所に移動してしまいます。

プログラミングにはこのような工夫が大切です。


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

#変数定義
cursor_x = 0 #カーソルのx座標
cursor_y = 0 #カーソルのy座標
mouse_x = 0 #マウスのx座標
mouse_y = 0 #マウスのy座標
mouse_c = 0 #マウスクリックを管理する変数
cat_x=10 #ネコのx座標
cat_y=550 #ネコのy座標
fruit_x=10 #落下したフルーツのx座標
cat_d=1 #ネコの進む方向
cat_c =0 #ネコのコスチューム
器_x=10 #器のx座標
score=0 #スコアの管理

#フルーツを配置するためのリスト⇒二次元リストの形で使用する
fruit=[]
#二次元リストの作成
for i in range(15):
    fruit.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])

#背景画像に方眼紙を表示する関数※完成後にコメントアウトする関数
def 方眼紙():
    for y in range(15):
        for x in range(20):
            cvs.create_rectangle(x*40,y*40,x*40+40,y*40+40)

#マウスポインタの座標を取得する関数
def mouse_move(e):
    global mouse_x , mouse_y
    mouse_x = e.x
    mouse_y = e.y

#マウスのクリックを取得する関数
def mouse_press(e):
    global mouse_c
    mouse_c = 1
    落下音.play()

#フルーツを描画する関数
def draw_fruit():
    for y in range(15):
        for x in range(20):
            if fruit[y][x]==1:#2次元リストが1の所にフルーツを描画する
                cvs.create_image(x*40+20,y*40+20,image=フルーツ[0],tag="fruit")
            if fruit[y][x]==2:#2次元リストが1の所にリンゴを描画する
                cvs.create_image(x*40+20,y*40+20,image=フルーツ[1],tag="fruit")
            if fruit[y][x]==3:#2次元リストが1の所にバナナを描画する
                cvs.create_image(x*40+20,y*40+20,image=フルーツ[2],tag="fruit")
            if fruit[y][x]==4:#2次元リストが1の所にスイカを描画する
                cvs.create_image(x*40+20,y*40+20,image=フルーツ[3],tag="fruit")
            if fruit[y][x]==5:#2次元リストが1の所にイチゴを描画する
                cvs.create_image(x*40+20,y*40+20,image=フルーツ[4],tag="fruit")

#フルーツを落下させる関数
def drop_fruit():
    for y in range(13,-1,-1):
        for x in range(20):
            if (fruit[y][x] == 1 and fruit[y+1][x]==0)or(fruit[y][x] == 2 and fruit[y+1][x]==0)or(fruit[y][x] ==3 and fruit[y+1][x]==0)or(fruit[y][x] ==4 and fruit[y+1][x]==0)or(fruit[y][x] ==5 and fruit[y+1][x]==0):
                fruit[y+1][x] = fruit[y][x]
                fruit[y][x] = 0

#フルーツを探す関数
def search_fruit():
    global fruit_x
    for x in range(20):
        if fruit[14][x] == 1 or fruit[14][x] == 2 or fruit[14][x] == 3 or fruit[14][x] == 4 or fruit[14][x] == 5 or fruit[5][x] == 1 or fruit[5][x] == 2 or fruit[5][x] == 3 or fruit[5][x] == 4 or fruit[5][x] == 5:
            fruit_x = x  # 落下したフルーツの位置に向かう
            break

#フルーツの位置にネコを移動させる関数
def move_cat():
    global cat_x,cat_d,器_x
    if cat_x < fruit_x: #ネコのいる場所とフルーツが落ちた場所を比較する
        cat_d=1
        cat_x += cat_d 
        器_x=random.randint(-20,20)
    elif cat_x > fruit_x: #ネコのいる場所とフルーツが落ちた場所を比較する
        cat_d=-1
        cat_x += cat_d
        器_x=random.randint(-20,20)
    pickup_fruit()#フルーツを拾う関数を呼び出す

#フルーツを拾う関数
def pickup_fruit():
    global score
    if  fruit[14][cat_x] == 1:
        fruit[14][cat_x] = 0
        score+=10
        キャッチ音.play()
    if  fruit[14][cat_x] == 2:
        fruit[14][cat_x] = 0
        score+=10
        キャッチ音.play()
    if  fruit[14][cat_x] == 3:
        fruit[14][cat_x] = 0
        score+=10
        キャッチ音.play()
    if  fruit[14][cat_x] == 4:
        fruit[14][cat_x] = 0
        score+=10
        キャッチ音.play()
    if  fruit[14][cat_x] == 5:
        fruit[14][cat_x] = 0
        score+=10
        キャッチ音.play()


#ネコの描画
def draw_cat():
    global cat_c
    cat_c +=1
    if cat_d>0:#ネコのいる方向(direction)がプラスのときは左向きのネコの画像を使う
        cvs.create_image(cat_x*40+20, cat_y, image=cat_right[cat_c%2], tag="cat")  
    if cat_d<0:#ネコのいる方向(direction)がマイナスのときは左向きのネコの画像を使う
        cvs.create_image(cat_x*40+20, cat_y, image=cat_left[cat_c%2], tag="cat")  
    cvs.create_image(cat_x*40+20+器_x, cat_y+25, image=器, tag="cat")

def count_score():
    global score
    cvs.delete("score")
    cvs.create_text(100,20,text="スコア  "+str(score),font=("system",30,"bold"),tag="score",fill="orange")

#繰り返し
def game_main():
    global cursor_x,cursor_y,mouse_c
    if 0 < mouse_x <800 and  0 < mouse_y < 600:
        cursor_x = int(mouse_x/40)
        cursor_y = int(mouse_y/40)
        if mouse_c ==1:#マウスをクリックした時の動作
            mouse_c = 0
            fruit[cursor_y][cursor_x] = random.randint(1,5)#二次元リスト(フルーツ)に1を代入する
            
    #画像の削除
    cvs.delete("cursor")
    cvs.delete("fruit")
    cvs.delete("cat")
    #カーソルの描画
    cvs.create_image(cursor_x*40+20,cursor_y*40+20,image = カーソル,tag ="cursor")
    
    #ネコの描画
    draw_cat()
    
    #フルーツの落下
    drop_fruit()
    
    #フルーツの描画
    draw_fruit()
    
    #フルーツを探す
    search_fruit()
    
    #フルーツの場所に移動
    move_cat()

    #スコアの表示
    count_score()
    
    
    root.after(100,game_main)



#ウィンドウの作成
root=tkinter.Tk()
#タイトル作成
root.title("フルーツキャッチ")
#ウィンドウの固定
root.resizable(False,False)

root.bind("<Motion>",mouse_move)
root.bind("<ButtonPress>",mouse_press)

#画像ファイルのアップロード
背景 = tkinter.PhotoImage(file=r"image\背景.png")
カーソル=tkinter.PhotoImage(file=r"image\カーソル.png")
オレンジ=tkinter.PhotoImage(file=r"image\オレンジ.png")
リンゴ=tkinter.PhotoImage(file=r"image\リンゴ.png")
バナナ=tkinter.PhotoImage(file=r"image\バナナ.png")
スイカ=tkinter.PhotoImage(file=r"image\スイカ.png")
イチゴ=tkinter.PhotoImage(file=r"image\イチゴ.png")
フルーツ=[オレンジ,リンゴ,バナナ,スイカ,イチゴ]
cat1=tkinter.PhotoImage(file=r"image\ネコ1.png")
cat2=tkinter.PhotoImage(file=r"image\ネコ2.png")
cat_right=[cat1,cat2]
cat3=tkinter.PhotoImage(file=r"image\ネコ3.png")
cat4=tkinter.PhotoImage(file=r"image\ネコ4.png")
cat_left=[cat3,cat4]
器=tkinter.PhotoImage(file=r"image\器.png")

#キャンバスの作成
cvs=tkinter.Canvas(root,width=800,height=600)
#キャンバスの配置
cvs.pack()
#背景画像の描画
cvs.create_image(400,300,image=背景)

#音の設定
 #pygameの初期化
pygame.init()
 #音声ファイルのアップロード
BGM=pygame.mixer.Sound("sound\BGM.wav")
落下音=pygame.mixer.Sound("sound\落下音.wav")
キャッチ音=pygame.mixer.Sound("sound\キャッチ音.wav")
 #BGMの再生
BGM.play(-1)
 #BGMのボリューム調整
BGM.set_volume(0.1)

#背景画像に方眼紙を表示する
#方眼紙()

#繰り返し処理の実行
game_main()

root.mainloop()