강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của zxvzxc65312457
zxvzxc65312457

câu hỏi đã được viết

Bài giảng Python miễn phí (Cách sử dụng Phần 1) - Tạo game arcade hoài cổ (3 giờ)

Xử lý va chạm

실행이 안됩니다.

Viết

·

311

0

작성한 코드

import pygame

pygame.init() # 초기화 (반드시 필요)

# 화면 크기 설정 
screen_width = 480 # 가로 크기
screen_height = 640 # 세로 크기
screen = pygame.display.set_mode((screen_widthscreen_height)) 

# 화면 타이틀 설정
pygame.display.set_caption("dddd"# 게임 이름

# FPS
clock = pygame.time.Clock()

# 배경 이미지 불러오기
background = pygame.image.load("C:/Users/zxc65/OneDrive/바탕 화면/PythonWorkspace/pygame_basic/background.png")

# 캐릭터(스프라이트) 불러오기
character =  pygame.image.load("C:/Users/zxc65/OneDrive/바탕 화면/PythonWorkspace/pygame_basic/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  # 화면 세로 크기 가장 아래에 해당하는 곳에 위치 (세로)

# 이동할 좌표
to_x = 0
to_y = 0

# 이동 속도
character_speed = 0.6

# 적 캐릭터
enemy =  pygame.image.load("C:/Users/zxc65/OneDrive/바탕 화면/PythonWorkspace/pygame_basic/enemy.png")
enemy_size = enemy.get_rect().size # 이미지의 크기를 구해옴 
enemy_width = enemy_size[0# 캐릭터의 가로 크기
enemy_height = enemy_size[1# 캐릭터의 세로 크기
enemy_x_pos = (screen_width / 2) - (enemy_width / 2# 화면 가로의 절반 크기에 해당하는 곳에 위치 (가로)
enemy_y_pos = (screen_height / 2) - (enemy_height / 2)  # 화면 세로 크기 가장 아래에 해당하는 곳에 위치 (세로)


# 이벤트 루프
running = True # 게임이 진행중인가?
while running:
    dt = clock.tick(60# 게임화면의 초당 프레임 수를 설정 
   
    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 # to_x = to_x - 5
            elif event.key == pygame.K_RIGHT# 캐릭터를 오른쪽으로 
                to_x += character_speed
            elif event.key == pygame.K_UP# 캐릭터를 위로
                to_y -= character_speed
            elif event.key == pygame.K_DOWN# 캐릭터를 아래로
                to_y += character_speed

        if event.type == pygame.KEYUP# 방향키를 때면 멈춤
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                to_x = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                to_y = 0
    
    character_x_pos += to_x * dt
    character_y_pos += to_y * dt

    # 가로 경계값 처리 
    if character_x_pos < 0:
        character_x_pos = 0
    elif character_x_pos > screen_width - character_width:
        character_x_pos = screen_width - character_width

    # 세로 경계값 처리
    if character_y_pos < 0:
        character_y_pos = 0
    elif character_y_pos > screen_height - character_height:
        character_y_pos = screen_height - character_height

    # 충돌 처리를 위한 rect 정보 업데이트
    character_rect = character.get_rect()
    character_rect.left = character_x_pos
    character_rect.top = character_y_pos

    enemy_rect = enemy.get.rect()
    # enemy_rect.left = enemy_x_pos
    # enemy_rect.top = enemy_y_pos

    # 충돌 체크
    if character_rect.colliderect(enemy_rect):
        print("충돌했어요")
        running = False

    screen.blit(background, (00))  # 배경 그리기
    screen.blit(character, (character_x_poscharacter_y_pos)) # 캐릭터 그리기
    screen.blit(enemy, (enemy_x_posenemy_y_pos)) # 적 그리기

    pygame.display.update() # 게임화면을 다시 그리기

    

# pygame 종료
pygame.quit()
터미널 출력
File "c:/Users/zxc65/OneDrive/바탕 화면/PythonWorkspace/pygame_basic/6_collision.py", line 88, in <module> enemy_rect = enemy.get.rect() AttributeError: 'pygame.Surface' object has no attribute 'get' PS C:\Users\zxc65\OneDrive\바탕 화면\PythonWorkspace>
싱행을 하면 꺼집니다 5.frame per second py는 작동 잘됩니다.
틀린 부분이 있으면 지적 부탁드립니다. 또는 자체의 문제라면 이 진도까지의
파일 부탁드립니다.
email: dbxbqm6531@gmail.com
(영어로 한글 자판 그대로 유튜브6531@gmail.com)
실행GUIpygamepython

Câu trả lời

Câu hỏi này đang chờ câu trả lời
Hãy là người đầu tiên trả lời!
Hình ảnh hồ sơ của zxvzxc65312457
zxvzxc65312457

câu hỏi đã được viết

Đặt câu hỏi