python08.pptx




8-13. 거북이 경주 게임

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import random
import turtle
 
t1 = turtle.Turtle()   # 첫 번째 거북이
t2 = turtle.Turtle()   # 두 번째 거북이
t3 = turtle.Turtle()   # 두 번째 거북이
 
t1.color("pink")
t1.shape("turtle")
t1.shapesize(5)
t1.pensize(5)
 
t2.color("blue")
t2.shape("turtle")
t2.shapesize(5)
t2.pensize(5)
 
t3.color("black")
t3.shape("turtle")
t3.shapesize(5)
t3.pensize(5)
 
t1.penup()
t1.goto(-300100)
t1.pendown()
 
t2.penup()
t2.goto(-3000)
t2.pendown()
 
t3.penup()
t3.goto(-300-100)
t3.pendown()
 
for i in range(100):         # 100번 반복한다.
  d1 = random.randint(16)     # 1부터 6 사이의 난수를 발생한다.
  t1.forward(d1)            # 난수만큼 이동한다.
  d2 = random.randint(16)     # 1부터 6사이의 난수를 발생한다.
  t2.forward(d2)            # 난수만큼 이동한다.
  d3 = random.randint(16)     # 1부터 6사이의 난수를 발생한다.
  t3.forward(d3)            # 난수만큼 이동한다.
cs

 



8-14. 애니메이션 만들기 (태극 무늬 그리는 함수)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def draw_shape(radius, color1):
    t.left(270)   #거북이가 아래방향으로 향함 
    t.width(3)   #선의 두께를 3
    t.color("black", color1)  #선의 색상은 black, 채워지는 색상은 color1
    t.begin_fill()
    t.circle(radius/2.0-180)  #반지름 radius/2.0의 반원을 그림
    t.circle(radius, 180)  # 반지름 radius의 반원을 그림
    t.left(180)  #거북이가 위쪽방향으로 향함
    t.circle(-radius/2.0-180)  #반지름 radius/2.0의 반원을 그림
    t.end_fill()
import turtle
 
t= turtle.Turtle()
t.reset()
draw_shape(200"red")
t.setheading(180)  #거북이가 서쪽방향으로 향함 
draw_shape(200"blue")
cs

 




8-17. 애스터로이드 게임

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import turtle
import random
 
player = turtle.Turtle()
player.color("blue")
player.shape("turtle")
player.penup()
player.speed(0)
screen = player.getscreen()
 
a1 = turtle.Turtle()
a1.color("red")
a1.shape("circle")
a1.penup()
a1.speed(0)
a1.goto(random.randint(-300300), random.randint(-300300))
 
a2 = turtle.Turtle()
a2.color("red")
a2.shape("circle")
a2.penup()
a2.speed(0)
a2.goto(random.randint(-300300), random.randint(-300300))
 
def turnleft():
    player.left(30# 왼쪽으로 30도 회전한다.
 
def turnright():
    player.right(30# 오른쪽으로 30도 회전한다.
 
screen.onkeypress(turnleft, "Left")
screen.onkeypress(turnright, "Right")
screen.listen()
 
def play():
    player.forward(2)   # 2픽셀 전진
    a1.forward(2)
    a2.forward(2)
    screen.ontimer(play, 10)   # 10ms가 지나면 play()를 다시 호출
 
screen.ontimer(play, 10)
cs

 




8-22. 앵그리 터틀 게임

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import turtle
import random
import math
 
player = turtle.Turtle()
player.shape("turtle")
screen = player.getscreen()
 
def turnleft():
          player.left(5)
 
def turnright():
          player.right(5)
def fire():
         x = 0
         y = 0
         velocity = 50
         angle = player.heading()
         vx = velocity*math.cos(angle*3.14/180.0)
         vy = velocity*math.sin(angle*3.14/180.0)
         while player.ycor() >= 0:
                  vx = vx
                  vy = vy - 5
                  x = x + vx
                  y = y + vy
                  player.goto(x, y)
 
screen.onkeypress(turnleft, "Left")
screen.onkeypress(turnright, "Right")
screen.onkeypress(fire, "space")
screen.listen()
cs

 



8-25. 암호화와 복호화

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
plain_text = input("암호화할 문자를 입력하시오 : ")
encrypted_text = ""
 
for c in plain_text:
      x = ord(c)  #문자를 코드값으로 변환
      x = x + 1   #코드값을 1 증가
      cc = chr(x) #코드값을 문자로 변환
      encrypted_text = encrypted_text + cc           #암호문에 추가
 
print(encrypted_text)
 
encrypted_text = input("암호를 풀 문자를 입력하시오 : ")
 
plain_text = ""
 
for c in encrypted_text:
      x = ord(c)  #문자를 코드값으로 변환
      x = x - 1   #코드값을 1 감소
      cc = chr(x) #코드값을 문자로 변환
      plain_text = plain_text + cc #평문에 추가
 
print(plain_text)
cs

 





8-28. 크리스마스 카드 그리기

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import turtle
import random
 
# 원그리기
def draw_circle(turtle, color, x, y, radius): 
    turtle.penup()
    turtle.fillcolor(color)
    turtle.goto(x, y)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(radius)
    turtle.end_fill()
 
# 사각형 그리기
def draw_rectangle(turtle, color, x, y, width, height):
    turtle.penup()
    turtle.fillcolor(color)
    turtle.goto(x, y)
    turtle.pendown()
    turtle.begin_fill()
    for i in range(2):
        turtle.forward(width)
        turtle.left(90)
        turtle.forward(height)
        turtle.left(90)
    turtle.end_fill()
    
# 마름모꼴 그리기함수
def draw_trapezoid(turtle, color, x, y, width, height):
    turtle.penup()
    turtle.fillcolor(color)
    turtle.goto(x, y)
    turtle.pendown()
    turtle.begin_fill()
    turtle.forward(width)
    turtle.right(60)
    turtle.forward(height)
    turtle.right(120)
    turtle.forward(width+20)
    turtle.right(120)
    turtle.forward(height)
    turtle.right(60)
    turtle.end_fill()
 
# 별그리기 함수
def draw_star(turtle, color, x, y, size):
    turtle.penup()
    turtle.fillcolor(color)
    turtle.goto(x, y)
    turtle.pendown()
    turtle.begin_fill()
    for i in range(10):
        turtle.forward(size)
        turtle.right(144)
    turtle.end_fill()
 
# 크리스마스 카드 그리기
= turtle.Turtle()
t.shape("turtle")
t.speed(0)
 
= 0
= 0
# 트리의 줄기를 그린다
draw_rectangle(t, "brown", x-20, y-503050)
 
# 트리의 잎을 그린다
width = 240
height = 20
for i in range(10):
    width = width - 20
    x = 0 - width/2
    draw_trapezoid(t,"green", x, y, width, height)
    draw_circle(t,"red", x + random.randint(0, width), y + random.randint(0, height), 10)
    y = y + height
 
 
draw_star(t,"yellow",4,y,100)
 
t.penup()
t.color("red")
t.goto(-200250)
t.write("Merry Christmas", font=("Arial"24"italic"))
t.goto(-200220)
t.write("Happy New Year!", font=("Arial"24"italic"))
cs

 



'PYTHON > TRAINNING' 카테고리의 다른 글

PYTHON 10장 tkinter로 GUI 만들기  (1) 2018.01.08
PYTHON 9장 리스트와 딕셔너리  (0) 2018.01.08
PYTHON 7장 함수  (1) 2018.01.08
PYTHON 6장 반복문  (0) 2018.01.06
PYTHON 5장 조건문  (0) 2018.01.06
Posted by 너래쟁이
: