🟥 Cube Catcher
✍️ Coded line by line by Nutpoom Sookkasem, age 16 💛 · See full profile and all works →
📖 What is this project?
Cube Catcher คือเกมฝึกความไวที่เขียนด้วย pygame — สี่เหลี่ยมสีแดงจะโผล่ขึ้นมาแบบสุ่มตำแหน่งบนจอ ผู้เล่นต้องคลิกให้โดน พอโดนปุ๊บมันจะกระโดดไปโผล่ที่ใหม่ทันทีและได้แต้มเพิ่ม เล่นไปเรื่อย ๆ ทำแต้มให้ได้มากที่สุด
สิ่งที่นักเรียนทำได้ดีคือการใช้ random.randint(0, width - 50) — ลบขนาดของสี่เหลี่ยมออกจากความกว้างจอ เพื่อกันไม่ให้วัตถุโผล่หลุดขอบจอ ซึ่งเป็นรายละเอียดที่คนเริ่มเขียนเกมมักลืมกัน
🕹️ Try the real thing!
This is the real pygame game, converted to run on the web with every position, colour and rule kept exactly the same — just click and play.
คลิก/แตะให้โดนสี่เหลี่ยมแดงให้ไวที่สุด — โดนแล้วมันจะกระโดดไปที่ใหม่
👨💻 The original Python source code (with the comments they wrote themselves)
import pygame
import random
pygame.init()
width = 800
height = 500
screen = pygame.display.set_mode((width, height))#กำหนดขนาดหน้าจอ
cube = pygame.Rect((random.randint(0, width - 50), random.randint(0, height - 50), 50, 50))#สุ่มจุดที่จะวาง"cube"
run = True
color = [255, 0, 0]#กำหนดสี
font = pygame.font.Font(None, 36)#กำหนด font และขนาด
point = 0
while run:
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (color), cube)#นำ"cube"ลงwindow
score_text = font.render(f"Points: {point}", True, (255, 255, 255))#กำหนดสีและตัวอักษร
screen.blit(score_text, (10, 10))#กำหนดที่อยู่ตัวอักษร
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:#ตรวจจับว่า mouse ถูกกดหรือไม่
if cube.collidepoint(event.pos):#ตรวจจับว่า mouse ถูกกดตรง "cube" หรือไม่
cube.topleft = (random.randint(0, width - 50),random.randint(0, height - 50))#เมื่อ "cube" ถูกกดให้สุ่มที่อยู่ต่อไปของ "cube"
point = point + 1 #เพิ่มค่า point หลังกด "cube" ได้
pygame.display.update()
Our Python course is taught live, one-on-one, and every child builds a real project of their own just like these.