370 likes | 629 Views
เกม PONG. เกม PONG. เกม PONG เกม Pong เป็นเกมคลาสสิกที่โด่งดังมากในอดีต ในครั้งแรกออกมาเป็นเกมตู้ก่อนประมาณ 1972 ต่อมาเมื่อได้รับความนิยมเพิ่มขึ้นในปี 1975 ทางบริษัท Atari จึงนำมาทำเป็นเกมคอนโซลสำหรับเล่นตามบ้านซึ่งทำให้บริษัท Atari ขายเครื่องได้มากกว่า 150,000 เครื่อง.
E N D
เกม PONG • เกม PONG • เกม Pong เป็นเกมคลาสสิกที่โด่งดังมากในอดีต ในครั้งแรกออกมาเป็นเกมตู้ก่อนประมาณ 1972 • ต่อมาเมื่อได้รับความนิยมเพิ่มขึ้นในปี 1975 ทางบริษัท Atari จึงนำมาทำเป็นเกมคอนโซลสำหรับเล่นตามบ้านซึ่งทำให้บริษัท Atari ขายเครื่องได้มากกว่า 150,000 เครื่อง
เปิดไฟล์ชื่อ 02-แบบฝึกหัด » 02-pong » Tutorial_pong
สร้าง Room • เริ่มต้นสร้าง room ขนาด 480 x 640 ซึ่งเกมของเราจะเปลี่ยนจากการเล่นในแนวนอนเหมือนอย่างต้นฉบับมาเป็นแนวตั้งบ้าง ส่วน Speed ตั้งไว้ที่ 60 เพื่อให้เกมดู Smooth ขึ้น
โหลดภาพ Sprite • นำภาพ Sprite ที่จะใช้ในเกมเข้ามาจากไฟล์ชื่อ 02-แบบฝึกหัด » 02-pong » sprites ซึ่งจะมีภาพ sp_p1 (ชื่อไฟล์ p1) แทนบาร์ของเรา, sp_p2 (ชื่อไฟล์ p2) แทนบาร์คู่ต่อสู้ และ sp-ball (ชื่อไฟล์ ball) แทนบอลที่ใช้เล่น สำหรับพื้นหลังใช้ background (ชื่อไฟล์ bg)
โหลดภาพ Sprite • สำหรับบาร์ทั้งสองภาพและบอลนั้น จุด origin ต้องตั้งให้อยู่กลางภาพ
สร้างฟอนต์ • สร้างฟอนต์ชื่อ font_score โดยใช้ฟอนต์ Arial • ขนาด 50 ตัวหนา
สร้างออบเจกต์ • หลังจากนั้นสร้างออบเจกต์เพื่อจะนำไปใช้เกมจำนวน 4 ตัว และตั้งค่าต่างๆ โดยเพิ่มออบเจกต์ทั้ง 4 ตัวนี้ลงไป ดังนี้
Object: p1 • Information about object: p1 • ออบเจกต์นี้คือบาร์ที่เราจะต้องบังคับ ซึ่งสามารถใช้เมาท์บังคับบาร์ให้เคลื่อนซ้ายขวาเพื่อไปรับบอลได้
Object: p1 • Sprite: sp_p1 • Solid: false • Visible: true • Depth: 0 • Persistent: false • Parent: <no parent> • Mask: <same as sprite>
Object: p1 • Step Event: • COMMENT: ให้บาร์เคลื่อนที่ตามเมาท์ • Move to position (mouse_x, y) • COMMENT: ทำให้บาร์ไม่ออกนอก room • If bbox_left is smaller than 0 • Set variable x to sprite_width/2 • If bbox_right is lager than room_width • Set variable x to room_width-(sprite_width/2)
Object: p2 • Information about object: p2 • เป็นบาร์ของคู่ต่อสู้ซึ่งจะต้องวิ่งมารับลูกเองตามเงื่อนไข
Object: p2 • Sprite: sp_p2 • Solid: false • Visible: true • Depth: 0 • Persistent: false • Parent: <no parent> • Mask: <same as sprite>
Object: p2 • Step Event: • COMMENT: เมื่อลูกผ่านครึ่ง room ด้านบนมาให้บาร์ฝ่ายตรงข้ามเคลื่อนเข้าหาบอล ถ้าบอลเลยครึ่ง room ด้านล่างก็ให้บาร์มาอยู่ตรงกลาง • If ball.y is smaller than room_height/2 • Perform a step towards position (ball.x, y) with • Speed 10 stop at slid only else • Perform a step towards position (room_width/2, y) with • Speed 3 stop at solid only • COMMENT: ทำให้บาร์ไม่ออกนอก room • If bbox_left is smaller than 0 • Set variable x to sprite_width/2 • If bbox_right is lager than room_width • Set variable x to room_width-(sprite_width/2)
Object: ball • Information about object: ball • บอลในเกมมีรายละเอียดดังต่อไปนี้ • Sprite: sp_ball • Solid: false • Visible: true • Depth: 0 • Persistent: false • Parent: <no parent> • Mask: <same as sprite>
Object: ball • Create Event: • COMMENT: สุ่มทิศทางปล่อยบอล • Start moving in directions 101000101 with speed set to 8
Object: ball • Step Event: • COMMENT: ควบคุมความเร็วไม่ให้เกิน 20 • If speed is larger than 20 • Set variable speed to 20
Object: ball • COMMENT: ทำให้ความเร็วเพิ่มขึ้นทีละ 1 • If vspeed is larger than 0 • Set variable speed relative to 1 • COMMENT: เมื่อบอลกระทบบาร์จะเด้งได้หลายทิศทางตามตำแหน่งที่กระทบ • Set variable direction to (other.x-x)+90
Object: ball • Collision Event with object p2: • COMMENT: ทำให้ความเร็วเพิ่มขึ้นทีละ 1 • If vspeed is smaller than 0 • Set variable speed relative to 1 • COMMENT: สุ่มทิศทางของบอลขณะเด้งกลับ • Set variable direction to - (random (120)+30)
Object: ball • Other Event: outside Room: • COMMENT: ผู้เล่นได้คะแนน • If y is smaller than 0 • Set variable global.score1 relative to 1 • Sleep 0 millisecond; redrawing the screen: true • Display message: you won !!! • COMMENT: ฝ่ายตรงข้ามได้คะแนน • If y is larger than room_height • Set variable global.score2 relative to 1 • Sleep 0 millisecond; redrawing the screen: true • Display message: you lose !!! • Restart the current room with transition effect • <no effect>
Object: ball • Other Event: Intersect Boundary: • COMMENT: เมื่อบอลชนขอบ room ซ้ายและขวาจะเด้งกลับ • If expression bbox_right>room_width && hspeed > 0 is true • Reverse horizontal direction • If expression bbox_left<0 && hspeed< 0 is true • Reverse horizontal direction
Object: show_score • Information about object: show_score • ใช้แสดงคะแนนทั้งสองฝ่าย • Sprite: <no sprite> • Solid: false • Visible: true • Depth: 0 • Persistent: false • Parent: <no parent> • Mask: <same as sprite>
Object: show_score • Other Event Game Start: • COMMENT: เมื่อเริ่มเกม ให้เซตคะแนนทั้งคู่เป็น 0 • Set variable global.score1 to 0 • Set variable global. score2 to 0
Object: show_score • Draw Event: • COMMENT: แสดงคะแนนทั้งสองฝ่ายบนจอ • Set the font for drawing text to font_score and align center • Set the drawing color to 16711680 at position (room_width/2,400) draw the value of: global.score1 • Set the font for drawing text to font_score and align center • Set the drawing color to 225 at position (room_width/2,150) draw the value of: global.score2
ที่ room properties กำหนดพื้นหลัง background0 ตามที่กำหนด
วางตำแหน่งออบเจกต์ใน Room • เมื่อสร้างออบเจกต์ทั้งหมดเสร็จเรียบร้อยแล้วจึงนำไปใส่ใน room ตามตำแหน่งที่กำหนด เป็นอันเสร็จเรียบร้อยและลองเล่นเกมดู