13 Sep 2018
|
Python
pygame
PyGame 기반 PNG 이미지 로드하는 방법
import pygame
import sys
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
white = (255, 255, 255)
black = (0, 0, 0)
pygame.init()
pygame.display.set_caption("Rain")
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
class Player(object):
def __init__(self):
self.x = 100
self.y = 300
self.image = pygame.image.load("images/player.png").convert_alpha()
def draw(self):
screen.blit(self.image, (self.x, self.y))
def main():
player = Player()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(black)
player.draw()
pygame.display.update()
if __name__ == "__main__":
main()
12 Sep 2018
|
Python
pygame
PyGame 기반 비오는 장면 연출
import pygame
import sys
import random
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
white = (255, 255, 255)
black = (0, 0, 0)
pygame.init()
pygame.display.set_caption("Rain")
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
class Raindrop(object):
def __init__(self):
self.height = random.randint(4, 7)
self.speed = random.randint(5, 10)
self.x = random.randint(0, SCREEN_WIDTH)
self.y = -self.height
def move(self):
self.y += self.speed
def draw(self):
pygame.draw.line(screen, white, (self.x, self.y), (self.x, self.y + self.height), 1)
def main():
raindrops = []
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
raindrops.append(Raindrop())
screen.fill(black)
for drop in raindrops:
drop.move()
drop.draw()
if drop.y > SCREEN_HEIGHT:
raindrops.remove(drop)
pygame.display.update()
if __name__ == "__main__":
main()
11 Sep 2018
|
Python
pygame
Key 입력으로 도형 움직이는 코드
import pygame
import sys
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
white = (255, 255, 255)
black = (0, 0, 0)
pygame.init()
pygame.display.set_caption("Simple PyGame Example")
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pos_x = 200
pos_y = 200
clock = pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
key_event = pygame.key.get_pressed()
if key_event[pygame.K_LEFT]:
pos_x -= 1
if key_event[pygame.K_RIGHT]:
pos_x += 1
if key_event[pygame.K_UP]:
pos_y -= 1
if key_event[pygame.K_DOWN]:
pos_y += 1
screen.fill(black)
pygame.draw.circle(screen, white, (pos_x, pos_y), 20)
pygame.display.update()
07 Sep 2018
|
Python
turtle
tkinter
tkinter와 turtle 동시에 사용하기
기본 예제
import turtle
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(master=root, width=500, height=500)
canvas.pack()
t = turtle.RawTurtle(canvas)
tk.Button(master=root, text="Forward", command=lambda: t.forward(100)).pack(side=tk.TOP)
tk.Button(master=root, text="Back", command=lambda: t.back(100)).pack(side=tk.BOTTOM)
tk.Button(master=root, text="Left", command=lambda: t.left(90)).pack(side=tk.LEFT)
tk.Button(master=root, text="Right", command=lambda: t.right(90)).pack(side=tk.RIGHT)
root.mainloop()
거북이 클래스 만들어서 tkinter와 같이 사용하기
import turtle
import tkinter as tk
WIDTH = 500
HEIGHT = 500
class MyTurtle(turtle.RawTurtle):
def __init__(self, canvas):
super(MyTurtle, self).__init__(canvas)
self.shape("turtle")
self.shapesize(2, 2)
self.getscreen().bgcolor("yellow")
self.center_offset_x = WIDTH / 2
self.center_offset_y = HEIGHT / 2
canvas.bind("<Button-1>", self.on_mouse_clicked)
self.is_moving = False
def on_mouse_clicked(self, event):
if self.acquire_lock():
x = event.x - self.center_offset_x
y = -(event.y - self.center_offset_y)
print("clicked ({0}, {1})".format(x, y))
self.goto(x, y)
self.release_lock()
def acquire_lock(self):
if self.is_moving is True:
return False
self.is_moving = True
return True
def release_lock(self):
self.is_moving = False
root = tk.Tk()
canvas = tk.Canvas(master=root, width=WIDTH, height=HEIGHT)
canvas.pack()
t = MyTurtle(canvas)
tk.Button(master=root, text="Forward", command=lambda: t.forward(100)).pack(side=tk.TOP)
tk.Button(master=root, text="Back", command=lambda: t.back(100)).pack(side=tk.BOTTOM)
tk.Button(master=root, text="Left", command=lambda: t.left(90)).pack(side=tk.LEFT)
tk.Button(master=root, text="Right", command=lambda: t.right(90)).pack(side=tk.RIGHT)
root.mainloop()
06 Sep 2018
|
Python
turtle
마우스 클릭 이벤트에 따라 움직이는 거북이 클래스 만들어보기
거북이 클래스 만들기
import turtle
class MyTurtle(turtle.Turtle):
def __init__(self):
super(MyTurtle, self).__init__()
self.shape("turtle")
self.shapesize(2, 2)
self.getscreen().bgcolor("yellow")
t = MyTurtle()
while True:
pass
마우스 클릭 이벤트에 반응하기
거북이가 이동하고 있는 도중에 마우스 클릭 이벤트가 온 경우를 처리하기 위한 코드가 포함되어 있습니다.
import turtle
class MyTurtle(turtle.Turtle):
def __init__(self):
super(MyTurtle, self).__init__()
self.shape("turtle")
self.shapesize(2, 2)
self.color("purple")
self.window = turtle.Screen()
self.window.onclick(self.on_mouse_clicked)
self.is_moving = False
def loop(self):
self.window.mainloop()
def on_mouse_clicked(self, x, y):
print("clicked ({0}, {1})".format(x, y))
if self.is_moving is True:
return
self.is_moving = True
self.goto(x, y)
self.is_moving = False
t = MyTurtle()
t.loop()
Lock 설정/해제 방식으로 마우스 클릭 이벤트 처리해보기
import turtle
class MyTurtle(turtle.Turtle):
def __init__(self):
super(MyTurtle, self).__init__()
self.shape("turtle")
self.shapesize(2, 2)
self.color("purple")
self.window = turtle.Screen()
self.window.onclick(self.on_mouse_clicked)
self.is_moving = False
def loop(self):
self.window.mainloop()
def on_mouse_clicked(self, x, y):
if self.acquire_lock():
self.goto(x, y)
self.release_lock()
def acquire_lock(self):
if self.is_moving is True:
return False
self.is_moving = True
return True
def release_lock(self):
self.is_moving = False
t = MyTurtle()
t.loop()