python_exercise_02_prank_on_friends (challenge not completed)

루시Soo의 우와한 서재·2022년 10월 16일
0

Toy_Projects

목록 보기
2/2

프로젝트 목적
Raspberry Pi Foundation에서 제공하는 토이프로젝트 중 'spot the difference'를 구현함

<해당프로젝트 링크>
https://projects.raspberrypi.org/en/projects/scary-spot-the-difference/0
⬆️ 링크에서 소개해주는 전체 코드 현재 사운드관련 error있음 주의

prank on friends

전체코드 without 설명

import pygame
from time import sleep
from random import randrange

pygame.init()

width = pygame.display.Info().current_w
height = pygame.display.Info().current_h

screen = pygame.display.set_mode((width, height))

difference = pygame.image.load('spot_the_diff.png')
difference = pygame.transform.scale(difference, (width, height))

zombie = pygame.image.load('scary_face.png')
zombie = pygame.transform.scale(zombie, (width, height))

scream = pygame.mixer.Sound("scream.wav")

screen.blit(difference, (0, 0))
pygame.display.update()

pygame.time.delay(randrange(5000,15000))

scream.play()

screen.blit(zombie, (0,0))
pygame.display.update()

pygame.time.delay(3000)

scream.stop()

pygame.quit()

전체코드 with 설명

# pygame library <- Command Prompt로 pygame이 내컴퓨터에 설치되어있는지 먼저 확인이 필요. 
# 설치되어 있지 않으면 https://www.pygame.org/docs/ 를 참조하여 다운받자
import pygame
# The "from" instruction works differently to the "import" instruction, allowing you to inport only the parts of a libaray you need rather than the whole libray. 
from time import sleep
from random import randrange

# pygame을 초기화해주는 코드
pygame.init()

# pygame.display <- pygame module to control the display window and screen
#아래 두줄의 코드로 해당 코드가 작동되는 모니터의 width와 height에 정보를 알 수 있다. 
width = pygame.display.Info().current_w
height = pygame.display.Info().current_h

# Initialize a window or screen for display / 모니터의 width와 height에 맞는 화면을 만들어주는 코드라고 생각하기
screen = pygame.display.set_mode((width, height))

#위에서 만든 화면에 사용될 이미지파일을 컴퓨터 메모리에 저장시켜주는 코드
difference = pygame.image.load('spot_the_diff.png')
# 위의 이미지가 사용될 때(즉, 스크린에 나타날 때) 모니터의 width와 height에 맞게 display해주기 위해 sclae를 조절하는 코드
difference = pygame.transform.scale(difference, (width, height))

zombie = pygame.image.load('scary_face.png')
zombie = pygame.transform.scale(zombie, (width, height))

#prank에 사용될 좀비이미지는 사운드효과와 같이 rendernig되는데, 이때 사용할 sound를 컴퓨터 메모리에 저장시켜주는 코드
scream = pygame.mixer.Sound("scream.wav")

# 여기까지해서 이 위에 위치한 코드들은 화면에 rendering하기 전해야하는 필수 작업들이라고 생각

#내컴퓨터 메모리에 저장된 difference이미지를 화면에 rendering 시키라고 명령하는 코드 
screen.blit(difference, (0, 0)) # copies the image onto the screen, starting at the top left corner
pygame.display.update() # tell Pygame to redraw the screen

#두번째 prank이미지가 화면에 rendering될 때까지 randrange()를 이용해서 5초에서 15초 사이에 랜덤하게 pause
pygame.time.delay(randrange(5000,15000))

# 메모리에 저장했던 사운드트랙을 play
scream.play()

#사운드트랙과 동시에 zombie이미지가 화면에 약 3초동안 rendering
screen.blit(zombie, (0,0))
pygame.display.update()

pygame.time.delay(3000)

# 3초가 지나면 사운트트랙 종료되면서 바로 프로그램도 종료
scream.stop()

pygame.quit()

💡How to display images with PyGame ?

There are four basic steps to displaying images on the pygame window :

  1. Create a display surface object using display.set_mode() method of pygame.

  2. Create a Image surface object i.e. surface object in which image is drawn on it, using image.load() method of pygame.

  3. Copy the image surface object to the display surface object using blit() method of pygame display surface object.

  4. Show the display surface object on the pygame window using display.update() method of pygame.


Challenge: take it further

  • Have a look at the Pygame website to find out how to detect mouse clicks from your victim so they can click on differences.

  • Make the game keep a fake score of differences spotted before the scary face appears.

profile
그냥 끄적끄적 공부 정리 블로그

0개의 댓글