質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

944閲覧

コードをそのまま転記してみましたが、うまく動きません

suya1106

総合スコア1

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/04/21 03:14

#1
from tkinter import*
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title('潜水艦ゲーム')
c = Canvas(window,width=WIDTH,height=HEIGHT,bg='darkblue')
c.pack()

#2
ship_id = c.create_polygon(5,5,5,25,30,15,fill='red')
ship_id2 = c.create_oval(0,0,30,30,outline='red')
SHIP_R =15
MID_X = WIDTH/2
MID_Y = HEIGHT/2
c.move(ship_id,MID_X,MID_Y)
c.move(ship_id2,MID_X,MID_Y)

#3
SHIP_SPD = 10
def move_ship(event):

if event.keysym == 'Up': c.move(ship_id,0,-SHIP_SPD) c.move(ship_id2,0,-SHIP_SPD) elif event.keysym == 'Down': c.move(ship_id,0,SHIP_SPD) c.move(ship_id2,0,SHIP_SPD) elif event.keysym == 'Left': c.move(ship_id,-SHIP_SPD,0) c.move(ship_id2,-SHIP_SPD,0) elif event.keysym == 'Right': c.move(ship_id,SHIP_SPD,0) c.move(ship_id2,SHIP_SPD,0)

c.bind_all('<Key>',move_ship)

#4
from random import randint
bub_id = list()
bub_r = list()
bub_speed = list()
MIN_BUB_R = 10
MAX_BUB_R = 30
MAX_BUB_SPD = 10
GAP = 100
def create_bubble():
x = WIDTH + GAP
y = randint(0,HEIGHT)
r = randint(MIN_BUB_R,MAX_BUB_R)
id1 = c.create_oval(x-y, y-r, x+y, y+r, outline = 'white')
bub_id.append(id1)
bub_r.append(r)
bub_speed.append(randint(1,MAX_BUB_SPD))

#5
def move_bubbles():
for i in range(len(bub_id)):
c.move(bub_id[i],-bub_speed[i],0)

#6
def get_coords(id_num):
pos = c.coords(id_num)
x = (pos[0] + pos[2])/2
y = (pos[1] + pos[3])/2
return x,y

def del_bubble(i):
del bub_r[i]
del bub_speed[i]
c.delete(bub_id[i])
del bub_id[i]

def clean_up_bubs():
for i in range(len(bub_id)-1,-1,-1):
x,y = get_coords(bub_id[i])
if x < -GAP:
del_bubble(i)

from math import sqrt
def distance(id1,id2):
x1,y1 = get_coords(id1)
x2,y2 = get_coords(id2)
return sqrt((x2 - x1)**2 + (y2 - y1)**2)

def collision():
points = 0
for bub in range(len(bub_id)-1,-1,-1):
if distance(ship_id2,bub_id[bub]) < (SHIP_R + bub_r[bub]):
points +=(bub_r[bub] + bub_speed[bub])
del_bubble(bub)
return points

c.create_text(50,30,text='タイム',fill='white')
c.create_text(150,30,text='スコア',fill='white')
time_text = c.create_text(50,50,fill='white')
score_text = c.create_text(150,50,fill='white')
def show_score(score):
c.itemconfig(score_text,text=str(score))
def show_time(time_left):
c.itemconfig(time_text,text=str(time_left))

from time import sleep, time
BUB_CHANCE = 10
#15
TIME_LIMIT = 30
BONUS_SCORE = 1000

score=int(0)

bonus = 0
end = time() + TIME_LIMIT

while time() < end:
if randint(1,BUB_CHANCE) == 1:
create_bubble()
move_bubbles()
clean_up_bubs()
score += collision()
if(int(score / BONUS_SCORE)) > bonus:
bonus += 1
end += TIME_LIMIT
show_score(score)
show_time(int(end - time()))
window.update()
sleep(0.01)

c.create_text(MID_X,MID_Y,text='ゲームオーバー',fill='white',font=('Helvetica',30))
c.create_text(MID_X,MID_Y + 30,text='スコア:'+ str(score),fill='white')
c.create_text(MID_X,MID_Y + 45,text='ボーナスタイム:'+ str(bonus*TIME_LIMIT),fill='white')

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

K_3578

2021/04/21 03:19

質問はなんでしょうか。この内容から回答を得ることはほぼ不可能です。 https://teratail.com/help/question-tips 上記ヘルプの「質問するときのヒント」を参考に質問を修正してください
suya1106

2021/04/21 03:47

score += collision()が TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType' というエラーのようですが、よくわかりません。
K_3578

2021/04/21 04:45

いやよく分からないのはこっちだ、要件追記してください。
guest

回答1

0

ベストアンサー

この問題は、unsupported operand type(s) for +=: 'int' and 'NoneType'についてと同じものなのですね。

今回はしかたがありませんが、質問の追加は質問の編集で行ってください。
同じ質問が複数に分かれるのは好ましくありません。

上記の質問で回答したように、collisionという関数の定義に誤りがある可能性が高いです。
pythonはインデントが重要です。
コードがマークダウンになっていないため推測となりますが以下の間違いでしょう。

推定したsuya1106さんのコード

python

1def collision(): 2 points = 0 3 for bub in range(len(bub_id)-1,-1,-1): 4 if distance(ship_id2,bub_id[bub]) < (SHIP_R + bub_r[bub]): 5 points +=(bub_r[bub] + bub_speed[bub]) 6 del_bubble(bub) 7 return points

正しいコード

python

1def collision(): 2 points = 0 3 for bub in range(len(bub_id)-1,-1,-1): 4 if distance(ship_id2,bub_id[bub]) < (SHIP_R + bub_r[bub]): 5 points +=(bub_r[bub] + bub_speed[bub]) 6 del_bubble(bub) 7 return points

間違ったコードでは、一つも当たらなかったときにNoneが返りますので、
TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'
となります。

投稿2021/04/21 08:12

ppaul

総合スコア24666

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

suya1106

2021/04/21 15:26

ありがとうございました。もうあきらめかけていました。助かりました。インデントが大事なんですね。質問の仕方もわからず、失礼いたしました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問