72
418512 ภภภภภภภภภภภ ภภภภภภภภภภภ Pygame ภภภภภภ ภภภภภภภ [email protected]

418512 ภาษาโปรแกรมคอมพิวเตอร์ Pygame

  • Upload
    lavi

  • View
    89

  • Download
    0

Embed Size (px)

DESCRIPTION

418512 ภาษาโปรแกรมคอมพิวเตอร์ Pygame. ประมุข ขันเงิน [email protected]. Pygame. เวบไซต์ : http://www.pygame.org ไลบรารีภาษาไพทอนสำหรับเขียนเกม เป็นไลบรารีที่เขียนมาหุ้มไลบรารีอีกตัวชื่อ SDL (Simple Direct Media Layer) ซึ่งใช้ติดต่อกับ การ์ดเสียง การ์ดจอ คียบอร์ด , เมาส์ , จอยสติ๊ก - PowerPoint PPT Presentation

Citation preview

Page 1: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

418512 ภาษาโปรแกรมคอมพิ วเตอร�

Pygameประม�ข ข�นเงิ น

[email protected]

Page 2: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

Pygame

• เวบไซต�: http://www.pygame.org• ไลบราร�ภาษาไพิทอนสำ าหร�บเข�ยนเกม• เป#นไลบราร�ท�$เข�ยนมาห�%มไลบราร�อ�กต�วชื่'$อ SDL

