에리카 대회 진행 상황 0807

임정혁·2023년 8월 7일
1

프로젝트

목록 보기
5/5
post-thumbnail

작동 이미지

  • 메인 게임 진행

  • 초록색이 캐릭터고 나머지가 퀘스트 블럭

    • 나중에 이미지 구하면 변경할 예정
  • 배경 이미지 추가

    • 시간이나 진행 상황에 따라 배경 움직이게 할 예정

  • 각 점수들 표시함

    • 게임 완성 단계에서 없어질 예정
  • 퀘스트 블럭과 닿으면 해당하는 퀘스트 점수 1씩 오름

    • 퀘스트 게임 로직이 구현되면 클리어 유무에 따라 증감 여부 결정

  • 퀘스트 블럭과 닿으면 터미널에 퀘스트 종류가 나옴

    • 임시로 보기좋게 해놓은거임

    • 나중에 print() 대신 구현한 다른 함수 넣어야됨

소스코드

import pygame  
import random 

pygame.init()  

# 화면 설정
screen_width = 1400  # 화면 너비 설정
screen_height = 800  # 화면 높이 설정

# 지정된 크기로 게임 화면을 생성
screen = pygame.display.set_mode((screen_width, screen_height))  
 # 게임 창의 제목을 'test'로 설정
pygame.display.set_caption("test") 

# 화면 배경 이미지 로드
background = pygame.image.load("assets/images/background_city1.png")  

# 캐릭터 및 퀘스트 아이템 색상 설정
char_color = (0, 128, 0)  # 캐릭터의 색상을 RGB로 설정 (추후 변경 예정)
item_colors = [(255, 0, 0), (0, 0, 255), (255, 128, 0)]  # 퀘스트 아이템의 색상을 RGB로 설정 (추후 변경 예정)
font = pygame.font.Font(None, 36)  # 글자 폰트 및 크기를 설정

# 캐릭터 속성 설정
char_width = 50  # 캐릭터 너비 설정
char_height = 70  # 캐릭터 높이 설정
char_x = 50  # 캐릭터 x 위치 설정
char_y = screen_height / 2 - char_height / 2  # 캐릭터 y 위치 설정 (화면 중앙)
char_speed = 3  # 캐릭터 이동 속도 설정

# 퀘스트 아이템 속성 설정
item_width = 50  # 퀘스트 아이템 너비 설정
item_height = 70  # 퀘스트 아이템 높이 설정
item_speed = 0.9  # 퀘스트 아이템 이동 속도 설정

# 점수 초기화
score = 0  # 전체 점수 초기화
programming_score = 0  # 프로그래밍 점수 초기화
math_score = 0  # 수학 점수 초기화
mini_game_score = 0  # 미니게임 점수 초기화

# 퀘스트 클래스 정의
class Quest:
    def __init__(self, quest_type, color, x, y, speed):
        # 퀘스트 아이템 속성 초기화
        self.type = quest_type  # 퀘스트 유형
        self.color = color  # 색상
        self.x = x  # x 위치
        self.y = y  # y 위치
        self.speed = speed  # 이동 속도
        self.active = True  # 활성/비활성 상태
        self.collided = False  # 충돌 상태
        self.points = 1  # 점수 (기본 1점)

    def move(self):
        # 퀘스트 아이템 이동 메서드
        if self.active:
            self.x -= self.speed  # 왼쪽으로 이동
            if self.x < -item_width:
                self.active = False  # 화면 왼쪽 밖으로 나가면 비활성화

    def draw(self, screen):
        # 퀘스트 아이템을 화면에 그리는 메서드
        pygame.draw.rect(screen, self.color, (self.x, self.y, item_width, item_height))

# 퀘스트 시작 시 출력하는 함수
def start_quest(quest_type):
    messages = {
        "programming": "프로그래밍 퀘스트",
        "math": "수학 퀘스트",
        "mini_game": "미니게임 퀘스트"
    }
    print(messages.get(quest_type, "알 수 없는 퀘스트"))  # 퀘스트 유형에 따른 메시지 출력

# 캐릭터와 퀘스트 아이템 간의 충돌 확인 함수
def check_collision(char_x, char_y, quest):
    global score, programming_score, math_score, mini_game_score
    # 충돌 조건 확인
    if quest.active and (char_x < quest.x + item_width) and (char_x + char_width > quest.x) and (char_y < quest.y + item_height) and (char_y + char_height > quest.y):
        quest.collided = True  # 충돌 상태로 설정
        start_quest(quest.type)  # 퀘스트 시작
        # 퀘스트 유형에 따른 점수 추가
        if quest.type == "programming":
            programming_score += quest.points
        elif quest.type == "math":
            math_score += quest.points
        else:
            mini_game_score += quest.points

        score += quest.points  # 전체 점수 증가

# 충돌 후 퀘스트 아이템 상태 업데이트 함수
def update_quests(quests):
    for quest in quests:
        if quest.collided or not quest.active:
            quest.x = screen_width + random.randint(50, 300)  # 새 위치 설정
            quest.y = random.randint(0, screen_height - item_height)  # 새 위치 설정
            quest.collided = False  # 충돌 상태 초기화
            quest.active = True  # 활성화 상태로 설정

# 퀘스트 아이템 초기 설정
quests = [
    Quest("programming", item_colors[0], screen_width, random.randint(0, screen_height - item_height), item_speed),
    Quest("math", item_colors[1], screen_width + random.randint(200, 400), random.randint(0, screen_height - item_height), item_speed),
    Quest("mini_game", item_colors[2], screen_width + random.randint(400, 600), random.randint(0, screen_height - item_height), item_speed)
]

# 게임 실행 함수
def main():
    global char_y, score

    running = True  # 게임 실행 상태
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:  # 창 닫기 이벤트 발생 시
                running = False  # 게임 종료

        # 캐릭터 상하 움직임 처리
        keys = pygame.key.get_pressed()  # 눌린 키 확인
        if keys[pygame.K_UP]:
            char_y -= char_speed  # 위로 이동
        if keys[pygame.K_DOWN]:
            char_y += char_speed  # 아래로 이동

        char_y = max(0, min(screen_height - char_height, char_y))  # 화면 경계 처리

        # 배경 및 캐릭터 그리기
        screen.blit(background, (0, 0))  # 배경 그리기
        pygame.draw.rect(screen, char_color, (char_x, char_y, char_width, char_height))  # 캐릭터 그리기

        for quest in quests:  # 각 퀘스트 아이템에 대해
            quest.move()  # 이동
            if quest.active:  # 활성 상태일 때만
                check_collision(char_x, char_y, quest)  # 충돌 확인
                quest.draw(screen)  # 화면에 그리기

        update_quests(quests)  # 퀘스트 상태 업데이트

        # 점수 화면에 표시
        programming_text = font.render(f"Programming Score: {programming_score}", True, (0, 0, 0))
        math_text = font.render(f"Math Score: {math_score}", True, (0, 0, 0))
        mini_game_text = font.render(f"Mini Game Score: {mini_game_score}", True, (0, 0, 0))

        screen.blit(programming_text, (10, 50))
        screen.blit(math_text, (10, 90))
        screen.blit(mini_game_text, (10, 130))

        pygame.display.update()  # 화면 갱신

    pygame.quit()  # 게임 종료

if __name__ == "__main__":
    main()  # 메인 함수 실행

남은거

퀘스트 미니게임이나 수학 문제들 나오는 팝업창 구현해야함

게임시간 정해놓고 시간 지나면 엔딩 로직으로 엔딩 나오게 해야됨

profile
개인 공부용 / 포트폴리오

0개의 댓글