[poo_game.py]
from selectors import EVENT_WRITE
from signal import pthread_sigmask
from unittest import runner
import pygame
import random
#################################################################################################################################
# 0.기본 화면 초기화 부분
pygame.init() #초기화
#화면 크기 조정하기
screen_width = 480
screen_height = 640
screen = pygame.display.set_mode((screen_width,screen_height))
#화면 타이틀 설정
pygame.display.set_caption("POO Game")
#FPS
clock = pygame.time.Clock()
#################################################################################################################################
# 1.사용자화 게임 초기화 (배경이미지, 게임캐릭터 불러오기, 좌표이동, 폰트, )
#배경이미지 파일경로 설정
background = pygame.image.load("/Users/dlgpdnjs_99/Desktop/VSCode/pygame/pygame_practice/background_h.PNG")
# 게임 캐릭터 불러오기 -> 5
character = pygame.image.load("/Users/dlgpdnjs_99/Desktop/VSCode/pygame/pygame_practice/prac_character.png")
character_size = character.get_rect().size #이미지의 크기를 가져와
character_width = character_size[0] #가로
character_height = character_size[1] #세로
character_x_pos = (screen_width / 2) - (character_width / 2) # 캐릭터가 처음 위치하는 엑스 좌표 (중앙에 위치)
character_y_pos = screen_height - character_height # 캐릭터가 처음 위치하는 와이 좌표
poo = pygame.image.load("/Users/dlgpdnjs_99/Desktop/VSCode/pygame/pygame_practice/prac_enemy.png")
poo_size = poo.get_rect().size #이미지의 크기를 가져와
poo_width = poo_size[0] #가로
poo_height = poo_size[1] #세로
poo_x_pos = random.randint(0, screen_width - poo_width) # 캐릭터가 처음 위치하는 엑스 좌표 (중앙에 위치)
poo_y_pos = 0 # 캐릭터가 처음 위치하는 와이 좌표
# 이동 위치
to_x = 0
character_speed = 10
poo_speed = 10
#################################################################################################################################
# 2.이벤트 처리 (키보드, 마우스 등)
running = True # 게임이 실행 중이라면 이후 모든 이벤트를 실행
while running : # 게임이 실행중인동안 이벤트 실행
dt = clock.tick(60) # 게임화면의 초당 프레임 설정
# print("FPS : " + str(clock.get_fps()))
for event in pygame.event.get() :
if event.type == pygame.QUIT : # 창에서 엑스를 눌러 종료 시켰을때 이벤트 실행
running = False
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_LEFT :
to_x -= character_speed
elif event.key == pygame.K_RIGHT :
to_x += character_speed
if event.type == pygame.KEYUP :
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT :
to_x = 0
# 3.게임 캐릭터 위치 정의 + 경곗값 처리 (while 문 안에서)
character_x_pos += to_x
if character_x_pos < 0 :
character_x_pos = 0
elif character_x_pos > screen_width - character_width :
character_x_pos = screen_width - character_width
poo_y_pos += poo_speed
if poo_y_pos > screen_height :
poo_y_pos = 0
poo_x_pos = random.randint(0, screen_width - poo_width)
# 4.적과 캐릭터의 충돌 처리를 위한 rect 정보 업데이트 (while 문 안에서)
character_rect = character.get_rect() # 캐릭터
character_rect.left = character_x_pos
character_rect.top = character_y_pos
poo_rect = poo.get_rect() # 적
poo_rect.left = poo_x_pos
poo_rect.top = poo_y_pos
if character_rect.colliderect(poo_rect) : # 충돌시
print('충돌!충돌!')
running = False
# 5.화면 그리기 (while 문 안에서)
screen.blit(background, (0,0)) # 변수,(위치)
screen.blit(character, (character_x_pos, character_y_pos))
screen.blit(poo, (poo_x_pos, poo_y_pos))
# 6.화면 업데이트 : 무조건 실행 (while 문 안에서)
pygame.display.update()
#################################################################################################################################
# 7.종료
pygame.quit()'source Code > Python' 카테고리의 다른 글
| Python | py_basic_week1 (0) | 2022.09.02 |
|---|---|
| Python | py_basic_week1 (0) | 2022.09.01 |
| Python | memory_game.py (0) | 2022.08.26 |
| Python | weapon_game.py (0) | 2022.08.25 |
| Python | calculator.py (0) | 2022.08.18 |