💻STUDY/PYTHON STUDY

13주차 수업 복습

coldNoodlePigeon 2021. 6. 3.

애니메이션과 게임

 

1. 시간 출력하기

 

  • time 모듈
import time
for i in range(10):
	print(time.asctime())
    time.sleep(1)
Thu Jun  3 10:55:34 2021
Thu Jun  3 10:55:35 2021
Thu Jun  3 10:55:36 2021
Thu Jun  3 10:55:37 2021
Thu Jun  3 10:55:38 2021
Thu Jun  3 10:55:39 2021
Thu Jun  3 10:55:40 2021
Thu Jun  3 10:55:41 2021
Thu Jun  3 10:55:42 2021
Thu Jun  3 10:55:43 2021

 

 

>>> import time
>>> time.localtime()
time.struct_time(tm_year=2021, tm_mon=6, tm_mday=3, tm_hour=10, tm_min=58, tm_sec=23, tm_wday=3, tm_yday=154, tm_isdst=0)
>>> t=time.localtime()
>>> t[0]
2021
>>> for item in t:
	print(item)

	
2021
6
3
10
58
33
3
154
0

 

디지털 시계 예제 

 

from tkinter import*
import time

tk=Tk()
canvas=Canvas(tk,width=500,height=500)
canvas.pack()

width=500
height=500

while 1:
    t=time.localtime()
    hour=t[3]
    minute=t[4]
    second=t[5]
    canvas.delete("all")
    myclock=str(hour)+":"+str(minute)+":"+str(second)
    canvas.create_text(250,250,text=myclock,font=('Arial',25))
    time.sleep(1)
    tk.update()

 

아날로그 시계 만들기

 

1초=6도=360/6

radian=(degree/360)*2*pi 

 

from tkinter import*
import time
import math

tk=Tk()
canvas=Canvas(tk,width=500,height=500)
canvas.configure(background='white')
canvas.pack()

width=500
height=500
cx=width/2
cy=height/2

sr=height/2 -50 #second r
mr=height/2 -80 #minute r
hr=height/2 -110 #clock r

while 1:
    t=time.localtime()
    hour=(t[3]+t[4]/60)*30
    minute=(t[4]+t[5]/60)*6
    second=t[5]*6

    canvas.delete("all")

    #시계 테두리
    canvas.create_arc(10,10,width-10,height-10,extent=359,style=CHORD,width=2)

    hx=hr*math.sin(hour/360*3.14*2)
    hy=hr*math.cos(hour/360*3.14*2)

    #시침
    canvas.create_line(cx,cy,cx+hx,cy-hy,fill='Blue',width=10)

    mx=mr*math.sin(minute/360*3.14*2)
    my=mr*math.cos(minute/360*3.14*2)

    #분침
    canvas.create_line(cx,cy,cx+mx,cy-my,fill='Green',width=6)

    sx=sr*math.sin(second/360*3.14*2)
    sy=sr*math.cos(second/360*3.14*2)

    #초침
    canvas.create_line(cx,cy,cx+sx,cy-sy,fill='Red',width=2)

    #시계 중심
    canvas.create_arc(cx-10,cy-10,cx+10,cy+10,extent=359,style=CHORD,width=2,fill='black')

    time.sleep(1)
    tk.update()

 

Frog 게임 

 

from tkinter import*
import time
import random

