Sure, here is a simple game of Pong in Python using the Pygame library:

```py
import random
import pygame, sys
from pygame.locals import *

pygame.init()
fps = pygame.time.Clock()

# colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)

# globals
WIDTH = 600
HEIGHT = 400
BALL_RADIUS = 20
PAD_WIDTH = 8
PAD_HEIGHT = 80
HALF_PAD_WIDTH = PAD_WIDTH / 2
HALF_PAD_HEIGHT = PAD_HEIGHT / 2
ball_pos = [0, 0]
ball_vel = [0, 0]
paddle1_vel = 0
paddle2_vel = 0
l_score = 0
r_score = 0

# canvas declaration
window = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
pygame.display.set_caption("Hello World")

# helper function that spawns a ball, returns a position vector and a velocity vector
# if right is True, spawn to the right, else spawn to the left
def ball_init(right):
    global ball_pos, ball_vel  # these are vectors stored as lists
    ball_pos = [WIDTH / 2, HEIGHT / 2]
    horz = random.randrange(2, 4)
    vert = random.randrange(1, 3)

    if right == False:
        horz = -horz

    ball_vel = [horz, -vert]


# define event handlers
def init():
    global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel, l_score, r_score  # these are floats
    global score1, score2  # these are ints
    paddle1_pos = [HALF_PAD_WIDTH - 1, HEIGHT / 2]
    paddle2_pos = [WIDTH + 1 - HALF_PAD_WIDTH, HEIGHT / 2]
    l_score = 0
    r_score = 0
    if random.randrange(0, 2) == 0:
        ball_init(True)
    else:
        ball_init(False)


# draw function of canvas
def draw(canvas):
    global paddle1_pos, paddle2_pos, ball_pos, ball_vel, l_score, r_score

    canvas.fill(BLACK)
    pygame.draw.line(canvas, WHITE, [WIDTH / 2, 0], [WIDTH / 2, HEIGHT], 1)
    pygame.draw.line(canvas, WHITE, [PAD_WIDTH, 0], [PAD_WIDTH, HEIGHT], 1)
    pygame.draw.line(
        canvas, WHITE, [WIDTH - PAD_WIDTH, 0], [WIDTH - PAD_WIDTH, HEIGHT], 1
    )
    pygame.draw.circle(canvas, WHITE, [WIDTH // 2, HEIGHT // 2], 70, 1)

    # update paddle's vertical position, keep paddle on the screen
    if paddle1_pos[1] > HALF_PAD_HEIGHT and paddle1_pos[1] < HEIGHT - HALF_PAD_HEIGHT:
        paddle1_pos[1] += paddle1_vel
    elif paddle1_pos[1] == HALF_PAD_HEIGHT and paddle1_vel > 0:
        paddle1_pos[1] += paddle1_vel
    elif paddle1_pos[1] == HEIGHT - HALF_PAD_HEIGHT and paddle1_vel < 0:
        paddle1_pos[1] += paddle1_vel

    if paddle2_pos[1] > HALF_PAD_HEIGHT and paddle2_pos[1] < HEIGHT - HALF_PAD_HEIGHT:
        paddle2_pos[1] += paddle2_vel
    elif paddle2_pos[1] == HALF_PAD_HEIGHT and paddle2_vel > 0:
        paddle2_pos[1] += paddle2_vel
    elif paddle2_pos[1] == HEIGHT - HALF_PAD_HEIGHT and paddle2_vel < 0:
        paddle2_pos[1] += paddle2_vel

    # update ball
    ball_pos[0] += int(ball_vel[0])
    ball_pos[1] += int(ball_vel[1])

    # draw paddles and ball
    pygame.draw.circle(canvas, RED, ball_pos, 20, 0)
    pygame.draw.polygon(
        canvas,
        GREEN,
        [
            [paddle1_pos[0] - HALF_PAD_WIDTH, paddle1_pos[1] - HALF_PAD_HEIGHT],
            [paddle1_pos[0] - HALF_PAD_WIDTH, paddle1_pos[1] + HALF_PAD_HEIGHT],
            [paddle1_pos[0] + HALF_PAD_WIDTH, paddle1_pos[1] + HALF_PAD_HEIGHT],
            [paddle1_pos[0] + HALF_PAD_WIDTH, paddle1_pos[1] - HALF_PAD_HEIGHT],
        ],
        0,
    )
    pygame.draw.polygon(
        canvas,
        GREEN,
        [
            [paddle2_pos[0] - HALF_PAD_WIDTH, paddle2_pos[1] - HALF_PAD_HEIGHT],
            [paddle2_pos[0] - HALF_PAD_WIDTH, paddle2_pos[1] + HALF_PAD_HEIGHT],
            [paddle2_pos[0] + HALF_PAD_WIDTH, paddle2_pos[1] + HALF_PAD_HEIGHT],
            [paddle2_pos[0] + HALF_PAD_WIDTH, paddle2_pos[1] - HALF_PAD_HEIGHT],
        ],
        0,
    )

    # ball collision check on top and bottom walls
    if int(ball_pos[1]) <= BALL_RADIUS:
        ball_vel[1] = -ball_vel[1]
    if int(ball_pos[1]) >= HEIGHT + 1 - BALL_RADIUS:
        ball_vel[1] = -ball_vel[1]

    # ball collison check on gutters or paddles
    if int(ball_pos[0]) <= BALL_RADIUS + PAD_WIDTH and int(ball_pos[1]) in range(
        paddle1_pos[1] - HALF_PAD_HEIGHT, paddle1_pos[1] + HALF_PAD_HEIGHT, 1
    ):
        ball_vel[0] = -ball_vel[0]
        ball_vel[0] *= 1.1
        ball_vel[1] *= 1.1
    elif int(ball_pos[0]) <= BALL_RADIUS + PAD_WIDTH:
        r_score += 1
        ball_init(True)

    if int(ball_pos[0]) >= WIDTH + 1 - BALL_RADIUS - PAD_WIDTH and int(
        ball_pos[1]
    ) in range(paddle2_pos[1] - HALF_PAD_HEIGHT, paddle2_pos[1] + HALF_PAD_HEIGHT, 1):
        ball_vel[0] = -ball_vel[0]
        ball_vel[0] *= 1.1
        ball_vel[1] *= 1.1
    elif int(ball_pos[0]) >= WIDTH + 1 - BALL_RADIUS - PAD_WIDTH:
        l_score += 1
        ball_init(False)

    # update scores
    myfont1 = pygame.font.SysFont("Comic Sans MS", 20)
    label1 = myfont1.render("Score " + str(l_score), 1, (255, 255, 0))
    canvas.blit(label1, (50, 20))

    myfont2 = pygame.font.SysFont("Comic Sans MS", 20)
    label2 = myfont2.render("Score " + str(r_score), 1, (255, 255, 0))
    canvas.blit(label2, (470, 20))


# keydown handler
def keydown(event):
    global paddle1_vel, paddle2_vel

    if event.key == K_UP:
        paddle2_vel = -8
    elif event.key == K_DOWN:
        paddle2_vel = 8
    elif event.key == K_w:
        paddle1_vel = -8
    elif event.key == K_s:
        paddle1_vel = 8


# keyup handler
def keyup(event):
    global paddle1_vel, paddle2_vel

    if event.key in (K_w, K_s):
        paddle1_vel = 0
    elif event.key in (K_UP, K_DOWN):
        paddle2_vel = 0


init()


# game loop
while True:

    draw(window)

    for event in pygame.event.get():

        if event.type == KEYDOWN:
            keydown(event)
        elif event.type == KEYUP:
            keyup(event)
        elif event.type == QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.update()
    fps.tick(60)
```
แน่นอนว่านี่คือเกมง่ายๆ ของ Pong ใน Python โดยใช้ไลบรารี Pygame: ```py import Random import pygame, sys from pygame.locals import * pygame.init() fps = pygame.time.Clock() # colours WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLACK = (0, 0, 0) # globals WIDTH = 600 HEIGHT = 400 BALL_RADIUS = 20 PAD_WIDTH = 8 PAD_HEIGHT = 80 HALF_PAD_WIDTH = PAD_WIDTH / 2 HALF_PAD_HEIGHT = PAD_HEIGHT / 2 ball_pos = [0, 0] ball_vel = [0, 0] ไม้พาย1_vel = 0 ไม้พาย2_vel = 0 l_score = 0 r_score = 0 # หน้าต่างประกาศผ้าใบ = pygame.display.set_mode((WIDTH , HEIGHT), 0, 32) pygame.display.set_caption("Hello World") # ฟังก์ชันตัวช่วยที่สร้างลูกบอล ส่งกลับเวกเตอร์ตำแหน่งและเวกเตอร์ความเร็ว # หากด้านขวาเป็น True ให้วางไข่ไปทางขวา มิฉะนั้นจะวางไข่ไปทาง def ซ้าย ball_init (ขวา): global ball_pos, ball_vel # เหล่านี้เป็นเวกเตอร์ที่เก็บไว้เป็นรายการ ball_pos = [WIDTH / 2, HEIGHT / 2] horz = Random.randrange (2, 4) vert = Random.randrange (1, 3) ถ้า right == False: horz = -horz ball_vel = [horz, -vert] # กำหนดตัวจัดการเหตุการณ์ def init(): global paddle1_pos, padd2_pos, padd1_vel, padd2_vel, l_score, r_score # สิ่งเหล่านี้ลอยอยู่ global Score1, Score2 # สิ่งเหล่านี้คือ ints Paddle1_pos = [HALF_PAD_WIDTH - 1, HEIGHT / 2] Paddle2_pos = [WIDTH + 1 - HALF_PAD_WIDTH, HEIGHT / 2] l_score = 0 r_score = 0 ถ้า Random.randrange(0, 2) == 0: ball_init(True) else: ball_init (False) # ฟังก์ชั่นการวาดของ canvas def Draw (canvas): Paddle1_pos, Paddle2_pos, ball_pos, ball_vel, l_score, r_score canvas.fill (BLACK) pygame.draw.line (canvas, WHITE, [WIDTH / 2, 0], [WIDTH / 2, HEIGHT], 1) pygame.draw.line(ผ้าใบ, สีขาว, [PAD_WIDTH, 0], [PAD_WIDTH, HEIGHT], 1) pygame.draw.line( ผ้าใบ, สีขาว, [WIDTH - PAD_WIDTH, 0 ], [WIDTH - PAD_WIDTH, HEIGHT], 1 ) pygame.draw.circle(canvas, WHITE, [WIDTH // 2, HEIGHT // 2], 70, 1) # อัปเดตตำแหน่งแนวตั้งของไม้พาย ให้วางไม้พายบนหน้าจอหาก Paddle1_pos[1] > HALF_PAD_HEIGHT และ Paddle1_pos[1] < HEIGHT - HALF_PAD_HEIGHT: ไม้พาย1_pos[1] += ไม้พาย1_vel elif ไม้พาย1_pos[1] == HALF_PAD_HEIGHT และไม้พาย1_vel > 0: ไม้พาย1_pos[1] += ไม้พาย1_vel elif ไม้พาย1_pos[1] == HEIGHT - HALF_PAD_HEIGHT และ Paddle1_vel < 0: Paddle1_pos[1] += Paddle1_vel ถ้า Paddle2_pos[1] > HALF_PAD_HEIGHT และ Paddle2_pos[1] < HEIGHT - HALF_PAD_HEIGHT: Paddle2_pos[1] += Paddle2_vel elif Paddle2_pos[1] == HALF_PAD_HEIGHT และ Paddle2_vel > 0: Paddle2_pos[1] += Paddle2_vel elif Paddle2_pos[1] == HEIGHT - HALF_PAD_HEIGHT และ Paddle2_vel < 0: Paddle2_pos[1] += Paddle2_vel # อัปเดตบอล ball_pos[0] += int(ball_vel[0]) ball_pos[1 ] += int(ball_vel[1]) # วาดไม้พายและลูกบอล pygame.draw.circle(canvas, RED, ball_pos, 20, 0) pygame.draw.polygon( canvas, GREEN, [ [paddle1_pos[0] - HALF_PAD_WIDTH, Paddle1_pos [1] - half_pad_height], [paddle1_pos [0] - half_pad_width, paddle1_pos [1] + half_pad_height], [paddle1_pos [0] + half_pad_width, paddle1_pos [1] _pos [ 1] - HALF_PAD_HEIGHT], ], 0, ) pygame.draw.polygon( ผ้าใบ, สีเขียว, [ [paddle2_pos[0] - HALF_PAD_WIDTH, Paddle2_pos[1] - HALF_PAD_HEIGHT], [paddle2_pos[0] - HALF_PAD_WIDTH, Paddle2_pos[1] + HALF_PAD_HEIGHT], [paddle2_pos[0] + HALF_PAD_WIDTH, Paddle2_pos[1] + HALF_PAD_HEIGHT], [paddle2_pos[0] + HALF_PAD_WIDTH, Paddle2_pos[1] - HALF_PAD_HEIGHT], ], 0, ) # การตรวจสอบการชนกันของลูกบอลบนผนังด้านบนและด้านล่าง ถ้า int(ball_pos[1]) <= BALL_RADIUS: ball_vel[1] = -ball_vel[1] ถ้า int(ball_pos[1]) >= HEIGHT + 1 - BALL_RADIUS: ball_vel[1] = -ball_vel[1] # บอล ตรวจสอบการชนกันของรางน้ำหรือไม้พายถ้า int(ball_pos[0]) <= BALL_RADIUS + PAD_WIDTH และ int(ball_pos[1]) อยู่ในช่วง ( Paddle1_pos[1] - HALF_PAD_HEIGHT, Paddle1_pos[1] + HALF_PAD_HEIGHT, 1 ): ball_vel[0 ] = -ball_vel[0] ball_vel[0] *= 1.1 ball_vel[1] *= 1.1 elif int(ball_pos[0]) <= BALL_RADIUS + PAD_WIDTH: r_score += 1 ball_init(True) ถ้า int(ball_pos[0] ) >= WIDTH + 1 - BALL_RADIUS - PAD_WIDTH และ int( ball_pos[1] ) อยู่ในช่วง (paddle2_pos[1] - HALF_PAD_HEIGHT, paddle2_pos[1] + HALF_PAD_HEIGHT, 1): ball_vel[0] = -ball_vel[0] ball_vel[ 0] *= 1.1 ball_vel[1] *= 1.1 elif int(ball_pos[0]) >= WIDTH + 1 - BALL_RADIUS - PAD_WIDTH: l_score += 1 ball_init(False) # อัปเดตคะแนน myfont1 = pygame.font.SysFont(" การ์ตูน Sans MS", 20) label1 = myfont1.render("Score " + str(l_score), 1, (255, 255, 0)) canvas.blit(label1, (50, 20)) myfont2 = pygame.font SysFont("Comic Sans MS", 20) label2 = myfont2.render("Score " + str(r_score), 1, (255, 255, 0)) canvas.blit(label2, (470, 20)) # ตัวจัดการคีย์ดาวน์ def keydown (เหตุการณ์): ทั่วโลก Paddle1_vel, Paddle2_vel ถ้า event.key == K_UP: Paddle2_vel = -8 elif event.key == K_DOWN: Paddle2_vel = 8 elif event.key == K_w: Paddle1_vel = -8 elif event.key = = K_s: paddle1_vel = 8 # ตัวจัดการคีย์อัพ def keyup (เหตุการณ์): Paddle1_vel ทั่วโลก, Paddle2_vel ถ้า event.key in (K_w, K_s): Paddle1_vel = 0 elif event.key in (K_UP, K_DOWN): Paddle2_vel = 0 init() # game loop ในขณะที่ True: Draw(window) สำหรับเหตุการณ์ใน pygame.event.get(): if event.type == KEYDOWN: keydown(event) elif event.type == KEYUP: keyup(event) elif event.type = = ออกจาก: pygame.quit() sys.exit() pygame.display.update() fps.tick(60) ```
spam fails_task lang_mismatch pii not_appropriate hate_speech sexual_content quality toxicity humor helpfulness creativity violence