계속 손에 안잡히고 시작 못해서
간단하게 파이게임 진행해보려고함.
파이게임 단순 화면띄우기 예제
import sys
import pygame
from pygame.locals import QUIT
pygame.init()
SURFACE = pygame.display.set_mode((400, 300))
pygame.display.set_caption("just window")
def main():
while True:
SURFACE.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
if __name__ == "__main__":
main()
이벤트
- 이벤트 큐에 저장
- 큐 맨앞에 이벤트 꺼내 처리
간단한 글자 띄우기
import sys
import pygame
from pygame.locals import QUIT
pygame.init()
SURFACE = pygame.display.set_mode((400, 300))
def main():
sysfont = pygame.font.SysFont(None, 36)
counter = 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
counter += 1
SURFACE.fill((0, 0, 0))
count_image = sysfont.render("count is {}".format(counter), True, (255, 255, 255))
SURFACE.blit(count_image, (50, 50))
pygame.display.update()
if __name__ == "__main__":
main()
fps 설정 1
- pygame.time.Clock() tick(10) 1초에 10번 루프
import sys
import pygame
from pygame.locals import QUIT
pygame.init()
SURFACE = pygame.display.set_mode((400, 300))
FPSCLOCK = pygame.time.Clock()
def main():
sysfont = pygame.font.SysFont(None, 36)
counter = 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
counter += 1
SURFACE.fill((0, 0, 0))
count_image = sysfont.render("count is {}".format(counter), True, (255, 255, 255))
SURFACE.blit(count_image, (50, 50))
pygame.display.update()
FPSCLOCK.tick(10)
if __name__ == "__main__":
main()
상자 그리기
import sys
import pygame
from pygame.locals import QUIT, Rect
pygame.init()
SURFACE = pygame.display.set_mode((400, 300))
FPSCLOCK = pygame.time.Clock()
def main():
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit
SURFACE.fill((255, 255, 255))
pygame.draw.rect(SURFACE, (255, 0, 0), (10, 20, 100, 50))
pygame.draw.rect(SURFACE, (255, 0, 0), (150, 10, 100, 30), 3)
pygame.draw.rect(SURFACE, (0, 255, 0), ((100, 80), (80, 50)))
rect0 = Rect((200, 60), (140, 80))
pygame.draw.rect(SURFACE, (0, 0, 255), rect0)
rect1 = Rect((30, 160), (100, 50))
pygame.draw.rect(SURFACE, (255, 255, 0), rect1)
pygame.display.update()
FPSCLOCK.tick(3)
if __name__ == "__main__":
main()
import sys
import pygame
from pygame.locals import QUIT, Rect, Circle
pygame.init()
SURFACE = pygame.display.set_mode((400, 300))
FPSCLOCK = pygame.time.Clock()
def main():
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit
SURFACE.fill((255, 255, 255))
pygame.draw.circle(SURFACE, (255, 0, 0), (50, 50), 20)
pygame.draw.circle(SURFACE, (255, 0, 0), (150, 50), 20, 10)
pygame.draw.circle(SURFACE, (0, 255, 0), (50, 150), 10)
pygame.draw.circle(SURFACE, (0, 255, 0), (150, 150), 20)
pygame.draw.circle(SURFACE, (0, 255, 0), (250, 150), 30)
pygame.draw.ellipse(SURFACE, (255, 0, 0), (50, 50, 140, 60))
pygame.draw.ellipse(SURFACE, (255, 0, 0), (250, 30, 90, 90))
pygame.display.update()
FPSCLOCK.tick(3)
if __name__ == "__main__":
main()
원, 타원 그리기
import sys
import pygame
from pygame.locals import QUIT, Rect
pygame.init()
SURFACE = pygame.display.set_mode((400, 300))
FPSCLOCK = pygame.time.Clock()
def main():
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit
SURFACE.fill((255, 255, 255))
pygame.draw.circle(SURFACE, (255, 0, 0), (50, 50), 20)
pygame.draw.circle(SURFACE, (255, 0, 0), (150, 50), 20, 10)
pygame.draw.circle(SURFACE, (0, 255, 0), (50, 150), 10)
pygame.draw.circle(SURFACE, (0, 255, 0), (150, 150), 20)
pygame.draw.circle(SURFACE, (0, 255, 0), (250, 150), 30)
pygame.draw.ellipse(SURFACE, (255, 0, 0), (50, 50, 140, 60))
pygame.draw.ellipse(SURFACE, (255, 0, 0), (250, 30, 90, 90))
pygame.display.update()
FPSCLOCK.tick(3)
if __name__ == "__main__":
main()
선긋기
import sys
import pygame
from pygame.locals import QUIT
pygame.init()
SURFACE = pygame.display.set_mode((400, 220))
FPSCLOCK = pygame.time.Clock()
def main():
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
SURFACE.fill((255, 255, 255))
pygame.draw.line(SURFACE, (255, 0, 0), (10, 80), (200, 80))
pygame.draw.line(SURFACE, (255, 0, 0), (10, 150), (200, 150), 15)
pygame.draw.line(SURFACE, (0, 255, 0), (250, 30), (250, 200))
start_pos = (300, 30)
end_pos = (380, 200)
pygame.draw.line(SURFACE, (0, 0, 255), start_pos, end_pos, 10)
pygame.display.update()
FPSCLOCK.tick(3)
if __name__ == "__main__":
main()
그리드 만들기
import sys
import pygame
from pygame.locals import QUIT
pygame.init()
SURFACE = pygame.display.set_mode((400, 300))
FPSCLOCK = pygame.time.Clock()
def main():
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
SURFACE.fill((0, 0, 0))
for xpos in range(0, 400, 25):
pygame.draw.line(SURFACE, 0xFFFFFF, (xpos, 0), (xpos,300))
for ypos in range(0, 300, 25):
pygame.draw.line(SURFACE, 0xFFFFFF, (0, ypos), (400, ypos))
pygame.display.update()
FPSCLOCK.tick(3)
if __name__ == "__main__":
main()
이은선
import sys
import random
import pygame
from pygame.locals import QUIT
pygame.init()
SURFACE = pygame.display.set_mode((400, 300))
FPSCLOCK = pygame.time.Clock()
def main():
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
SURFACE.fill((0, 0, 0))
pointlist = []
for _ in range(10):
xpos = random.randint(0, 400)
ypos = random.randint(0, 300)
pointlist.append((xpos, ypos))
pygame.draw.lines(SURFACE, (255, 255, 255), True, pointlist, 5)
pygame.display.update()
FPSCLOCK.tick(3)
if __name__ == "__main__":
main()
텍스트 쓰기
import sys
import pygame
from pygame.locals import QUIT
pygame.init()
SURFACE = pygame.display.set_mode((400, 200))
FPSCLOCK = pygame.time.Clock()
def main():
sysfont = pygame.font.SysFont(None, 72)
msg = sysfont.render("hello python", True, (0, 128, 128))
msg_rect = msg.get_rect()
msg_rect.center = (200, 100)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
SURFACE.fill((255, 255, 255))
SURFACE.blit(msg, msg_rect)
pygame.display.update()
FPSCLOCK.tick(3)
if __name__ == "__main__":
main()
텍스트 줌 회전
import sys
import pygame
from pygame.locals import QUIT
pygame.init()
SURFACE = pygame.display.set_mode((400, 200))
FPSCLOCK = pygame.time.Clock()
def main():
sysfont = pygame.font.SysFont(None, 72)
msg = sysfont.render("hello python", True, (0, 128, 128))
msg_rect = msg.get_rect()
theta = 0
scale = 1
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
SURFACE.fill((255, 255, 255))
theta += 5
scale = (theta % 360) / 180
tmp_msg = pygame.transform.rotozoom(msg, theta, scale)
tmp_rect = tmp_msg.get_rect()
tmp_rect.center = (200,150)
SURFACE.blit(tmp_msg, tmp_rect)
pygame.display.update()
FPSCLOCK.tick(30)
if __name__ == "__main__":
main()
마우스 클릭 이벤트
import sys
import pygame
from pygame.locals import QUIT, MOUSEBUTTONDOWN
pygame.init()
SURFACE = pygame.display.set_mode((400, 300))
FPSCLOCK = pygame.time.Clock()
def main():
mousepos = []
while True:
SURFACE.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
mousepos.append(event.pos)
for pos in mousepos:
pygame.draw.circle(SURFACE, (0, 255, 0), pos, 5)
pygame.display.update()
FPSCLOCK.tick(10)
if __name__ == "__main__":
main()
'컴퓨터과학' 카테고리의 다른 글
파이게임 - 3. 뱀 (0) | 2024.03.18 |
---|---|
파이게임 - 2. 지뢰찾기 (0) | 2024.03.18 |
jetson nano yahboom USB 부팅 삽질기 3(yolo 돌리기) (0) | 2022.11.10 |
jetson nano yahboom USB 부팅 삽질기 2(usb 부팅 완료, vnc까지) (0) | 2022.11.09 |
jetson nano yahboom USB 부팅 삽질기 1(emmc 설치 완료) (0) | 2022.11.04 |