import pygame
import random
# กำหนดสีที่ใช้ในเกม
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# ขนาดหน้าจอ
WIDTH = 800
HEIGHT = 600
# สร้างหน้าจอเกม
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("เกมยิงปืนเด็ก")
# ตั้งค่า FPS (เฟรมต่อวินาที)
clock
= pygame
.time.Clock
()
# สร้างตัวละคร (เด็ก) และลูกบอล
player_width = 50
player_height = 50
player_x = WIDTH // 2 - player_width // 2
player_y = HEIGHT - player_height - 10
player_speed = 5
bullet_width = 10
bullet_height = 20
bullet_speed = 7
bullets = []
target_width = 50
target_height = 50
target_x = random.randint(0, WIDTH - target_width)
target_y = random.randint(0, HEIGHT // 2)
target_speed = 3
# คะแนน
score = 0
font = pygame.font.SysFont('Arial', 30)
def draw_player(x, y):
pygame.draw.rect(screen, BLUE, (x, y, player_width, player_height))
def draw_bullet(bullet):
pygame.draw.rect(screen, RED, (bullet[0], bullet[1], bullet_width, bullet_height))
def draw_target(x, y):
pygame.draw.rect(screen, GREEN, (x, y, target_width, target_height))
def display_score(score):
score_text = font.render(f"คะแนน: {score}", True, BLACK)
screen.blit(score_text, (10, 10))
def main():
global player_x, bullets, score, target_x, target_y
running = True
while running:
screen.fill(WHITE)
# ตรวจสอบอีเวนต์
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# เคอร์เซอร์
keys
= pygame
.key.get_pressed
() if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < WIDTH - player_width:
player_x += player_speed
if keys[pygame.K_SPACE]:
# ยิงลูกบอล
bullets.append([player_x + player_width // 2 - bullet_width // 2, player_y])
# อัพเดทตำแหน่งลูกบอล
for bullet in bullets[:]:
bullet[1] -= bullet_speed
if bullet[1] < 0:
bullets.remove(bullet)
# อัพเดทตำแหน่งเป้าหมาย
target_y += target_speed
if target_y > HEIGHT:
target_y = random.randint(0, HEIGHT // 2)
target_x = random.randint(0, WIDTH - target_width)
# ตรวจสอบการยิงโดนเป้า
for bullet in bullets[:]:
if (bullet[0] > target_x and bullet[0] < target_x + target_width and
bullet[1] > target_y and bullet[1] < target_y + target_height):
bullets.remove(bullet)
score += 10
target_y = random.randint(0, HEIGHT // 2)
target_x = random.randint(0, WIDTH - target_width)
# วาดสิ่งต่างๆ
draw_player(player_x, player_y)
for bullet in bullets:
draw_bullet(bullet)
draw_target(target_x, target_y)
display_score(score)
# อัพเดทหน้าจอ
pygame.display.update()
# ตั้งค่า FPS
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()
aW1wb3J0IHB5Z2FtZQppbXBvcnQgcmFuZG9tCgojIOC4geC4s+C4q+C4meC4lOC4quC4teC4l+C4teC5iOC5g+C4iuC5ieC5g+C4meC5gOC4geC4oQpXSElURSA9ICgyNTUsIDI1NSwgMjU1KQpCTEFDSyA9ICgwLCAwLCAwKQpSRUQgPSAoMjU1LCAwLCAwKQpCTFVFID0gKDAsIDAsIDI1NSkKCiMg4LiC4LiZ4Liy4LiU4Lir4LiZ4LmJ4Liy4LiI4LitCldJRFRIID0gODAwCkhFSUdIVCA9IDYwMAoKIyDguKrguKPguYnguLLguIfguKvguJnguYnguLLguIjguK3guYDguIHguKEKcHlnYW1lLmluaXQoKQpzY3JlZW4gPSBweWdhbWUuZGlzcGxheS5zZXRfbW9kZSgoV0lEVEgsIEhFSUdIVCkpCnB5Z2FtZS5kaXNwbGF5LnNldF9jYXB0aW9uKCLguYDguIHguKHguKLguLTguIfguJvguLfguJnguYDguJTguYfguIEiKQoKIyDguJXguLHguYnguIfguITguYjguLIgRlBTICjguYDguJ/guKPguKHguJXguYjguK3guKfguLTguJnguLLguJfguLUpCmNsb2NrID0gcHlnYW1lLnRpbWUuQ2xvY2soKQoKIyDguKrguKPguYnguLLguIfguJXguLHguKfguKXguLDguITguKMgKOC5gOC4lOC5h+C4gSkg4LmB4Lil4Liw4L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guITguLDguYHguJnguJk6IHtzY29yZX0iLCBUcnVlLCBCTEFDSykKICAgIHNjcmVlbi5ibGl0KHNjb3JlX3RleHQsICgxMCwgMTApKQoKZGVmIG1haW4oKToKICAgIGdsb2JhbCBwbGF5ZXJfeCwgYnVsbGV0cywgc2NvcmUsIHRhcmdldF94LCB0YXJnZXRfeQogICAgcnVubmluZyA9IFRydWUKICAgIHdoaWxlIHJ1bm5pbmc6CiAgICAgICAgc2NyZWVuLmZpbGwoV0hJVEUpCgogICAgICAgICMg4LiV4Lij4Lin4LiI4Liq4Lit4Lia4Lit4Li14LmA4Lin4LiZ4LiV4LmMCiAgICAgICAgZm9yIGV2ZW50IGluIHB5Z2FtZS5ldmVudC5nZXQoKToKICAgICAgICAgICAgaWYgZXZlbnQudHlwZSA9PSBweWdhbWUuUVVJVDoKICAgICAgICAgICAgICAgIHJ1bm5pbmcgPSBGYWxzZQoKICAgICAgICAjIOC5gOC4hOC4reC4o+C5jOC5gOC4i+C4reC4o+C5jAogICAgICAgIGtleXMgPSBweWdhbWUua2V5LmdldF9wcmVzc2VkKCkKICAgICAgICBpZiBrZXlzW3B5Z2FtZS5LX0xFRlRdIGFuZCBwbGF5ZXJfeCA+IDA6CiAgICAgICAgICAgIHBsYXllcl94IC09IHBsYXllcl9zcGVlZAogICAgICAgIGlmIGtleXNbcHlnYW1lLktfUklHSFRdIGFuZCBwbGF5ZXJfeCA8IFdJRFRIIC0gcGxheWVyX3dpZHRoOgogICAgICAgICAgICBwbGF5ZXJfeCArPSBwbGF5ZXJfc3BlZWQKICAgICAgICBpZiBrZXlzW3B5Z2FtZS5LX1NQQUNFXToKICAgICAgICAgICAgIyDguKLguLTguIfguKXguLnguIHguJrguK3guKUKICAgICAgICAgICAgYnVsbGV0cy5hcHBlbmQoW3BsYXllcl94ICsgcGxheWVyX3dpZHRoIC8vIDIgLSBidWxsZXRfd2lkdGggLy8gMiwgcGxheWVyX3ldKQoKICAgICAgICAjIOC4reC4seC4nuC5gOC4lOC4l+C4leC4s+C5geC4q+C4meC5iOC4h+C4peC4ueC4geC4muC4reC4pQogICAgICAgIGZvciBidWxsZXQgaW4gYnVsbGV0c1s6XToKICAgICAgICAgICAgYnVsbGV0WzFdIC09IGJ1bGxldF9zcGVlZAogICAgICAgICAgICBpZiBidWxsZXRbMV0gPCAwOgogICAgICAgICAgICAgICAgYnVsbGV0cy5yZW1vdmUoYnVsbGV0KQoKICAgICAgICAjIOC4reC4seC4nuC5gOC4lOC4l+C4leC4s+C5geC4q+C4meC5iOC4h+C5gOC4m+C5ieC4suC4q+C4oeC4suC4ogogICAgICAgIHRhcmdldF95ICs9IHRhcmdldF9zcGVlZAogICAgICAgIGlmIHRhcmdldF95ID4gSEVJR0hUOgogICAgICAgICAgICB0YXJnZXRfeSA9IHJhbmRvbS5yYW5kaW50KDAsIEhFSUdIVCAvLyAyKQogICAgICAgICAgICB0YXJnZXRfeCA9IHJhbmRvbS5yYW5kaW50KDAsIFdJRFRIIC0gdGFyZ2V0X3dpZHRoKQoKICAgICAgICAjIOC4leC4o+C4p+C4iOC4quC4reC4muC4geC4suC4o+C4ouC4tOC4h+C5guC4lOC4meC5gOC4m+C5ieC4sgogICAgICAgIGZvciBidWxsZXQgaW4gYnVsbGV0c1s6XToKICAgICAgICAgICAgaWYgKGJ1bGxldFswXSA+IHRhcmdldF94IGFuZCBidWxsZXRbMF0gPCB0YXJnZXRfeCArIHRhcmdldF93aWR0aCBhbmQKICAgICAgICAgICAgICAgIGJ1bGxldFsxXSA+IHRhcmdldF95IGFuZCBidWxsZXRbMV0gPCB0YXJnZXRfeSArIHRhcmdldF9oZWlnaHQpOgogICAgICAgICAgICAgICAgYnVsbGV0cy5yZW1vdmUoYnVsbGV0KQogICAgICAgICAgICAgICAgc2NvcmUgKz0gMTAKICAgICAgICAgICAgICAgIHRhcmdldF95ID0gcmFuZG9tLnJhbmRpbnQoMCwgSEVJR0hUIC8vIDIpCiAgICAgICAgICAgICAgICB0YXJnZXRfeCA9IHJhbmRvbS5yYW5kaW50KDAsIFdJRFRIIC0gdGFyZ2V0X3dpZHRoKQoKICAgICAgICAjIOC4p+C4suC4lOC4quC4tOC5iOC4h+C4leC5iOC4suC4h+C5hgogICAgICAgIGRyYXdfcGxheWVyKHBsYXllcl94LCBwbGF5ZXJfeSkKICAgICAgICBmb3IgYnVsbGV0IGluIGJ1bGxldHM6CiAgICAgICAgICAgIGRyYXdfYnVsbGV0KGJ1bGxldCkKICAgICAgICBkcmF3X3RhcmdldCh0YXJnZXRfeCwgdGFyZ2V0X3kpCiAgICAgICAgZGlzcGxheV9zY29yZShzY29yZSkKCiAgICAgICAgIyDguK3guLHguJ7guYDguJTguJfguKvguJnguYnguLLguIjguK0KICAgICAgICBweWdhbWUuZGlzcGxheS51cGRhdGUoKQoKICAgICAgICAjIOC4leC4seC5ieC4h+C4hOC5iOC4siBGUFMKICAgICAgICBjbG9jay50aWNrKDYwKQoKICAgIHB5Z2FtZS5xdWl0KCkKCmlmIF9fbmFtZV9fID09ICJfX21haW5fXyI6CiAgICBtYWluKCk=
import pygame
import random
# กำหนดสีที่ใช้ในเกม
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# ขนาดหน้าจอ
WIDTH = 800
HEIGHT = 600
# สร้างหน้าจอเกม
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("เกมยิงปืนเด็ก")
# ตั้งค่า FPS (เฟรมต่อวินาที)
clock = pygame.time.Clock()
# สร้างตัวละคร (เด็ก) และลูกบอล
player_width = 50
player_height = 50
player_x = WIDTH // 2 - player_width // 2
player_y = HEIGHT - player_height - 10
player_speed = 5
bullet_width = 10
bullet_height = 20
bullet_speed = 7
bullets = []
target_width = 50
target_height = 50
target_x = random.randint(0, WIDTH - target_width)
target_y = random.randint(0, HEIGHT // 2)
target_speed = 3
# คะแนน
score = 0
font = pygame.font.SysFont('Arial', 30)
def draw_player(x, y):
pygame.draw.rect(screen, BLUE, (x, y, player_width, player_height))
def draw_bullet(bullet):
pygame.draw.rect(screen, RED, (bullet[0], bullet[1], bullet_width, bullet_height))
def draw_target(x, y):
pygame.draw.rect(screen, GREEN, (x, y, target_width, target_height))
def display_score(score):
score_text = font.render(f"คะแนน: {score}", True, BLACK)
screen.blit(score_text, (10, 10))
def main():
global player_x, bullets, score, target_x, target_y
running = True
while running:
screen.fill(WHITE)
# ตรวจสอบอีเวนต์
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# เคอร์เซอร์
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < WIDTH - player_width:
player_x += player_speed
if keys[pygame.K_SPACE]:
# ยิงลูกบอล
bullets.append([player_x + player_width // 2 - bullet_width // 2, player_y])
# อัพเดทตำแหน่งลูกบอล
for bullet in bullets[:]:
bullet[1] -= bullet_speed
if bullet[1] < 0:
bullets.remove(bullet)
# อัพเดทตำแหน่งเป้าหมาย
target_y += target_speed
if target_y > HEIGHT:
target_y = random.randint(0, HEIGHT // 2)
target_x = random.randint(0, WIDTH - target_width)
# ตรวจสอบการยิงโดนเป้า
for bullet in bullets[:]:
if (bullet[0] > target_x and bullet[0] < target_x + target_width and
bullet[1] > target_y and bullet[1] < target_y + target_height):
bullets.remove(bullet)
score += 10
target_y = random.randint(0, HEIGHT // 2)
target_x = random.randint(0, WIDTH - target_width)
# วาดสิ่งต่างๆ
draw_player(player_x, player_y)
for bullet in bullets:
draw_bullet(bullet)
draw_target(target_x, target_y)
display_score(score)
# อัพเดทหน้าจอ
pygame.display.update()
# ตั้งค่า FPS
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()