2017-12-18 5 views
0

私はオリジナルのヘビのようなゲームを作ろうとしていて、「食べ物」をランダムな場所に移動したいのですが、クラスでそれをどうやって使うのかは分かりません。コードには他にもいくつかのエラーがありますが、今プレイヤーと食品に集中したいと思っています。Pythonカメでランダムモジュールを使用するにはどうすればいいですか?

import turtle 
import random 
"""-------""" 
t=turtle.Turtle() 
s=turtle.Screen() 
cube=turtle.Turtle 
"""------------""" 
WIDTH, HEIGHT=300, 300 
DISTANCE=5 
"""--------------""" 
s.setup(WIDTH,HEIGHT) 
"""------------""" 
t.width(1) 
s.bgcolor("dark green") 
"""-----------------""" 
class Border(): 
    def check(): 
     x, y = t.position() 
     if not -WIDTH/2 < x < WIDTH/2 or not -HEIGHT/2 < y < HEIGHT/2: 
      t.undo() # undo error 
      t.speed(0) 
      t.left(180) # turn around 
      t.forward(10) # redo movement but in new direction 
      t.speed(3) 
"""-------------""" 
randint=random.randint 
x=random.randint(cube.xcor, 0) 
y=random.randint(0,cube.ycor) 
"""---------------""" 
class Food(): 
    def block(): 
     cube.color("red") 
     for i in range(4): 
      cube.goto(x, -x, y, -y) 
      cube.begin_fill() 
      cube.forward(20) 
      cube.right(90) 
      cube.end_fill() 
"""---------------""" 
class Player(): 
    def move_up(): 
     player=False 
     while player==False: 
      for i in range(1): 
       t.forward(DISTANCE) 
       t.clear() 
    def move_left(): 
     t.speed(0) 
     t.left(90) 
     t.speed(3) 
    def move_right(): 
     t.speed(0) 
     t.right(90) 
     t.speed(3) 
"""------------""" 
collistion_check=Border() 
player1=Player() 
s.onkey(Player.move_up,"up") 
s.onkey(Player.move_left,"left") 
s.onkey(Player.move_right,"right") 
s.listen() 

答えて

0

一般的に言えば、プログラムは災害です。限り乱数であなたの当面の問題として、このコードは問題があります:

from random import randint 
... 
x = randint(cube.xcor(), 0) 
y = randint(0, cube.ycor()) 

すなわち:

randint=random.randint 
x=random.randint(cube.xcor, 0) 
y=random.randint(0,cube.ycor) 

構文ワイズ、これはより多くのようなものでなければなりません変数randintを使用しないため、変数xcor()ycor()が括弧を必要とする理由は明確ではありません。

あなたの目標は、画面上にランダムに食べ物を見つけることであるならば、私はいいと思う:あなたは冗長性を減らすために合うように変数を追加する

x = randint(cube.xcor()/2 - WIDTH/2, WIDTH/2 - cube.xcor()/2) 
y = randint(cube.ycor()/2 - HEIGHT/2, HEIGHT/2 - cube.ycor()/2) 

playerは今まであなたが無限ループにしている、矢を打つので、一度Trueになりますどのように

def move_up(): 
     player=False 
     while player==False: 
      for i in range(1): 
       t.forward(DISTANCE) 
       t.clear() 

それははっきりしていない:私はあなたのコードの残りの部分を見る最大の問題は、この方法です。しかし、食糧問題が解決されると、これに集中すると思います。

関連する問題