python으로 게임 만들기/테트리스
[테트리스] 좌표값 설정
bay07
2024. 2. 23. 10:45
pygame에서는 pygame.rect 라는 직사각형 오브젝트를 제공한다.
pygame.Rect(left, top, width, height)
여기에서 left는 왼쪽 변의 중앙값을 말하고, top은 윗변의 중앙값을 가리킨다.
그리고 width는 사각형의 넓이, height는 높이를 이야기한다
pygame.draw.rect(배경, 색상, object이름)
이렇게 하면 우리가 만든 직사각형을 출력해볼 수 있다.
# 오브젝트 생성하기
# (100,50) 는 스크린에서의 위치
box = pygame.Rect(200,100,200,200)
pygame.draw.rect(background,pink,box)
더보기
import sys
import pygame
from pygame.locals import *
# 초기화 & 기능 사용 시작을 알림
pygame.init()
## 초당 프레임 단위 설정 ##
FPS = 30
FramePerSec = pygame.time.Clock()
# 전체 스크린의 가로, 세로 설정
screen_width = 500
screen_height = 600
# 컬러셋팅
white = (255,255,255)
black = (0,0,0)
organe = (255,204,153)
green = (204,255,229)
blue = (204,229,255)
pink = (255,204,229)
purple = (204,204,255)
# 스크린 생성하기
background = pygame.display.set_mode((screen_width, screen_height))
background.fill(blue)
print(background)
# 오브젝트 생성하기
# (100,50) 는 스크린에서의 위치
box = pygame.Rect(200,100,200,200)
pygame.draw.rect(background,pink,box)
# 게임창 이름 설정
pygame.display.set_caption("PYGAME Example")
# 아하 ! 연습할 때, 게임을 종료시키는 함수가 있어야 실행이 된다.
# 아래 코드 계속 적어줘야함.
# 게임을 종료시키는 함수
while True:
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
FramePerSec.tick(FPS)
pygame.draw.rect(background,pink,box,10)
또 이렇게 마지막에 숫자를 붙여주게 되면, 직사각형이 아닌 테두리를 가진 오브젝트가 출력된다
# 오브젝트 생성하기
# (100,50) 는 스크린에서의 위치
box = pygame.Rect(200,100,200,200)
pygame.draw.rect(background,pink,box,10)
더보기
import sys
import pygame
from pygame.locals import *
# 초기화 & 기능 사용 시작을 알림
pygame.init()
## 초당 프레임 단위 설정 ##
FPS = 30
FramePerSec = pygame.time.Clock()
# 전체 스크린의 가로, 세로 설정
screen_width = 500
screen_height = 600
# 컬러셋팅
white = (255,255,255)
black = (0,0,0)
organe = (255,204,153)
green = (204,255,229)
blue = (204,229,255)
pink = (255,204,229)
purple = (204,204,255)
# 스크린 생성하기
background = pygame.display.set_mode((screen_width, screen_height))
background.fill(blue)
print(background)
# 오브젝트 생성하기
# (100,50) 는 스크린에서의 위치
box = pygame.Rect(200,100,200,200)
pygame.draw.rect(background,pink,box,10)
# 게임창 이름 설정
pygame.display.set_caption("PYGAME Example")
# 아하 ! 연습할 때, 게임을 종료시키는 함수가 있어야 실행이 된다.
# 아래 코드 계속 적어줘야함.
# 게임을 종료시키는 함수
while True:
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
FramePerSec.tick(FPS)
▷ 참고자료