(Simple Direct Media Layer) ซ($งิใชื่%ต ดต+อก�บ– การ�ดเสำ�ยงิ– การ�ดจอ– ค�ยบอร�ด, เมาสำ�, จอยสำต -ก– นาฬิ กา

Page 3: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

Pygame

• ชื่+วยท าให%การเข�ยนเกมงิ+ายข(/น• แต+ก0ย�งิต%องิเข�ยนมากพิอสำมควร– ไม+ม� collition detection แบบละเอ�ยด– ไม+ม� physics engine– ไม+ม� game engine ระด�บสำ1งิ– ถ้%าจะท า game 3D ก0ต%องิเข�ยน OpenGL เองิ

Page 4: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

การต ดต�/งิ• ดาวน�โหลดไฟล�ต ดต�/งิจาก

http://www.pygame.org/download.shtml• ระว�งิด1 version ของิ Python ท�$ค�ณใชื่%ให%ถ้1กก�บไฟล�

ท�$ดาวน�โหลดมาด%วย• คนท�$อยากเข�ยนโปรแกรมเกม 3D ให%ไปดาวน�โหลด

PyOpenGL จาก http://pyopengl.sourceforge.net มาด%วย

Page 5: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมแรกbackground_image_filename = 'kagami-wallpaper.jpg'

mouse_image_filename = 'kagami.png'

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen = pygame.display.set_mode((800,600), 0, 32)

pygame.display.set_caption("Hello, world!")

background = pygame.image.load(background_image_filename).convert()

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

screen.blit(background, (0,0))

x,y = pygame.mouse.get_pos()

x -= mouse_cursor.get_width() / 2

y -= mouse_cursor.get_height() / 2

screen.blit(mouse_cursor, (x,y))

pygame.display.update()

ลอกมาจาก Will McGugan, Beginnning Game Development with Python and Pygame

Page 6: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมแรก

Page 7: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมแรกbackground_image_filename = 'kagami-wallpaper.jpg'

mouse_image_filename = 'kagami.png'

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen = pygame.display.set_mode((800,600), 0, 32)

pygame.display.set_caption("Hello, world!")

background = pygame.image.load(background_image_filename).convert()

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

screen.blit(background, (0,0))

x,y = pygame.mouse.get_pos()

x -= mouse_cursor.get_width() / 2

y -= mouse_cursor.get_height() / 2

screen.blit(mouse_cursor, (x,y))

pygame.display.update()

ท าการ import โมด1ลต+างิๆ ท�$เราจะใชื่%

Page 8: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมแรก• import pygame

from pygame.locals import *– สำองิบรรท�ดน�/ import ฟ6งิก�ชื่�นและค+าคงิท�$ต+างิๆ ของิ

pygame

• from sys import exit– ฟ6งิก�ชื่�น exit เม'$อเร�ยกแล%วโปรแกรมจะเล กท างิาน

Page 9: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมแรกbackground_image_filename = 'kagami-wallpaper.jpg'

mouse_image_filename = 'kagami.png'

import pygame

from pygame.locals import *

from sys import exit

pygame.init()screen = pygame.display.set_mode((800,600), 0, 32)

pygame.display.set_caption("Hello, world!")

background = pygame.image.load(background_image_filename).convert()

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

screen.blit(background, (0,0))

x,y = pygame.mouse.get_pos()

x -= mouse_cursor.get_width() / 2

y -= mouse_cursor.get_height() / 2

screen.blit(mouse_cursor, (x,y))

pygame.display.update()

เร�ยกเพิ'$อให% pygame ก าหนดค+าเร $มต%นต+างิๆ ของิต�วเองิ

Page 10: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมแรกbackground_image_filename = 'kagami-wallpaper.jpg'

mouse_image_filename = 'kagami.png'

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen = pygame.display.set_mode((800,600), 0, 32)

pygame.display.set_caption("Hello, world!")

background = pygame.image.load(background_image_filename).convert()

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

screen.blit(background, (0,0))

x,y = pygame.mouse.get_pos()

x -= mouse_cursor.get_width() / 2

y -= mouse_cursor.get_height() / 2

screen.blit(mouse_cursor, (x,y))

pygame.display.update()

ก าหนดขนาดเร $มต%นและไตเต /ลของิหน%าต+างิ

Page 11: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

pygame.display.set_mode

• สำร%างิ object ชื่น ด Surface ท�$แทนพิ'/นท�$ของิหน%าต+างิท�/งิหมด– Surface เป#น class ของิ object ท�$ใชื่%เก0บร1ปต+างิๆ

• ร�บ argument สำามต�ว– ต�วแรกเป#น tuple (w,h) โดย w = ความกว%างิ, h =

ความสำ1งิ หน+วยเป#นพิ กเซล– ต�วท�$สำองิเป#น flag ท�$ใชื่%ก าหนดชื่น ดของิหน%าต+างิ– ต�วสำ�ดท%ายเป#นจ านวนบ ตของิแต+ละพิ กเซลในหน%าต+างิ

ปกต แล%วเราจะเซตเป#น 32 เสำมอ

Page 12: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

Flag ของิ argument ต�วท�$สำองิFlag ความหมายFULLSCREEN หน%าต+างิเต มหน%าจอDOUBLEBUF ท า double buffer

HWSURFACE ใชื่%หน+วยความจ าในการ�ดจอเก0บสำ�ของิ pixel ใน surface (ต%องิใชื่%ร+วมก�บ FULLSCREEN เท+าน�/น)

OPENGL ท าให% OpenGL สำามารถ้เข�ยน pixel ลงิใน surface น�/ได%

RESIZABLE ท าให%หน%าต+างิย+อขยายขนาดได%NOFRAME ท าให%หน%าต+างิไม+ม�ขอบ

Page 13: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

จ านวนบ ตของิพิ กเซลท�$น ยมก าหนดก�น

จำ�านวนบิ�ต จำ�านวนสี�8 256 สำ� ซ($งิสำามารถ้เล'อกมาได%จากจานสำ�ท�$ใหญ่+

กว+า15 32,768

16 65,536

24 16.7 ล%านสำ�32 16.7 ล%านสำ� ม�อ�ก 1 ไบต�ไว%เก0บ alpha (ความ

โปร+งิแสำดงิ)

Page 14: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

ภาพิ• ตารางิสำ�$เหล�$ยมผื'นผื%า แต+ละชื่+องิม�สำ�หน($งิสำ�• แต+ละชื่+องิเร�ยกว+า พิ กเซล (pixel)

http://en.wikipedia.org/wiki/Pixels

Page 15: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

สำ�• สำ� = tuple ม�สำมาชื่ กเป#นเลข 3 ต�ว (R, G, B) – R บอกระด�บความเข%มของิแสำงิสำ�แดงิ– G บอกระด�บความเข%มของิแสำงิสำ�เข�ยว– B บอกระด�บความเข%มของิแสำงิสำ�น /าเงิ น

• เลขแต+ละต�วม�ค+าต�/งิแต+ 0 ถ้(งิ 2จ านวนบ ตท�$ใชื่%เก0บความเข%มแต+ละ

สำ�-1– สำ+วนใหญ่+เราจะใชื่%พิ'/นท�$ 32 บ ตเก0บ 1 พิ กเซล– แต+ละสำ�ได% 8 บ ต– ด�งิน�/นค+าสำ1งิสำ�ดค'อ 255

Page 16: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

Trichromatic Theory of Vision

• สำ�ท�$มน�ษย�มองิเห0นแบ+งิออกเป#นสำามสำ+วน – แดงิ เข�ยว น /าเงิ น – ประสำาทสำ�มผื�สำของิมน�ษย�ของิแต+ละสำ�เป#นอ สำระจากก�น – สำ�อ'$นๆ เก ดจาก การน าสำ�ท� /งิสำามน�/มาประกอบก�น

• หล�กฐาน– เซลล�โคนในเรต นาม�สำามชื่น ด– แต+ละชื่น ดไวต+อ สำ�แดงิ สำ�เข�ยว สำ�น /าเงิ น ตามล าด�บ

Page 17: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

สำ�หล�กๆ(255,0,0) (0,255,0)

(0,0,255)

(255,255,0)

(255,0,255) (0,255,255)

(255,255,255)

(0,0,0)

Page 18: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

pygame.display.set_caption

• ก าหนดไตเต /ลของิหน%าต+างิ• ให%อาร�ก วเมนต�ต�วเด�ยว ค'อ string ท�$จะให%เป#นไต

เต /ล• ยกต�วอย+างิเชื่+น

pygame.display_set_caption(“Hello, world!”)

Page 19: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมแรกbackground_image_filename = 'kagami-wallpaper.jpg'

mouse_image_filename = 'kagami.png'

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen = pygame.display.set_mode((800,600), 0, 32)

pygame.display.set_caption("Hello, world!")

background = pygame.image.load(background_image_filename).convert()

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

screen.blit(background, (0,0))

x,y = pygame.mouse.get_pos()

x -= mouse_cursor.get_width() / 2

y -= mouse_cursor.get_height() / 2

screen.blit(mouse_cursor, (x,y))

pygame.display.update()

ลอกมาจาก Will McGugan, Beginnning Game Developmentwith Python and Pygame

โหลดร1ป background และร1ปท�$จะใชื่%เป#น mouse cursor

Page 20: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

pygame.image.load• สำร%างิ object ชื่น ด surface โดยการโหลดร1ปข(/นมาจาก file• ให% argument เป#นชื่'$อไฟล�• ชื่น ดของิไฟล�ท�$ก าหนดได%– JPG– PNG– GIF (non animated)– BMP– PCX– TGA (uncompressed)– TIF– LBM (and PBM)– PBM (and PGM, PPM)– XPM

Page 21: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

pygame.Surface.convert

• เป#น method ของิ object ชื่น ด Surface• สำร%างิ object ชื่น ด Surface อ�กอ�นท�$ม�ร1ปแบบการ

เก0บพิ กเซลเหม'อนก�บ Surface ของิหน%าจอ

Page 22: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

pygame.Surface.convert_alpha

• ท าหน%าท�$เหม'อน pygame.Surface.convert แต+ใชื่%ในกรณ�ท�$ร1ปใน Surface ม�ข%อม1ลความโปร+งิใสำ (alpha channel)

• ร1ปแบบไฟล�ท�$ม�ข%อม1ลความโปรงิใสำได%ค'อ GIF และ PNG

• kagami.png ม�ความโปร+งิใสำ เราจ(งิใชื่% convert_alpha• Kagami_wallpaper.jpg ไม+ม�ความโปร+งิใสำ (เพิราะม�น

เป#นร1ป JPEG) เราจ(งิใชื่% convert เฉยๆ

Page 23: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame
Page 24: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมแรกbackground_image_filename = 'kagami-wallpaper.jpg'

mouse_image_filename = 'kagami.png'

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen = pygame.display.set_mode((800,600), 0, 32)

pygame.display.set_caption("Hello, world!")

background = pygame.image.load(background_image_filename).convert()

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

screen.blit(background, (0,0))

x,y = pygame.mouse.get_pos()

x -= mouse_cursor.get_width() / 2

y -= mouse_cursor.get_height() / 2

screen.blit(mouse_cursor, (x,y))

pygame.display.update()

initialization

Page 25: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมแรกbackground_image_filename = 'kagami-wallpaper.jpg'

mouse_image_filename = 'kagami.png'

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen = pygame.display.set_mode((800,600), 0, 32)

pygame.display.set_caption("Hello, world!")

background = pygame.image.load(background_image_filename).convert()

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

screen.blit(background, (0,0))

x,y = pygame.mouse.get_pos()

x -= mouse_cursor.get_width() / 2

y -= mouse_cursor.get_height() / 2

screen.blit(mouse_cursor, (x,y))

pygame.display.update()

player input

Page 26: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมแรกbackground_image_filename = 'kagami-wallpaper.jpg'

mouse_image_filename = 'kagami.png'

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen = pygame.display.set_mode((800,600), 0, 32)

pygame.display.set_caption("Hello, world!")

background = pygame.image.load(background_image_filename).convert()

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

screen.blit(background, (0,0))

x,y = pygame.mouse.get_pos()

x -= mouse_cursor.get_width() / 2

y -= mouse_cursor.get_height() / 2

screen.blit(mouse_cursor, (x,y))

pygame.display.update()

update display

Page 27: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

เข�ยน Game Loop ใน Pygame

• สำร%างิ loop while ท�$ไม+ม�ว�นจบwhile True:

• ร�บ input ด%วยการด�กจ�บ Event– จะพิ1ดถ้(งิในหน%าต+อไป

• ท า Game Logic• วาดร1ปหน%าจอ• เสำร0จแล%วเร�ยก pygame.display.update()

Page 28: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

Event

• เหต�การณ�ต+างิๆ ท�$เก ดข(/นท�$โปรแกรมสำามารถ้เล'อกด�กจ�บและตอบสำนองิได%

• ต�วอย+างิ– การกดแป;นพิ มพิ�– การคล กเมาสำ�– การท�$ผื1%ใชื่%เล'อกป<ดหน%าต+างิหร'อกด Alt+F4– ฯลฯ

• ใน pygame จะแทน Event ด%วย object ประเภท pygame.event.Event

Page 29: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

Event Loopbackground_image_filename = 'kagami-wallpaper.jpg'

mouse_image_filename = 'kagami.png'

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen = pygame.display.set_mode((800,600), 0, 32)

pygame.display.set_caption("Hello, world!")

background = pygame.image.load(background_image_filename).convert()

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

screen.blit(background, (0,0))

x,y = pygame.mouse.get_pos()

x -= mouse_cursor.get_width() / 2

y -= mouse_cursor.get_height() / 2

screen.blit(mouse_cursor, (x,y))

pygame.display.update()

ด(งิ event ท�$เพิ $งิเก ดข(/นในล1ปรอบท�$ผื+านมามาประมวลผืล

Page 30: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

pygame.event.get

• ปกต เวลาม� event ใหม+เก ดข(/นม�นจะถ้1กในไปเก0บไว%ใน queue ของิ pygame เองิ

• pygame.event.get ม�ไว%ด(งิ event ท�/งิหมดท�$ค%างิใน queue ออกมาแล%วค'นค+าเป#น list ของิ object ประเภท pygame.event.Event

• ด�งิน�/นถ้%าต%องิการประมวลผืล event ท�ละต�วจะต%องิใสำ+ for loop บน pygame.event.get

Page 31: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

Event Loop ในโปรแกรมแรกfor event in pygame.event.get():

if event.type == QUIT: exit()

• Object ประเภท pygame.event.Event ท�กต�วจะม� attribute “type” ซ($งิเป#นต�วเลขบอกว+าม�นเป#น event แบบใด

• ใน loop ข%างิบนเราเชื่0คหา event ประเภท QUIT ซ($งิจะปรากฏเม'$อผื1%ใชื่%ป<ดหน%าต+างิหร'อกด Alt+F4

• ถ้%า event ประเภทน�/ปรากฏข(/นเราก0จะให%โปรแกรมเล กการท างิานโดยการเร�ยก exit()

Page 32: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมแรกbackground_image_filename = 'kagami-wallpaper.jpg'

mouse_image_filename = 'kagami.png'

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen = pygame.display.set_mode((800,600), 0, 32)

pygame.display.set_caption("Hello, world!")

background = pygame.image.load(background_image_filename).convert()

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

screen.blit(background, (0,0)) x,y = pygame.mouse.get_pos()

x -= mouse_cursor.get_width() / 2

y -= mouse_cursor.get_height() / 2

screen.blit(mouse_cursor, (x,y))

pygame.display.update()

วาดร1ป background

Page 33: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

pygame.Surface.blit

• blit = block image transfrer– หมายความถ้(งิการก0อปป?/ ภาพิหน($งิไปย�งิอ�กภาพิหน($งิ– blit เป#น operation ท�$ม�ความสำ าค�ญ่ท�$สำ�ด – แค+ใชื่% blit อย+างิเด�ยวค�ณก0สำามารถ้วาดภาพิได%เก'อบท�ก

อย+างิแล%ว• ร1ปแบบ

dest.blit(source, (x,y))เม'$อ– dest ค'อ pygame.Surface ท�$ท าหน%าท�$เป#นกระดาษ– source ค'อ pygame.Surface ท�$เก0บภาพิท�$ต%องิการวาด– (x,y) เป#น tuple ของิต าแหน+งิบนภาพิ dest ท�$ต%องิการให%ม�ม

บนขวาของิภาพิ source ไปปรากฎ

Page 34: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

ระบบ coordinate

• pygame.Surface เป#นพิ'/นท�$เก0บร1ปท�$เป#นสำ�$เหล�$ยมผื'นผื%า

• เราว�ดต าแหน+งิด%วยหน+วยพิ กเซล• (0,0) ค'อต าแหน+งิม�มบนซ%ายของิร1ป• (w, h) ค'อต าแหน+งิท�$อย1+ท�$ม�มล+างิขวาของิร1ป เม'$อ

w = ความกว%างิ และ h = ความสำ1งิ ของิร1ป

Page 35: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

ระบบ coordinate(0,0)

(w,h)

Page 36: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมแรกbackground_image_filename = 'kagami-wallpaper.jpg'

mouse_image_filename = 'kagami.png'

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen = pygame.display.set_mode((800,600), 0, 32)

pygame.display.set_caption("Hello, world!")

background = pygame.image.load(background_image_filename).convert()

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

screen.blit(background, (0,0))

x,y = pygame.mouse.get_pos()

x -= mouse_cursor.get_width() / 2

y -= mouse_cursor.get_height() / 2

screen.blit(mouse_cursor, (x,y))

pygame.display.update()

ค านวณต าแหน+งิและวาด mouse cursor

Page 37: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

pygame.moust.get_pos

• ค'น tuple (x,y) ซ($งิเป#นต าแหน+งิของิเมาสำ�บนหน%าต+างิ

• ใชื่%ระบบ coordinate ท�$กล+าวถ้(งิเม'$อตะก�/น�/

Page 38: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

pygame.Surface.get_???

• pygame.Surface.get_size()– ค'นค1+ล าด�บ (w,h) โดย w = ความกว%างิ และ h = ความ

สำ1งิ ของิ surface

• pygame.Surface.get_width()– ค'นความกว%างิของิ surface

• pygame.Surface.get_height()– ค'นความสำ1งิของิ surface

Page 39: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

การประมวลผืล Event

• ม�ค าสำ�$งิในการด(งิ event ออกจาก queue นอกจาก pygame.event.get อ�กสำองิสำามค าสำ�$งิ

• pygame.event.poll()– ด(งิ event ออกมาจาก queue แค+ event เด�ยว– ถ้%าไม+ม�จะได% pygame.NOEVENT ออกมา

• pygame.event.wait()– ด(งิ event ออกมาจาก queue แค+ event เด�ยว– ถ้%าไม+ม�จะรอจนกระท�$งิม� event มา

Page 40: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมพิ มพิ� event ต+างิๆimport pygamefrom pygame.locals import *from sys import exit

pygame.init()SCREEN_SIZE = (800,600)screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)

font = pygame.font.SysFont("arial", 16)font_height = font.get_linesize()event_text = []

while True: event = pygame.event.wait() event_text.append(str(event)) event_text = event_text[-SCREEN_SIZE[1]/font_height:] if event.type == QUIT: exit() screen.fill((255,255,255)) y = SCREEN_SIZE[1] - font_height for text in reversed(event_text): screen.blit(font.render(text, True, (0,0,0)), (0,y)) y -= font_height pygame.display.update()

ลอกมาจาก Will McGugan, Beginnning Game Development with Python and Pygame

Page 41: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมพิ มพิ� event ต+างิๆimport pygamefrom pygame.locals import *from sys import exit

pygame.init()SCREEN_SIZE = (800,600)screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)

font = pygame.font.SysFont("arial", 16)font_height = font.get_linesize()event_text = []

while True:

event = pygame.event.wait() event_text.append(str(event)) event_text = event_text[-SCREEN_SIZE[1]/font_height:] if event.type == QUIT: exit() screen.fill((255,255,255)) y = SCREEN_SIZE[1] - font_height for text in reversed(event_text): screen.blit(font.render(text, True, (0,0,0)), (0,y)) y -= font_height pygame.display.update()

ลอกมาจาก Will McGugan, Beginnning Game Development with Python and Pygame

รอจนกระท�$งิม� event หน($งิ event แล%วด(งิ event น�/นออกจาก queue

Page 42: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมพิ มพิ� event ต+างิๆ

Page 43: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โครงิสำร%างิของิ pygame.event.Event

• ม� attribute “type” เก0บชื่น ดของิ event ไว%• นอกจากน�/นอาจม� attribute อ'$นๆ ตามแต+ชื่น ด

ของิ event– เชื่+น ถ้%า type ม�ค+าเท+าก�บ KEYDOWN– จะม� attribute• unicode เก0บรห�สำ unicode ของิป�Aมท�$กด• key เก0บรห�สำของิป�Aมท�$กด• mod เก0บป�Aมอ'$นท�$กดด%วย เชื่+น shift, ctrl, หร'อ alt

Page 44: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

Event ต+างิๆ ใน Pygameชน�ดของ event ความหมาย Attribute อ��นๆQUIT ผื1%ใชื่%กดป�Aมป<ดหน%าต+างิ ไม+ม�ACTIVEEVENT หน%าต+างิ pygame ถ้1กซ+อน

หร'อถ้1กเป<ดgain, state

KEYDOWN ผื1%ใชื่%กดป�Aม keyboard unicode, key, mod

KEYUP ผื1%ใชื่%ปล+อยป�Aม keyboard key, mod

MOUSEMOTION ผื1%ใชื่%เล'$อน mouse pos, rel, button

MOUSEBUTTONDOWN ผื1%ใชื่%กดป�Aม mouse pos, button

MOUSEBUTTONUP ผื1%ใชื่%ปล+อยป�Aม mouse pos, button

VIDEORESIZE หน%าต+างิถ้1กเปล�$ยนขนาด size, w, h

VIDEOEXPOSE หน%าต+างิหร'อบางิสำ+วนของิหน%าต+างิไม+ถ้1กบ�งิ

ไม+ม�

USEREVENT Event ท�$ผื1%ใชื่%ก าหนดเองิ code

Page 45: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

Keyboard Events

• KEYDOWN– ม� attribute 3 ต�ว: unicode, key, และ mod– key เป#นเลขค+าคงิท�$ซ($งิแทนป�Aมท�$ผื1%ใชื่%กด• ค+าน�/ import มาจาก pygame.locals

– mod เป#น flag ซ($งิแสำดงิว+าผื1%ใชื่%กดป�Aม Ctrl, Alt, หร'อ Shift หร'อไม+• Flag ท�/งิสำามค'อ KMOD_SHIFT, KMOD_ALT, KMOD_CTRL• เวลาเชื่0คใชื่% bitwise-AND (&)

– unicode เป#นรห�สำ unicode ของิป�Aมท�$กด

Page 46: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

Keyboard Events

• KEYUP– ม� attribute 2 ต�วค'อ: key, mod– key และ mod เหม'อน KEYDOWN

Page 47: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมเล'$อนต�วการ�ต1นด%วย keyboard

Page 48: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมเล'$อนต�วการ�ต1นด%วย keyboard

background_image_filename = 'yuuno-background.jpg'

mouse_image_filename = 'yuuno.png'

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen_size = (800, 524)

screen = pygame.display.set_mode(screen_size, 0, 32)

pygame.display.set_caption("Hello, Yuuno!")

background = pygame.image.load(background_image_filename).convert()

cursor = pygame.image.load(mouse_image_filename).convert_alpha()

cursor_x = (screen_size[0] - cursor.get_width()) / 2

cursor_y = (screen_size[1] - cursor.get_height()) / 2

hold = {K_LEFT: False, K_RIGHT: False, K_UP: False, K_DOWN: False}

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

if event.type == KEYDOWN:

if event.key in [K_LEFT, K_RIGHT, K_UP, K_DOWN]:

hold[event.key] = True

if event.type == KEYUP:

if event.key in [K_LEFT, K_RIGHT, K_UP, K_DOWN]:

hold[event.key] = False

dx, dy = 0, 0 if hold[K_LEFT]: dx = -1 if hold[K_RIGHT]: dx = 1 if hold[K_UP]: dy = -1 if hold[K_DOWN]: dy = 1

cursor_x += dx cursor_y += dy

if cursor_x < 0: cursor_x = 0 if cursor_x > screen_size[0]-cursor.get_width(): cursor_x = screen_size[0]-cursor.get_width() if cursor_y < 0: cursor_y = 0 if cursor_y > screen_size[1]-cursor.get_height(): cursor_y = screen_size[1]-cursor.get_height() screen.blit(background, (0,0)) screen.blit(cursor, (cursor_x, cursor_y)) pygame.display.update()

Page 49: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

สำ+วนจ�ดการ inputhold = {K_LEFT: False, K_RIGHT: False,

K_UP: False, K_DOWN: False}

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

if event.type == KEYDOWN:

if event.key in [K_LEFT, K_RIGHT, K_UP, K_DOWN]:

hold[event.key] = True

if event.type == KEYUP:

if event.key in [K_LEFT, K_RIGHT, K_UP, K_DOWN]:

hold[event.key] = False

Page 50: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

สำ+วนจ�ดการ input

• เราสำร%างิ dictionary อ�นหน($งิชื่'$อ hold• ใชื่% map รห�สำของิป�Aมกดไปย�งิ True/False• hold[key] = True ถ้%าผื1%ใชื่%กดป�Aม key อย1+• hold[key] = False ถ้%าผื1%ใชื่%ไม+ได%กดป�Aม key อย1+

• ด�กจ�บ KEYDOWN ถ้%ากด key ก0เซต hold[key] = True

• ด�กจ�บ KEYUP ถ้%ากด key ก0เซต hold[key] = False

Page 51: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

สำ+วนจ�ดการการเปล�$ยนต าแหน+งิของิต�วการ�ต1น

dx, dy = 0, 0

if hold[K_LEFT]:

dx = -1

if hold[K_RIGHT]:

dx = 1

if hold[K_UP]:

dy = -1

if hold[K_DOWN]:

dy = 1

• dx และ dy เก0บความเปล�$ยนแปลงิด%านแกน x และแกน y

• เราเซตค+าของิม�นตามป�Aมท�$ผื1%ใชื่%กด

Page 52: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

สำ+วนจ�ดการการเปล�$ยนต าแหน+งิของิต�วการ�ต1น

cursor_x += dxcursor_y += dy

if cursor_x < 0: cursor_x = 0if cursor_x > screen_size[0]-cursor.get_width(): cursor_x = screen_size[0]-cursor.get_width()if cursor_y < 0: cursor_y = 0if cursor_y > screen_size[1]-cursor.get_height(): cursor_y = screen_size[1]-cursor.get_height()

• เปล�$ยนต าแหน+งิของิต�วการ�ต1น (cursor_x และ cursor_y) ด%วยการบวกด%วย dx และ dy

• แล%วจ�ดให%ม�นอย1+ในกรอบของิหน%าต+างิ

Page 53: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

Mouse Events

• MOUSEBUTTONDOWN– ม� attribute 2 ต�วค'อ: button และ pos– button เป#นเลข integer ซ($งิม�ค+า• 1 เม'$อเป#น mouse ป�Aมซ%าย• 2 เม'$อเป#น mouse ป�Aมกลางิ• 3 เม'$อเป#น mouse ป�Aมขวา

– pos เป#นค1+ล าด�บ (x,y) แทนต าแหน+งิท�$ผื1%ใชื่%กดป�Aมเมาสำ�

Page 54: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

Mouse Events

• MOUSEBUTTONUP– ม� attribute 2 ต�วค'อ: button และ pos– Attribute ท�/งิสำองิต�วเหม'อนก�บ

MOUSEBUTTONDOWN

Page 55: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

Mouse Events

• MOUSEMOTION– ปรากฎข(/นเม'$อผื1%ใชื่%เล'$อน mouse pointer– ม� argument 3 ต�ว: buttons, pos, rel– buttons เป#น tuple ม� 3 component• buttons[0] หมายถ้(งิ เมาสำ�ป�Aมซ%าย• buttons[1] หมายถ้(งิ เมาสำ�ป�Aมกลางิ• buttons[2] หมายถ้(งิ เมาสำ�ป�Aมขวา• แต+ละ component ถ้%าเป#น 0 หมายถ้(งิผื1%ใชื่%ไม+ได%กดป�Aม ถ้%า

เป#น 1 หมายความว+าผื1%ใชื่%กดป�Aมขณะเล'$อนเมาสำ�

Page 56: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

Mouse Events

• MOUSEMOTION (ต+อ)– pos ค'อ ค1+ล าด�บ (x,y) แทนต าแหน+งิของิ mouse

pointer– rel ค'อ ค1+ล าด�บ (x,y) แทนการขจ�ดของิ mouse

pointer จากต าแหน+งิของิ mouse pointer ตอนท�$ MOUSEMOTION ปรากฎข(/นคร�/งิท�$แล%ว

Page 57: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมลากต�วการ�ต1นด%วย mouse

Page 58: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

ต�วแปรต+างิๆ• cartoon_x, cartoon_y: ต าแหน+งิม�มบนซ%ายของิ

ร1ปการ�ต1น• start_x, start_y: ต าแหน+งิท�$ผื1%ใชื่%กดป�Aม mouse• cartoon_start_x, cartoon_start_y: ค+าของิ

cartoon_x, cartoon_y เม'$อผื1%ใชื่%กดป�Aม mouse• drag เป#นต�วแปรแบบ boolean ซ($งิเป#น True ถ้%าผื1%

ใชื่%ก าล�งิลากต�วการ�ต1นอย1+ ม เชื่+นน�/นจะเป#น False

Page 59: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

การจ�ดการก�บการกดป�Aม mouseif event.type == MOUSEBUTTONDOWN:

if event.button == 1:

if inside(cartoon_x, cartoon_y,

cartoon.get_width(),

cartoon.get_height(),

event.pos[0], event.pos[1]):

x = event.pos[0] - cartoon_x

y = event.pos[1] - cartoon_y

color = cartoon.get_at((x,y))

if color[3] > 0:

start_x = event.pos[0]

start_y = event.pos[1]

start_cartoon_x = cartoon_x

start_cartoon_y = cartoon_y

drag = True

Page 60: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

การจ�ดการก�บการกดป�Aม mouse

• เราเชื่0คว+า event.button == 1 เพิ'$อเชื่0คว+าผื1%ใชื่%กดป�Aมขวาหร'อไม+

• หล�งิจากน�/นใชื่%ฟ6งิก�ชื่�น inside เพิ'$อเชื่0คว+า mouse pointer อย1+ใน surface ของิร1ปการ�ต1นหร'อไม+

def inside(left, top, width, height, x, y):

return (left <= x and x < left + width and

top <= y and y < top + height)

Page 61: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

การจ�ดการก�บการกดป�Aม mouse

• หล�งิจากน�/นค านวณต าแหน+งิ x, y โดยย(ดม�มบนซ%ายของิร1ปการ�ต1นเป#นจ�ด origin

x = event.pos[0] - cartoon_x

y = event.pos[1] - cartoon_y

• หล�งิจากน�/นเราด(งิ pixel ออกจากร1ปการ�ต1นท�$ต าแหน+งิ x,y มาด%วยฟ6งิก�ชื่�น pygame.Surface.get_at

color = cartoon.get_at((x,y))

Page 62: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

การจ�ดการก�บการกดป�Aม mouse

• เน'$องิจาก surface ของิร1ปการ�ต1นม�ขนาด 32 บ ต (ตามหน%าจอ) เราจ(งิได% color เป#น tuple ท�$ม� 4 component (r,g,b,a)

• เราจ(งิสำามารถ้เชื่0คได%ว+าพิ กเซลท�$คล กโปร+งิใสำหร'อไม+โดยเชื่0คว+า color[3] ม�ค+าเท+าก�บ 0 หร'อไม+

• ถ้%าไม+ แสำดงิว+าผื1%ใชื่%คล กถ้1กภาพิแล%ว เราจ(งิเก0บข%อม1ลท�$จ าเป#นต+อการลากร1ปการ�ต1นต+อไป

if color[3] > 0: start_x = event.pos[0] start_y = event.pos[1] start_cartoon_x = cartoon_x start_cartoon_y = cartoon_y drag = True

Page 63: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

การจ�ดการก�บการปล+อยป�Aม mouse

• เซตค+า drag เป#น False

if event.type == MOUSEBUTTONUP:

if event.button == 1:

drag = False

Page 64: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

การจ�ดการก�บการเล'$อน mouse

• เซตค+า cartoon_x และ cartoon_y ให%เป#นค+าใหม+

if event.type == MOUSEMOTION:

if drag:

cartoon_x = (start_cartoon_x +

event.pos[0] - start_x)

cartoon_y = (start_cartoon_y +

event.pos[1] - start_y)

Page 65: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

การวาดข%อความ• ข�/นแรกต%องิสำร%างิ object ประเภท

pygame.font.Font ข(/นมา• เราสำามารถ้ใชื่%ฟ6งิก�ชื่�น pygame.font.SysFont

ท าการโหลด font ท�$ม�อย1+แล%วในระบบ• pygame.font.SysFont– อาร�ก วเมนต�ต�วแรกค'อชื่'$อฟอนต�– อาร�ก วเมนต�ต�วท�$สำองิค'อขนาดของิฟอนต�– ต�วอย+างิ

font = pygame.font.SysFont(“Times New Roman”, 16)

Page 66: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

การวาดข%อความ• หล�งิจากน�/นจ(งิเร�ยก method

pygame.font.Font.render เพิ'$อสำร%างิ pygame.Surface ท�$บรรจ�ร1ปของิข%อความเอาไว%

• pygame.font.Font.render– ม�อาร�ก วเมนต�สำ�$ต�ว– ต�วท�$หน($งิค'อข%อความท�$อยากเข�ยน– ต�วท�$สำองิเป#นต�วแปรประเภท boolean ซ($งิม�ค+าเป#น True ถ้%า

เราต%องิการให%ระบบท า antialiasing ให%ก�บข%อความท�$สำ� $งิให%วาด– ต�วท�$สำามเป#นสำ�ของิข%อความ– ต�วท�$สำ�$เป#นสำ�ของิพิ'/นหล�งิ ถ้%าไม+ใสำ+มาพิ'/นหล�งิจะโปร+งิแสำงิ– ต�วอย+างิ

text1 = font.render(“Hello”, True, (0,0,0), (255,255,255))text2 = font.redner(“ABC”, True, (255,128,128))

Page 67: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

การวาดข%อความ• หล�งิจากน�/นจ(งิ blit surface น�/นเข%าสำ1+ surface อ'$น

Page 68: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

โปรแกรมวาดต�วอ�กษรแบบสำ�+มๆ

Page 69: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

สำร%างิ pygame.Surface ข(/นมาเองิ• Constructor ของิ pygame.Surface ม�

อาร�ก วเมนต�เหม'อน pygame.display.set_mode• ในโปรแกรมน�/เราสำร%างิ pygame.Surface ต�วหน($งิ

ชื่'$อ paper ท าหน%าท�$เป#น กระดาษ สำ าหร�บวาดต�ว“ ”อ�กษรออกไป

paper = pygame.Surface((800,600), 0, 32)

paper.fill((255,255,255))

Page 70: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

pygame.Surface.fill

• ถ้ม surface ด%วยสำ�ท�$ก าหนดให%• ให%อาร�ก วเมนต�หน($งิต�วค'อสำ�ท�$ต%องิการถ้ม

Page 71: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

สำร%างิ pygame.font.Font

• เราสำร%างิ pygame.font.Font ไว%หลาย object โดยท�$แต+ละ object แทนฟอนต�หน($งิขนาด

• เก0บ pygame.font.Font ไว%ใน dictionary โดยใชื่%ขนาดของิฟอนต�เป#น key

fonts = {}

for size in range(10,120):

fonts[size] = pygame.font.SysFont(

"Times New Roman", size)

Page 72: 418512  ภาษาโปรแกรมคอมพิวเตอร์ Pygame

วาดต�วอ�กษรใหม+เม'$อผื1%ใชื่%คล กif event.type == MOUSEBUTTONDOWN:

if event.button == 1:

color = (randint(0,255),

randint(0,255),

randint(0,255))

character = chr(randint(65,90))

char_surface fonts[randint(10,119)].render(

character, True, color)

x = event.pos[0] - char_surface.get_width()/2

y = event.pos[1] - char_surface.get_height()/2

paper.blit(char_surface, (x,y))