ตรวจสอบการคลิกเมาส์ที่สไปร์ทตัวละคร
# exercise 6
# Pygame Zero Basic : Basic collision detection between mouse coordinates and images
import pgzrun
# กำหนดขนาด window
WIDTH = 500
HEIGHT = 500
TITLE = "Pygame Zero: Basic collision detection"
# สไปร์ทตัวละคร
player = Actor("alien_pink") # ไฟล์รูป sprite red.png ไฟล์ต้องเก็บไว้ในโฟลเดอร์ images เท่านั้น!
player.pos = 250, 250
# ฟังก์ชั่นวาดบนจอ
def draw():
screen.fill((128, 128, 128))
player.draw()
#ฟังก์ชั่นตรวจสอบการคลิกเมาสที่ สไปร์ทตัวละคร
def on_mouse_down(pos):
if player.collidepoint(pos): # เมื่อคลิกที่ sprite player
player.image = "alien_green" # เปลี่ยน player รูปเป็น alien_green
#รันโปรแกรม
pgzrun.go()
เริ่มต้นสไปร์ทเป็น “alien_pink”
เมื่อคลิกที่สไปร์ท player จะเปลี่ยนเป็นสไปร์ท “alien_green”