CodingMaker

Python 본문

Python

Python

메이커K 2019. 12. 3. 07:13

거북이 그래픽 응용

마우스로 거북이를 움직이는

코드 실행
import turtle as t

t.speed(0)
t.pensize(2)
t.hideturtle()
t.onscreenclick(t.goto)

타자 게임

타자 연습 프로그램 실행
import random
import time

w=["cat", "dog", "fox","monkey","mouse","panda","frog","snake","wolf"]
n=1 #문제 번호
print("[타자문제] 준비되면 엔터!")
input() #사용자가 엔터를 누를 때까지 기다립니다.
start = time.time() #시작 시간을 기록합니다.

q=random.choice(w)


while n <=5: #문제를 5번 반복합니다.
    print("*문제",n)
    print(q)    #문제를 보여줍니다.
    x=input() # 사용자 입력을 받습니다.
    if q==x:    # 문제와 입력이 같을 때(올바로 입력했을 때)
        print("통과!") # "통과!"라고 출력합니다.
        n=n+1  #문제 번호를 1 증가시킵니다.
        q=random.choice(w)  # 새 문제를 뽑숩니다.

    else:
        print("오타! 다시도전!")

end = time.time()   # 끝난 시간을 기록합니다.
et=end-start        # 실제로 걸린 시간을 계산합니다.
et=format(et,".2f") # 보기 좋게 소수점 2자리까지만 표기합니다.
print("타자시간:", et,"초")

  거북이 대포 게임  

   
import turtle as t
import random

def turn_up(): #[↑]
    t.left(2)
def turn_down():
    t.right(2)
def fire():
    ang=t.heading()
    while t.ycor()>0:
        t.forward(15)
        t.right(5)

    d=t.distance(target,0) #거북이~목표 지점과의 거리를 구하기
    t.sety(random.randint(10,100)) # 성공or 실패 메시지를 위치지정
    if d<25:
        t.color("blue")
        t.write("Good!",False,"center",("",15))
    else:
        t.color("red")
        t.write("Bad!",False,"center",("",15))
    t.color("black")
    t.goto(-200,10)
    t.setheading(ang)

t.goto(-300,0)
t.down()
t.goto(300,0)

target=random.randint(50,150)
t.pensize(3)
t.color("green")
t.up()
t.goto(target-25,2)
t.down()
t.goto(target+25,2)

t.color("black")
t.up()
t.goto(-200,10)
t.setheading(20)
t.onkeypress(turn_up,"Up")
t.onkeypress(turn_down,"Down")
t.onkeypress(fire,"space")
t.listen()

  에러 해석하기  

 - 여러분들이 입력한코드가 때로는 작동하지 않고 에러가 발생할때가 있습니다. 이때 에러 메세지가 표시되는데 이를 해석하면 좀더 쉽게 에러를 고칠수 있습니다.

 

  수학문제 풀기  

컴퓨터의 기능중

 

  파이썬 기타 라이브러리