Pygameใ็จใใฆใใญใใฏๅดฉใใไฝใๆนๆณใจใฝใผในใณใผใใ่งฃ่ชฌใใพใใ
Pygameใงใใญใใฏๅดฉใใฒใผใ ใฎไฝๆ
ๅๅใฎ่จไบใในใใฉใคใใฎไฝฟใๆนใจใฐใซใผใๅใใจใในใใฉใคใๅๅฃซใฎ่ก็ชๅคๅฎใจ่ทณใญ่ฟใใใฎๆดป็จไพใจใใฆใไปฅไธใฎใใญใใฏๅดฉใใฒใผใ ใไฝๆใใๆนๆณใ่งฃ่ชฌใใพใใ
ใไปๅไฝฟ็จใใ็ปๅใ
Windowsใฎใใคใณใใฝใใใงไฝใใพใใใใชใณใฏๅ
ใใใใฆใณใญใผใใใฆใ่ช็ฑใซใไฝฟใใใ ใใใ
ใไปๅไฝฟ็จใใ้ณๅฃฐใ
ใ็กๆ BGMใปๅนๆ้ณใฎใใชใผ้ณๆบ็ด ๆ(Springin’ Sound Stock)ๆงใใใใๅใใใพใใใ
- paddle_sound.mp3ใปใปใปใชใณใฏๅ ใฎ้ณๅฃฐใใกใคใซใ8bitไธ้9.mp3ใใไฝฟ็จใใพใใ
- block_sound.mp3ใปใปใปใชใณใฏๅ ใฎ้ณๅฃฐใใกใคใซใ็ ดๅฃ้ณ_ใใใใ็ ดๅฃ.mp3ใใไฝฟ็จใใพใใ
- gameover_sound.mp3ใปใปใปใชใณใฏๅ ใฎ้ณๅฃฐใใกใคใซใ8bit้ธๆ8.mp3ใใไฝฟ็จใใพใใ
ๅ็ป่งฃ่ชฌ็
ๆฌใใผใธใฎๅ ๅฎนใฏไปฅไธๅ็ปใงใ่งฃ่ชฌใใฆใใพใใ
ใตใณใใซใณใผใ
ใณใผใ่งฃ่ชฌ
ไธ่จใตใณใใซใณใผใใฎๅ้จๅใ่งฃ่ชฌใใพใใ
1. ใคใณใใผใใจๅฎๆฐใฎๅฎ็พฉ
import math
import sys
import pygame
from pygame.locals import *
math
ใจsys
ใฏPythonใฎๆจๆบใฉใคใใฉใชใงใคใณใใผใใใพใใpygame
ใฏใฒใผใ ้็บ็จใฎใฉใคใใฉใชใงใคใณใใผใใใพใใpygame.locals
ใใ็นๅฎใฎๅฎๆฐใใคใณใใผใใใพใใ
2. ็ป้ขใตใคใบใจใใกใคใซใในใฎๅฎ็พฉ
# ็ป้ขใตใคใบ
SCREEN = Rect(0, 0, 400, 400)
# ็ปๅใใกใคใซใฎใใน
PADDLE_IMG_PATH = "/Users/github/sample/python/pygame/breakout/sample01/assets/img/paddle.png"
BLOCK_IMG_PATH = "/Users/github/sample/python/pygame/breakout/sample01/assets/img/block.png"
BALL_IMG_PATH = "/Users/github/sample/python/pygame/breakout/sample01/assets/img/ball.png"
# ้ณๅฃฐใใกใคใซใฎใใน
PADDLE_SOUND_PATH = "/Users/github/sample/python/pygame/breakout/sample01/assets/mp3/paddle_sound.mp3"
BLOCK_SOUND_PATH = "/Users/github/sample/python/pygame/breakout/sample01/assets/mp3/block_sound.mp3"
GAMEOVER_SOUND_PATH = "/Users/github/sample/python/pygame/breakout/sample01/assets/mp3/gameover_sound.mp3"
- ็ป้ขใตใคใบใๅฎ็พฉใ
- ็ปๅใ้ณๅฃฐใใกใคใซใฎใในใๅฎ็พฉใ
3. ใใใซใฎในใใฉใคใใฏใฉใน
# ใใใซใฎในใใฉใคใใฏใฉใน
class Paddle(pygame.sprite.Sprite):
# ใณใณในใใฉใฏใฟ๏ผๅๆๅใกใฝใใ๏ผ
def __init__(self, filename):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = pygame.image.load(filename).convert()
self.rect = self.image.get_rect()
self.rect.bottom = SCREEN.bottom - 20 # ใใใซใฎyๅบงๆจ
def update(self):
self.rect.centerx = pygame.mouse.get_pos()[0] # ใใฆในใฎxๅบงๆจใใใใซใฎxๅบงๆจใซ
self.rect.clamp_ip(SCREEN) # ใฒใผใ ็ป้ขๅ
ใฎใฟใง็งปๅ
- ใใใซใฎ็ปๅใ่ชญใฟ่พผใฟใไฝ็ฝฎใ่จญๅฎใ
- ใใฆในใฎไฝ็ฝฎใซๅฟใใฆใใใซใ็งปๅใ
4. ใใผใซใฎในใใฉใคใใฏใฉใน
ใใฎใฏใฉในใฏใใใผใซใฎๅใใ็ฎก็ใใใฒใผใ ใฎ้ฒ่กใๅถๅพกใใ้่ฆใช้จๅใงใใ
็นใซใ่ก็ชๅฆ็ใๅๅฐ่งๅบฆใฎ่จ็ฎใ่ใจใชใใพใใ
# ใใผใซใฎในใใฉใคใใฏใฉใน
class Ball(pygame.sprite.Sprite):
# ใณใณในใใฉใฏใฟ๏ผๅๆๅใกใฝใใ๏ผ
def __init__(self, filename, paddle, blocks, score, speed, angle_left, angle_right):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = pygame.image.load(filename).convert()
self.rect = self.image.get_rect()
self.dx = self.dy = 0 # ใใผใซใฎ้ๅบฆ
self.paddle = paddle # ใใใซใธใฎๅ็
ง
self.blocks = blocks # ใใญใใฏใฐใซใผใใธใฎๅ็
ง
self.update = self.start # ใฒใผใ ้ๅง็ถๆ
ใซๆดๆฐ
self.score = score
self.hit = 0 # ้ฃ็ถใงใใญใใฏใๅฃใใๅๆฐ
self.speed = speed # ใใผใซใฎๅๆ้ๅบฆ
self.angle_left = angle_left # ใใใซใฎๅๅฐๆนๅ(ๅทฆ็ซฏ:135ๅบฆ๏ผ
self.angle_right = angle_right # ใใใซใฎๅๅฐๆนๅ(ๅณ็ซฏ:45ๅบฆ๏ผ
- initใกใฝใใ: ใใผใซใฎๅๆ่จญๅฎใ่กใใพใใ
- ็ปๅใฎ่ชญใฟ่พผใฟใไฝ็ฝฎใฎ่จญๅฎใ้ๅบฆใฎๅๆๅใในใใฉใคใใฐใซใผใใblocksใใธใฎ่ฟฝๅ ใใใใซใใใญใใฏใธใฎๅ็ งใ่กใฃใฆใใพใใ
- ใใใซใใใญใใฏใจใฎ่ก็ชใ็ฎก็ใ
# ใฒใผใ ้ๅง็ถๆ
๏ผใใฆในใๅทฆใฏใชใใฏๆใใใจใใผใซๅฐๅบ๏ผ
def start(self):
# ใใผใซใฎๅๆไฝ็ฝฎ(ใใใซใฎไธ)
self.rect.centerx = self.paddle.rect.centerx
self.rect.bottom = self.paddle.rect.top
# ๅทฆใฏใชใใฏใงใใผใซๅฐๅบ
if pygame.mouse.get_pressed()[0] == 1:
self.dx = 0
self.dy = -self.speed
self.update = self.move
- startใกใฝใใ: ใฒใผใ ้ๅงๆใฎใใผใซใฎไฝ็ฝฎใใใใซใฎไธใซ่จญๅฎใใๅทฆใฏใชใใฏใงใใผใซใๅฐๅบใใพใใ
# ใใผใซใฎๆๅ
def move(self):
self.rect.centerx += self.dx
self.rect.centery += self.dy
# ๅฃใจใฎๅๅฐ
if self.rect.left < SCREEN.left: # ๅทฆๅด
self.rect.left = SCREEN.left
self.dx = -self.dx # ้ๅบฆใๅ่ปข
if self.rect.right > SCREEN.right: # ๅณๅด
self.rect.right = SCREEN.right
self.dx = -self.dx
if self.rect.top < SCREEN.top: # ไธๅด
self.rect.top = SCREEN.top
self.dy = -self.dy
# ใใใซใจใฎๅๅฐ(ๅทฆ็ซฏ:135ๅบฆๆนๅ, ๅณ็ซฏ:45ๅบฆๆนๅ, ใใไปฅๅค:็ทๅฝข่ฃ้)
# 2ใคใฎspriteใๆฅ่งฆใใฆใใใใฉใใใฎๅคๅฎ
if self.rect.colliderect(self.paddle.rect) and self.dy > 0:
self.hit = 0 # ้ฃ็ถใใใใ0ใซๆปใ
(x1, y1) = (self.paddle.rect.left - self.rect.width, self.angle_left)
(x2, y2) = (self.paddle.rect.right, self.angle_right)
x = self.rect.left # ใใผใซใๅฝใใฃใไฝ็ฝฎ
y = (float(y2-y1)/(x2-x1)) * (x - x1) + y1 # ็ทๅฝข่ฃ้
angle = math.radians(y) # ๅๅฐ่งๅบฆ
self.dx = self.speed * math.cos(angle)
self.dy = -self.speed * math.sin(angle)
self.paddle_sound.play() # ๅๅฐ้ณ
# ใใผใซใ่ฝใจใใๅ ดๅ
if self.rect.top > SCREEN.bottom:
self.update = self.start # ใใผใซใๅๆ็ถๆ
ใซ
self.gameover_sound.play()
self.hit = 0
self.score.set_score(0) # ในใณใขใ0็นใซใใ
#self.score.add_score(-100) # ในใณใขๆธ็น-100็น
- moveใกใฝใใ: ใใผใซใฎ็งปๅใจ่ก็ชๅฆ็ใ่กใใพใใๅฃใใใใซใใใญใใฏใจใฎ่ก็ชใๆคๅบใใๅๅฐใในใณใขใฎๆดๆฐใ่กใใพใใ
ใใญใใฏใจใใผใซใฎ่ก็ชๅคๅฎใจใใญใใฏใฎๅ้ค
blocks_collided = pygame.sprite.spritecollide(self, self.blocks, True)
if blocks_collided: # ่ก็ชใใญใใฏใใใๅ ดๅ
oldrect = self.rect
for block in blocks_collided:
# ใใผใซใๅทฆใใใใญใใฏใธ่ก็ชใใๅ ดๅ
if oldrect.left < block.rect.left and oldrect.right < block.rect.right:
self.rect.right = block.rect.left
self.dx = -self.dx
# ใใผใซใๅณใใใใญใใฏใธ่ก็ชใใๅ ดๅ
if block.rect.left < oldrect.left and block.rect.right < oldrect.right:
self.rect.left = block.rect.right
self.dx = -self.dx
# ใใผใซใไธใใใใญใใฏใธ่ก็ชใใๅ ดๅ
if oldrect.top < block.rect.top and oldrect.bottom < block.rect.bottom:
self.rect.bottom = block.rect.top
self.dy = -self.dy
# ใใผใซใไธใใใใญใใฏใธ่ก็ชใใๅ ดๅ
if block.rect.top < oldrect.top and block.rect.bottom < oldrect.bottom:
self.rect.top = block.rect.bottom
self.dy = -self.dy
self.block_sound.play() # ๅนๆ้ณใ้ณดใใ
self.hit += 1 # ่ก็ชๅๆฐ
self.score.add_score(self.hit * 10) # ่ก็ชๅๆฐใซๅฟใใฆในใณใขๅ ็ฎ
pygame.sprite.spritecollide(self, self.blocks, True)
: ใใผใซ (self
) ใจใใญใใฏ (self.blocks
) ใฎ่ก็ชใใใงใใฏใใพใใ็ฌฌไธๅผๆฐใฎTrue
ใฏใใใผใซใจ่ก็ชใใใใญใใฏใใฐใซใผใใใๅ้คใใใใจใๆๅณใใพใใใใใซใใใใใญใใฏใๆถใใพใใif blocks_collided:
: ่ก็ชใใใใญใใฏใใใๅ ดๅใซๅฆ็ใ่กใใพใใfor block in blocks_collided:
: ่ก็ชใใใในใฆใฎใใญใใฏใซๅฏพใใฆๅฆ็ใ่กใใพใใ- ๅๆนๅใใใฎ่ก็ชๅคๅฎ: ใใผใซใใใญใใฏใซใฉใฎๆนๅใใ่ก็ชใใใใๅคๅฎใใใใผใซใฎๅๅฐๆนๅใๅคๆดใใพใใ
self.block_sound.play()
: ่ก็ช้ณใๅ็ใใพใใself.hit += 1
: ่ก็ชๅๆฐใใซใฆใณใใใพใใself.score.add_score(self.hit * 10)
: ่ก็ชๅๆฐใซๅฟใใฆในใณใขใๅ ็ฎใใพใใ
5. ใใญใใฏใฏใฉใน
class Block(pygame.sprite.Sprite):
def __init__(self, filename, x, y):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = pygame.image.load(filename).convert()
self.rect = self.image.get_rect()
# ใใญใใฏใฎๅทฆไธๅบงๆจ
self.rect.left = SCREEN.left + x * self.rect.width
self.rect.top = SCREEN.top + y * self.rect.height
__init__
ใกใฝใใ: ใใญใใฏใฎๅๆๅใ่กใใพใใpygame.sprite.Sprite.__init__(self, self.containers)
: ในใใฉใคใใฎๅๆๅใ่กใใใใญใใฏใในใใฉใคใใฐใซใผใใซ่ฟฝๅ ใใพใใself.image = pygame.image.load(filename).convert()
: ใใญใใฏใฎ็ปๅใ่ชญใฟ่พผใฟใself.image
ใซ่จญๅฎใใพใใself.rect = self.image.get_rect()
: ็ปๅใฎ็ฉๅฝข๏ผไฝ็ฝฎใจใตใคใบ๏ผใๅๅพใใself.rect
ใซ่จญๅฎใใพใใself.rect.left
ใจself.rect.top
: ใใญใใฏใฎๅทฆไธๅบงๆจใ่จญๅฎใใพใใx
ใจy
ใฎๅคใซๅบใฅใใฆใใใญใใฏใฎไฝ็ฝฎใ่จ็ฎใใพใใ
6. ในใณใขใฏใฉใน
class Score():
def __init__(self, x, y):
self.sysfont = pygame.font.SysFont(None, 20)
self.score = 0
(self.x, self.y) = (x, y)
def draw(self, screen):
img = self.sysfont.render("SCORE:" + str(self.score), True, (255,255,250))
screen.blit(img, (self.x, self.y))
def add_score(self, x):
self.score += x
def set_score(self, score):
self.score = score
__init__
ใกใฝใใ: ในใณใขใฎๅๆๅใ่กใใพใใself.sysfont = pygame.font.SysFont(None, 20)
: ใใฉใณใใ่จญๅฎใใพใใใตใคใบใฏ20ใใฏใปใซใงใใself.score = 0
: ในใณใขใ0ใซๅๆๅใใพใใ(self.x, self.y) = (x, y)
: ในใณใขใฎ่กจ็คบไฝ็ฝฎใ่จญๅฎใใพใใ
draw
ใกใฝใใ: ในใณใขใ็ป้ขใซๆ็ปใใพใใimg = self.sysfont.render("SCORE:" + str(self.score), True, (255,255,250))
: ในใณใขใใใญในใใจใใฆใฌใณใใชใณใฐใใ็ปๅใจใใฆ็ๆใใพใใscreen.blit(img, (self.x, self.y))
: ็ๆใใในใณใข็ปๅใๆๅฎใใไฝ็ฝฎใซๆ็ปใใพใใ
add_score
ใกใฝใใ: ในใณใขใๅ ็ฎใใพใใself.score += x
: ็พๅจใฎในใณใขใซๆๅฎใใใๅคใๅ ็ฎใใพใใ
set_score
ใกใฝใใ: ในใณใขใ่จญๅฎใใพใใself.score = score
: ในใณใขใๆๅฎใใใๅคใซ่จญๅฎใใพใใ
7. ใฒใผใ ใฎใกใคใณใซใผใ
ใฒใผใ ใฎไธป่ฆใชในใใฉใคใใๅๆๅใใ้ฉๅใชใฐใซใผใใซ่ฟฝๅ ใใใใจใงใๆ็ปใ่ก็ชๅคๅฎใๅน็็ใซ่กใใใใใซใใฆใใพใใ
ๆ็ป็จใฎในใใฉใคใใฐใซใผใ
group = pygame.sprite.RenderUpdates()
group
: ใในใฆใฎในใใฉใคใใ็ฎก็ใใใฐใซใผใใงใใใใฎใฐใซใผใใฏใในใใฉใคใใฎๆ็ปใจๆดๆฐใ่กใใพใใ
่ก็ชๅคๅฎ็จใฎในใใฉใคใใฐใซใผใ
blocks = pygame.sprite.Group()
blocks
: ใใญใใฏใฎในใใฉใคใใ็ฎก็ใใใฐใซใผใใงใใใใฎใฐใซใผใใฏใใใผใซใจใฎ่ก็ชๅคๅฎใซไฝฟ็จใใใพใใ
ในใใฉใคใใฐใซใผใใซ่ฟฝๅ
Paddle.containers = group
Ball.containers = group
Block.containers = group, blocks
Paddle.containers
: ใใใซใฎในใใฉใคใใgroup
ใซ่ฟฝๅ ใใพใใBall.containers
: ใใผใซใฎในใใฉใคใใgroup
ใซ่ฟฝๅ ใใพใใBlock.containers
: ใใญใใฏใฎในใใฉใคใใgroup
ใจblocks
ใฎไธกๆนใซ่ฟฝๅ ใใพใใ
Block.containers
ใซgroup
ใจblocks
ใฎไธกๆนใ่ฟฝๅ ใใ็็ฑใฏใใใญใใฏใฎในใใฉใคใใๆ็ปใจ่ก็ชๅคๅฎใฎไธกๆนใซ้ขไฟใใใใใงใใ
- ๆ็ปใฎใใใฎใฐใซใผใ (
group
)- ๆ็ป:
group
ใฏใใในใฆใฎในใใฉใคใใๆ็ปใใใใใฎใฐใซใผใใงใใใใใซใใใใใญใใฏใ็ป้ขใซ่กจ็คบใใใพใใ - ๆดๆฐ:
group
ใฏใในใใฉใคใใฎไฝ็ฝฎใ็ถๆ ใๆดๆฐใใใใใซใไฝฟ็จใใใพใใใใใซใใใใฒใผใ ใฎ้ฒ่กใซๅใใใฆในใใฉใคใใ้ฉๅใซๅไฝใใพใใ
- ๆ็ป:
- ่ก็ชๅคๅฎใฎใใใฎใฐใซใผใ (
blocks
)- ่ก็ชๅคๅฎ:
blocks
ใฏใใใผใซใจใฎ่ก็ชๅคๅฎใ่กใใใใฎใฐใซใผใใงใใใใใซใใใใใผใซใใใญใใฏใซๅฝใใฃใใจใใซ้ฉๅใชๅๅฟ๏ผใใญใใฏใฎๆถๅคฑใในใณใขใฎๆดๆฐ๏ผใ่กใใใพใใ
- ่ก็ชๅคๅฎ:
ใใฎใใใซใใใญใใฏใฎในใใฉใคใใไธกๆนใฎใฐใซใผใใซ่ฟฝๅ ใใใใจใงใใฒใผใ ใฎๆ็ปใจ่ก็ชๅคๅฎใฎไธกๆนใๅน็็ใซ็ฎก็ใใใใจใใงใใพใใ
`pygame.sprite.spritecollide(self, self.blocks, True)`ใฏใๆๅฎใใใในใใฉใคใ๏ผใใผใซ๏ผใจใฐใซใผใ๏ผ`blocks`๏ผๅ ใฎในใใฉใคใใจใฎ่ก็ชใใใงใใฏใใพใใ
ใใใ`Block.containers = group, blocks`ใ`Block.containers = group`ใซๅคๆดใใใจใใใญใใฏใฎในใใฉใคใใ`blocks`ใฐใซใผใใซ่ฟฝๅ ใใใชใใชใใพใใ
ใคใพใใ`pygame.sprite.spritecollide(self, self.blocks, True)`ใงใใญใใฏใจใฎ่ก็ชใๆคๅบใใใชใใชใใพใใใใฎ็ตๆใใใผใซใใใญใใฏใซๅฝใใฃใฆใใใญใใฏใๆถใใใในใณใขใๆดๆฐใใใพใใใ
ใใผใซ่ชไฝใฏไปใฎในใใฉใคใ๏ผใใใซใใใญใใฏ๏ผใจใฎ่ก็ชๅคๅฎใ่กใใพใใใใใผใซใไปใฎในใใฉใคใใจ่ก็ชใใๅฏพ่ฑกใจใฏใชใใชใใใใ`blocks`ใฐใซใผใใซๅซใพใใๅฟ ่ฆใฏใใใพใใใ
ใใใซใฎไฝๆ
paddle = Paddle(PADDLE_IMG_PATH)
paddle
: ใใใซใฎในใใฉใคใใไฝๆใใ็ปๅใ่ชญใฟ่พผใฟใพใใ
ใใญใใฏใฎไฝๆ
for x in range(1, 15):
for y in range(1, 11):
Block(BLOCK_IMG_PATH, x, y)
- ใใญใใฏใฎ้ ็ฝฎ: 14ๅร10่กใฎใใญใใฏใไฝๆใใๅใใญใใฏใฎไฝ็ฝฎใ่จญๅฎใใพใใ
ในใณใขใฎ่กจ็คบ
score = Score(10, 10)
score
: ในใณใข่กจ็คบ็จใฎใชใใธใงใฏใใไฝๆใใ็ป้ขใฎๅบงๆจ(10, 10)ใซ้ ็ฝฎใใพใใ
ใใผใซใฎไฝๆ
Ball(BALL_IMG_PATH, paddle, blocks, score, 5, 135, 45)
Ball
: ใใผใซใฎในใใฉใคใใไฝๆใใๅๆ่จญๅฎใ่กใใพใใใใใซใใใญใใฏใในใณใขใ้ๅบฆใๅๅฐ่งๅบฆใชใฉใ่จญๅฎใใพใใ
ใซใผใๅฆ็ใฎๅฎ่กใ็ถ็ถใใใใฉใฐ
running = True # ใซใผใๅฆ็ใฎๅฎ่กใ็ถ็ถใใใใฉใฐ
running
: ใฒใผใ ใซใผใใ็ถ็ถใใใใฉใใใๅถๅพกใใใใฉใฐใงใใTrue
ใฎ้ใฏใซใผใใ็ถใใพใใ
8. ใกใคใณใซใผใ
while running:
clock.tick(60) # ใใฌใผใ ใฌใผใ(60fps)
screen.fill((0,20,0))
# ๅ
จใฆใฎในใใฉใคใใฐใซใผใใๆดๆฐ
group.update()
# ๅ
จใฆใฎในใใฉใคใใฐใซใผใใๆ็ป
group.draw(screen)
# ในใณใขใๆ็ป
score.draw(screen)
# ็ป้ขๆดๆฐ
pygame.display.update()
clock.tick(60)
: ใใฌใผใ ใฌใผใใ60FPSใซ่จญๅฎใใพใใใใใซใใใใฒใผใ ใฎๅไฝใไธๅฎใฎ้ๅบฆใง้ฒ่กใใพใใscreen.fill((0,20,0))
: ็ป้ขใ็ท่ฒใงๅกใใคใถใใพใใใใใซใใใๅใฎใใฌใผใ ใฎๆฎๅใๆถใใพใใgroup.update()
: ใในใฆใฎในใใฉใคใใฎ็ถๆ ใๆดๆฐใใพใใๅในใใฉใคใใฎupdate
ใกใฝใใใๅผใณๅบใใใพใใgroup.draw(screen)
: ใในใฆใฎในใใฉใคใใ็ป้ขใซๆ็ปใใพใใscore.draw(screen)
: ในใณใขใ็ป้ขใซๆ็ปใใพใใpygame.display.update()
: ็ป้ขใๆดๆฐใใๆ็ปๅ ๅฎนใ่กจ็คบใใพใใ
ใคใใณใๅฆ็
for event in pygame.event.get():
# ้ใใใใฟใณใๆผใใใใ็ตไบ
if event.type == QUIT:
running = False
# ใญใผใคใใณใ
if event.type == KEYDOWN:
# Escใญใผใๆผใใใใ็ตไบ
if event.key == K_ESCAPE:
running = False
- ใคใใณใใซใผใ: ใฆใผใถใผใฎๅ ฅๅใใทในใใ ใฎใคใใณใใๅฆ็ใใพใใ
QUIT
ใคใใณใ: ใฆใฃใณใใฆใฎ้ใใใใฟใณใๆผใใใๅ ดๅใซ็บ็ใใrunning
ใFalse
ใซใใฆใซใผใใ็ตไบใใพใใKEYDOWN
ใคใใณใ: ใญใผใๆผใใใใจใใซ็บ็ใใพใใK_ESCAPE
ใญใผ: Escใญใผใๆผใใใๅ ดๅใrunning
ใFalse
ใซใใฆใซใผใใ็ตไบใใพใใ
9. ็ตไบๅฆ็
pygame.quit()
sys.exit()
pygame.quit()
: Pygameใ็ตไบใใใชใฝใผในใ่งฃๆพใใพใใsys.exit()
: ใใญใฐใฉใ ใ็ตไบใใพใใ
้ข้ฃใใผใธ
Pygameใฎไฝฟใๆนใซใคใใฆใฏไปฅไธใใผใธใง่งฃ่ชฌใใฆใใพใใ

