เรียนภาษา Python ด้วยเกม Code Combat - Part 4
สวัสดีครับ มาต่อกันที่ Part 4 วันนี้จะเป็นการตะลุยด่านใหม่ละครับ และก็เคลียร์ Game Development ของเก่าให้จบ สำหรับ Part อื่นๆ ดูได้ท้ายบทความ หรือดู Playlist จาก video ได้เลยครับ
Game Development
ในการออกแบบเกม เราสามารถกำหนดค่าพลังให้ hero
ได้ ตัวอย่างเช่น เพิ่ม ความเร็วในการเดิม เพิ่มเลือก เพิ่มค่าพลังโจมตี ตามชื่อ property
ที่เราต้องการ
player = game.spawnPlayerXY("goliath", 20,20)
player.maxSpeed = 12
player.maxHealth = 600
player.health = 600
player.attackDamage = 9001
เราสามารถกำหนดทิศทาง (direction) ส่ิงที่เราสร้างขึ้นมาได้
spew = game.spawnXY("fire-spewer", 10, 10)
spew.direction = "horizontal"
# หรือ vertical
spew.direction = "vertical"
เราสามารถเก็บข้อมูลลง db (database) ได้ เช่น
db.add("plays", 1)
เกม มีการโชว์หน้า stat (HUD) ด้วยคำสั่ง
ui.track(db, "plays")
ui.track(db, "wins")
ui.track(db, "total defeated")
Backwoods Forest
พอมาเล่นด่านนี้ ตัว hero
เราจะมีความสามารถในการเดินมากกว่าแค่ ขึ้น ลง ซ้าย ขวา
hero.moveUp()
hero.moveRight()
hero.moveDown()
hero.moveLeft()
คือเราสามารถเคลื่อนที่ ไปต่ำแหน่ง x, y ได้เลย แบบนี้
hero.moveXY(x, y)
If Statement
เราใช้ If Statement ในการเช็คเงื่อนไข ถ้าเงื่อนไขเป็น จริง (true) ก็จะทำงาน แต่ถ้าเป็น เท็จ (false) ก็จะข้ามไป ตัวอย่างเช่น คำสั่งด้านล่าง หา findNearestEnemy()
- ถ้าเจอ enemy แสดงว่า เป็น true ก็จะทำการ
attack()
- แต่ถ้าไม่เจอ ก็จะเป็น false ไม่ต้องทำอะไร
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
If-Else
เราสามารถใช้ else
ทำคำสั่ง ถ้าไม่ตรงตามเงื่อนไข if
ที่เราตั้งไว้ เช่น หา enemy ถ้าเจอ ก็ทำคำสั่ง 1 ถ้าไม่เจอ ทำคำสั่ง 2 เป็นต้น
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
else:
hero.say("I don't see an enemy!")
Else If
นอกจากนั้น เรายังสามารถตั้ง if condition ต่อๆ กันได้ เช่น
enemy = hero.findNearestEnemy()
if not enemy:
# If there is *not* an enemy, then move...
hero.moveXY(30, 30)
elif enemy.type is "thrower":
# ... else if the enemy's type *equals* "thrower", then attack...
hero.attack(enemy)
elif hero.isReady("cleave") and hero.distanceTo(enemy) < 10:
# ... else if "cleave" is ready *and* the distance is *less than* 10m, then cleave...
hero.cleave(enemy)
else:
# ... else, shield.
hero.shield()
Comparison Operator
เวลาที่เราจะใช้เงื่อนไข ในการเช็ค x = y หรือไม่ ใน If Condition เราจะใช้ ==
ไม่ใช่ =
==
ใช้ในการเปรียบเทียบใน If Statement=
ใช้ assign ค่าให้ตัวแปร
ตัวอย่างจากแบบฝึกในเกม
# If-statement code only runs when the if’s condition is true.
# Fix all the if-statements to beat the level.
# == means "is equal to".
if 1 + 1 + 1 == 4: # ∆ Make this false.
hero.moveXY(5, 15) # Move to the first mines.
if 2 + 2 == 4: # ∆ Make this true.
hero.moveXY(15, 40) # Move to the first gem.
# != means "is not equal to".
if 2 + 2 != 5: # ∆ Make this true.
hero.moveXY(25, 15) # Move to the second gem.
# < means "is less than".
if 2 + 2 < 5: # ∆ Make this true.
enemy = hero.findNearestEnemy()
hero.attack(enemy)
if 2 < 1: # ∆ Make this false.
hero.moveXY(40, 55)
if False: # ∆ Make this false.
hero.moveXY(50, 10)
if True: # ∆ Make this true.
hero.moveXY(55, 25)
Boolean
Boolean คือ data ชนิดหนึ่ง มีค่าเป็น True
หรือ False
ตอนอื่นๆ



