머신러닝

본문 바로가기
사이트 내 전체검색


머신러닝
머신러닝

3. pygame 이벤트 예제

페이지 정보

작성자 관리자 댓글 0건 조회 1,047회 작성일 21-02-15 20:58

본문

3. pygame 이벤트 예제

이벤트 목록   


pygame을 사용할 때 pygame 자체적으로 제공하는 Event 목록을 적절하게 이용하게 되면, 훨씬 게임을 유용하게 코딩할 수 있게 됩니다.


이벤트는 마우스, 키보드, 조이스틱, 디스플레이 및 기타 여러 가지 행동에 대한 상태 정보를 인지하여 좀 더 유용한 코딩을 가능하게 합니다.


먼저 이벤트 목록에 대해 알아보겠습니다.


1.PNG



이벤트 예제 


# Import a library of functions called 'pygame'

import pygame

 

# Initialize the game engine

pygame.init()

 

# Define the colors we will use in RGB format

BLACK= ( 0,  0,  0)

WHITE= (255,255,255)

BLUE = ( 0,  0,255)

GREEN= ( 0,255,  0)

RED  = (255,  0,  0)

 

# Set the height and width of the screen

size  = [400,300]

screen= pygame.display.set_mode(size)

font= pygame.font.SysFont("consolas",20)

 

pygame.display.set_caption("Game Title")

  

#Loop until the user clicks the close button.

done = False

flag = True

clock= pygame.time.Clock()

 

while not done:

 

    # This limits the while loop to a max of 10 times per second.

    # Leave this out and we will use all CPU we can.

    clock.tick(10)

     

    # Main Event Loop

    for event in pygame.event.get():# User did something

        if event.type == pygame.ACTIVEEVENT:# If user's mouse in the display or out.

            flag ^= True

        elif event.type == pygame.QUIT:

            done = True                     # If user clicked close.

 

  

    # All drawing code happens after the for loop and but

    # inside the main while done==False loop.

      

    # Clear the screen and set the screen background

    screen.fill(WHITE)

 

flag라는 변수가 True혹은 False로 바뀌었을 때 출력되도록 설정한 코드입니다.

마우스가 밖으로 나가거나 화면 안으로 들어올 때마다 True 혹은 False로 바뀌도록 하였습니다.


    # Drawing House If User Mouse Is In Display. 

    if flag== True:

        pygame.draw.polygon(screen, GREEN, [[30,150], [125,100], [220,150]],5)

        pygame.draw.polygon(screen, GREEN, [[30,150], [125,100], [220,150]],0)

        pygame.draw.lines(screen, RED,False, [[50,150], [50,250], [200,250], [200,150]],5)

        pygame.draw.rect(screen, BLACK, [75,175,75,50],5)

        pygame.draw.rect(screen, BLUE, [75,175,75,50],0)

        pygame.draw.line(screen, BLACK, [112,175], [112,225],5)

        pygame.draw.line(screen, BLACK, [75,200], [150,200],5)

 

    # Print Message If User Mouse Is In Outside.

    else:

        textSurface     = font.render('Mouse is outside!!',True, pygame.Color('BLACK'),None)

        textRect        = textSurface.get_rect()

        textRect.topleft= (50,50)


화면에 글씨가 출력될 수 있도록 하는 코드입니다. 

  

        screen.blit(textSurface, textRect)

 

    # Go ahead and update the screen with what we've drawn.

    # This MUST happen after all the other drawing commands.

    pygame.display.flip()

  

# Be IDLE friendly

pygame.quit()




2.PNG


3.PNG


댓글목록

등록된 댓글이 없습니다.


개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 063-469-4551 FAX. 063-469-4560 전북 군산시 대학로 558
군산대학교 컴퓨터정보공학과

Copyright © www.leelab.co.kr. All rights reserved.