Pythonๅ จ่ฌใซใคใใฆใฏไปฅไธใใผใธใง่งฃ่ชฌใใฆใใพใใ

ใณใกใณใ
# ๅฃใจใฎๅๅฐ
if self.rect.left SCREEN.right: # ๅณๅด
ใใใฎ้จๅใซใจใฉใผใๅบใฆใใพใใพใๅๅ ใฏไฝใงใใใใ๏ผ
# ๅฃใจใฎๅๅฐ
if self.rect.left SCREEN.right: # ๅณๅด
ใใใฎ้จๅใซใจใฉใผใๅบใฆใใพใใพใใ
ใพใ ใพใ ๅๅฟ่ ใงใจใใใใใณใใผ๏ผใใผในใใงๅฎ่กใใฆใใใ ใใชใฎใงใใใๅฐใฃใฆใใพใใ
=ใใใชใใฎใๅๅ ใงใใใใ๏ผ
ใใใใใ้กใใใพใใ
ใณใกใณใใใใใจใใใใใพใใ
ใๆๆใฎใจใใ็ญๅทใไธ็ญๅทใใชใใฎใๅๅ ใงใใ
่จไบใฎใฝใผในใณใผใใไฟฎๆญฃใใพใใใฎใงใ็ขบ่ชใใใ ใใ
ไฝฟ็จใใฆใใpngใฏใฉใใงๅ ฅๆใงใใพใใ๏ผ
URLใจใใใใฐๆใใฆใใ ใใใ
็ปๅใใกใคใซไธใงๅณใฏใชใใฏใใ[็ปๅใใกใคใซใไฟๅญใใ]ใชใฉใ้ธๆใใใจๅ ็ปๅใไฟๅญใงใใพใใ
ๅ่ใซใใใฆใใใ ใใฆใใใพใใ
ใฉใใใฆใใใฝใผในใณใผใใๅ ฅใ่พผใใงใฟใใฎใงใใใimportใฎ็ฎๆใง่ชญใฟ่พผใฟใพใใใ
ๅฎๆๅใจๆฏ่ผใใใใใฎใงใใกใคใซใใใใ ใใใฐๅนธใใงใใ
ใฉใใใงใใฆใใชใใฎใๆฏ่ผใใใใใงใใ
ไฝๅใใใใใ้กใใใใใพใใ
ๆฒ่ผใใฆใใใใญใฐใฉใ ใๅฎๆ็ใงใใ
ใฒใผใ ๅ ใงใญใผใใใ็ปๅใใกใคใซใฏๆขใซๆฒ่ผใใฆใใใพใใใ้ณๅฃฐใใกใคใซใฏๅ้ ๅธ็ฆๆญขใฎใใชใผ้ณๆบใไฝฟ็จใใฆใใใใ
ๅ ฅๆๅ ใ็ดนไปใใใพใงใจใชใฃใฆใใใพใใ
ใใฎใใใๅ จใใกใคใซไธๅผใใๆไพใจใใใฎใฏใงใใพใใใฎใงใใ็่งฃใใใ ใใพใใจๅนธใใงใใ
importใงใจใฉใผใๅบใฆใใใจใใใใจใฏpygameใขใธใฅใผใซใใคใณในใใผใซใงใใฆใใชใๅฏ่ฝๆงใใใใพใใฎใง
ๅๅบฆใใpip install pygameใใงใคใณในใใผใซใใใฆใฟใฆใฏใใใใงใใใใใ
LinuxใงPythonใๅๅผทใๅงใใ็ด ไบบใงใใ
ใใกใคใซใฎ้จๅใง่ฅๅนฒไฟฎๆญฃใ่ฆใใพใใใใ็กไบๅใใพใใใ
ใใใใจใใใใใพใใ