#Frog class
class Frog:
    #초기화 method
    def __init__(self,canvas,car1,car2,car3,color):
        self.canvas=canvas
        self.car1=car1
        self.car2=car2
        self.car3=car3
        self.id=canvas.create_oval(10,10,50,50,fill=color)
        self.x=0
        self.y=0
        self.step=60
        self.life=5
        self.score=0
        self.canvas_width=self.canvas.winfo_width()
        self.canvas.bind_all('<KeyPress-Up>',self.move_up)
        self.canvas.bind_all('<KeyPress-Left>',self.move_left)
        self.canvas.bind_all('<KeyPress-Right>',self.move_right)
        canvas.create_text(90,40,text="score : "+str(self.score))
        canvas.create_text(400,40,text="life : "+str(self.life))


    #충돌 검사 메소드
    def hit_car(self,pos):
        car_pos=self.canvas.coords(self.car1.id)
        if pos[2] >= car_pos[0] and pos[0] <= car_pos[2]:
            if pos[1] >= car_pos[1] and pos[1] <= car_pos[3]:
                return True
        car_pos=self.canvas.coords(self.car2.id)
        if pos[2] >= car_pos[0] and pos[0] <= car_pos[2]:
            if pos[1] >= car_pos[1] and pos[1] <= car_pos[3]:
                return True
        car_pos=self.canvas.coords(self.car3.id)
        if pos[2] >= car_pos[0] and pos[0] <= car_pos[2]:
            if pos[1] >= car_pos[1] and pos[1] <= car_pos[3]:
                return True


        return False

    #화면 출력 메소드
    def draw(self):
        self.canvas.move(self.id,self.x,self.y)
        self.x=0
        self.y=0

        pos=self.canvas.coords(self.id)

        if pos[0] <=0:
            self.canvas.move(self.id,self.step/2,self.y)
            self.x=0
        elif pos[2] >=self.canvas_width:
            self.canvas.move(self.id,-self.step/2,self.y)
            self.x=0
        elif pos[1]<60:
            self.score=self.score+10
            canvas.create_rectangle(10,10,200,60,outline=tk.cget('bg'),fill=tk.cget('bg'))
            canvas.create_text(90,40,text="score : "+str(self.score))
            self.canvas.move(self.id,250-pos[0],420)


        if self.hit_car(pos) == True:
           self.life=self.life - 1
           if self.life<0:
               canvas.create_text(250,260,text="G A M E O V E R")
           else:
               canvas.create_rectangle(300,10,550,60,outline=tk.cget('bg'),fill=tk.cget('bg'))
               canvas.create_text(400,40,text='life: '+str(self.life))
               self.canvas.move(self.id,250-pos[0],430-pos[1])


       #위,왼쪽,오른쪽 이동 메소드
    def move_up(self,evt):
        self.y=-self.step
    def move_left(self,evt):
        self.x=-self.step/2 #천천히 이동 위해 2로 나눠줌
    def move_right(self,evt):
        self.x=self.step/2



#자동차 클래스

class Car:
    #자동차 초기화 메소드
    def __init__(self,canvas,x,y,color,speed):
        self.canvas=canvas
        self.id=canvas.create_rectangle(10,10,100,60,fill=color)
        self.canvas.move(self.id,x,y)
        self.speed=speed
        self.x=speed
        self.y=0

    #자동차 출력 메소드
    def draw(self):
        self.canvas.move(self.id,self.x,self.y)
        pos=self.canvas.coords(self.id)
        if pos[0] <= -100:
            self.canvas.move(self.id,600,0)
        elif pos[2] >= 700:
            self.canvas.move(self.id,-700,0)


tk=Tk()
tk.title("Frog")
tk.resizable(0,0)
tk.wm_attributes("-topmost",1)
canvas=Canvas(tk,width=500,height=500)
canvas.pack()
tk.update()

car1=Car(canvas,10,60,"red",2)
car2=Car(canvas,500,100,"green",-3)
car3=Car(canvas,10,300,"yellow",1)
frog=Frog(canvas,car1,car2,car3,"blue")

while True:
    if frog.life >=0:
        car1.draw()
        car2.draw()
        car3.draw()
        frog.draw()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)

'💻STUDY > PYTHON STUDY' 카테고리의 다른 글

12주차 수업복습  (0) 2021.05.29
11주차 수업 복습  (0) 2021.05.22
10주차 수업 복습  (0) 2021.05.13
9주차 수업 복습  (0) 2021.05.09
8주차 배운거 복습  (0) 2021.04.28

댓글