0
1

More than 3 years have passed since last update.

Python boot camp by Dr.Angela day20

Last updated at Posted at 2021-05-29

Mission>> Invader Game Program

必要な機能
☑ Create a bar body
☑ Move the bar
☑ Control the line
4. Detect collision with food
5. Create scoreboard
6. Detect collision with wall
7. Detect collision with tail

※4~7はday21で扱う

Step 1: barの作成

最初のbarの長さは3
---> Angelaは3つの四角をくっつけて表示し、time.sleep( )で遅くしてアニメーションみたいにする手法を取っていたが、それだとこの後turnしたりしたときにコーディングが複雑になりそうだと思ったので、自分は以下のようにbar自体の長さをstretchする方向で考えた。そうすると、snakeみたいに1マスずつ動かないでbarごと動く感じになることに注意。今回はinvader gameなので問題ないが。

bar = Turtle(shape = "square")     #default square size = 20×20
new_bar = []
x = 0
y = 0
bar.color("white","white")
bar.home()

for i in range(0,3):
    new_bar.append(i)
    length = len(new_bar)

line.turtlesize(stretch_wid=1,stretch_len=length,outline=1) 

Step 2: barのアニメーション

まずは四隅に行ったら左に曲がるという永遠にスクリーン周囲をループするアニメーションを作成してみた。ちなみにset.heading( )の引数は下記参照。
set.heading( ).PNG

invader_loop_1.py
from turtle import Turtle, Screen

screen = Screen()
screen.setup(1000,600)
screen.bgcolor("black")
screen.title("Snake Game")

bar = Turtle(shape = "square")  #default square size = 20×20
new_bar = []
x = 0
y = 0
bar.color("white","white")
bar.home()

for i in range(0,3):
    new_bar.append(i)
    length = len(new_bar)

bar.turtlesize(stretch_wid=1,stretch_len=length,outline=1)  #bar自体長くする

game_is_on = True
while game_is_on:                   #---> whileの中、修正必要
    if bar.xcor() > 260:
        bar.setheading(90)
    if bar.ycor() > 260:
        bar.setheading(180) 
    if bar.xcor() < -260:
        bar.setheading(270)
    if bar.ycor() < -260:
        bar.setheading(0)

    line.penup()
    line.forward(20)

screen.exitonclick()

でもこれだと、右端に行ったときにループを繰り返してくれなくなる。2週目のwhileにおいて左下から右下に移動した後、辺上に沿わず、挙動がおかしくなる。whileの中を修正しないといけない。

★スクリーン上をひたすらLoop

invader_loop_2.py
from turtle import Turtle, Screen

screen = Screen()
screen.setup(600,600)
screen.bgcolor("black")
screen.title("Invader Game")

bar = Turtle(shape = "square")     #default square size = 20×20
x = -260
y = -260
bar.setpos(x,y)
bar.color("white","white")
bar.penup()
new_bar = []

for i in range(0,3):
    new_bar.append(i)
    length = len(new_bar)

bar.turtlesize(stretch_wid=1,stretch_len=length,outline=1)  #lineの大きさ長くする

game_is_on = True
bar.forward(2*abs(x))    #今回はstartがsetpos(-260,-260)なので2*abs(x)の分だけ進む

while game_is_on:
    if abs(bar.xcor()) == 260 and abs(bar.ycor()) == 260:
        bar.left(90)

    bar.penup()
    bar.forward(20)

screen.exitonclick()

★スクリーン上をひたすらLoop (OOP ver.)

この後はOOPでコーディングしていく。
classとしては 1. Bar のみでまず作成。とりあえず、上記と同様の挙動(Loop)を示すアニメーションを作成した。

invader_main_loop.py

from turtle import Turtle, Screen
from snake_bar_loop import Bar

screen = Screen()
screen.setup(600,600)
screen.bgcolor("black")
screen.title("Invader Game")

bar = Bar()
#Bar().create_bar()   bar.move()の中で、create_bar()を呼んでいるので、main.pyの方でcreate_bar()を呼ばなくて良い!
bar.move()

screen.exitonclick()
invader_bar_loop.py
from turtle import Turtle

#BAR = Turtle(shape = "square")  #default square size = 20×20  BARとした方がいいのかself.barとした方がいいのか
X = -260
Y = -260
GAME_IS_ON = True

class Bar:

    def __init__(self):
        self.new_bar = []
        self.bar = Turtle(shape = "square")
        self.create_bar()   #ここでcreate_bar()を呼んでいるので、main.pyの方でcreate_bar()を呼ばなくて良い!

    def create_bar(self):
        self.bar.penup()
        self.bar.setpos(X,Y)
        self.bar.color("white","white")
        for i in range(0,3):
            self.new_bar.append(i)
            length = len(self.new_bar)
        #print(length)
        self.bar.turtlesize(stretch_wid=1,stretch_len=length,outline=1) 

    def move(self):
        self.bar.penup()
        self.bar.forward(2*abs(X))
        while GAME_IS_ON:
            if abs(self.bar.xcor()) == 260 and abs(self.bar.ycor()) == 260:
                self.bar.left(90)

            self.bar.penup()
            self.bar.forward(20)

Step 3: 矢印キーでbarの進路変更

invader_main.py
from turtle import Turtle, Screen
from snake_bar import Bar
#from snake_food import Food
#from snake_scoreboard import Scoreboard

screen = Screen()
screen.setup(600,600)
screen.bgcolor("black")
screen.title("Invader Game")

bar = Bar()
screen.listen()
#key = ["Up", "Down","Left","Right"]
#if input_key in key:
screen.onkey(bar.up, "Up")
screen.onkey(bar.down, "Down")
screen.onkey(bar.left, "Left")
screen.onkey(bar.right, "Right")
#screen.onkey(bar.home, "space")  #---> これやったらもうmove()動かしたくないんだが...。

#Bar().create_bar()   bar.move()の中で、create_bar()を呼んでいるので、main.pyの方でcreate_bar()を呼ばなくて良い!
#bar.head()   ---> __init__の中に入れてある
bar.move()

screen.exitonclick()
invader_bar.py
from turtle import Turtle, Screen

#BAR = Turtle(shape = "square")  #default square size = 20×20  BARとした方がいいのかself.barとした方がいいの
GAME_IS_ON = True
X = -260
Y = -260
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0        #ハードコーディングを避けるべく、全体を通して不変の変数はクラス外で定義

class Bar:

    def __init__(self):
        self.new_bar = []
        self.bar = Turtle(shape = "square")
        self.create_bar()   #ここでcreate_bar()を呼んでいるので、main.pyの方でcreate_bar()を呼ばなくて良い!

    def create_bar(self):
        self.bar.penup()
        self.bar.setpos(X,Y)
        self.bar.color("white","white")
        for i in range(0,3):
            self.new_bar.append(i)
            length = len(self.new_bar)
        self.bar.turtlesize(stretch_wid=1,stretch_len=length,outline=1)

    def move(self):
        self.bar.penup()
        for i in range(100):   #while game_is_on:
            self.bar.forward(20)

    def up(self):
        self.bar.setheading(UP)

    def down(self):
        self.bar.setheading(DOWN)

    def right(self):
        self.bar.setheading(RIGHT)

    def left(self):
        self.bar.setheading(LEFT)

    def home(self):     #まだ使用していない
        GAME_IS_ON = False
        self.bar.home()
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1