Giới thiệu sản phẩm

#code here
import pygame
from random import randint
pygame.init()
WIDTH = 800
HEIGHT = 600
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
AQUA = (0, 255, 255)
MAGENTA = (255, 0, 255)
root = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('first py"game"')
running = True
class Pad(pygame.sprite.Sprite):
    def __init__(self, x, y, color):
        super().__init__()
        self.x = x
        self.y = y
        self.color = color
        self.surface = pygame.Surface((30, 120))
        self.surface.fill(self.color)
        self.rect = self.surface.get_rect(topleft = (self.x, self.y))
    def update(self):
        if self.rect.y < 0:
            self.rect.y = 0
        if self.rect.y > (HEIGHT - 120):
            self.rect.y = (HEIGHT - 120)
class Ball(pygame.sprite.Sprite):
    def __init__(self, speed_x, speed_y, color):
        super().__init__()
        self.speed_x = speed_x
        self.speed_y = speed_y
        self.color = color
        self.surface = pygame.Surface((30, 30), pygame.SRCALPHA)
        
        self.rect = self.surface.get_rect(topleft = (400, 300))
        pygame.draw.circle(self.surface, self.color, (10, 10), 10)
    def update(self):
        self.rect.move_ip(self.speed_x, self.speed_y)
        if self.rect.y < 0 or self.rect.bottom > 600:
            self.speed_y *= -1
ball = Ball(1, -1, YELLOW)
pad_a = Pad(30, 240, RED)
pad_b = Pad(740, 240, BLUE)
all_pads = pygame.sprite.Group()
all_pads.add(pad_a, pad_b)
fps = 120
clock = pygame.time.Clock()
while running:
    clock.tick(fps)
    root.fill(GREEN)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            winner = ""
            running = False
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        pad_a.rect.move_ip(0, -1)
    if keys[pygame.K_s]:
        pad_a.rect.move_ip(0, 1)
    if keys[pygame.K_UP]:
        pad_b.rect.move_ip(0, -1)
    if keys[pygame.K_DOWN]:
        pad_b.rect.move_ip(0, 1)
    pygame.draw.line(root, WHITE, (400, 0), (400, 200), 30)
    pygame.draw.circle(root, WHITE, (400, 300), 110, 30)
    pygame.draw.line(root, WHITE, (400, 400), (400, 600), 30)
    root.blit(pad_a.surface, pad_a.rect)
    root.blit(pad_b.surface, pad_b.rect)
    all_pads.update()
    root.blit(ball.surface, ball.rect)
    ball.update()
    if 56 < ball.rect.x < 60 or 720 < ball.rect.x < 724:
        if pygame.sprite.spritecollide(ball, all_pads, False):
            ball.speed_x *= -1
    if ball.rect.x < -100 or ball.rect.x > 800:
        ball.rect.x = 400
        ball.rect.y = 300
        ball.speed_x *= -1
    pygame.display.update()
pygame.quit()
print(" " + winner)
#python #pygame #py"game" #ping_pong #pong #ball #ball_games

Hình ảnh sản phẩm
Hãy bình luận để nhặt 100 thóc nhé
Lê Ánh Ngọc

🌟🌟🌟 xin cậu đừng giỏi nữa

Trả lời 607d

Đăng nhập để tham gia bình luận


Trần Vĩ Tiến

Chất như nước cất ♥️♥️

Trả lời 607d

Đăng nhập để tham gia bình luận


Phạm Hoàng Dương

Holly Waka Molly gì mà đỉnh quá vậy bạn eiii 🔥🌞

Trả lời 607d

Đăng nhập để tham gia bình luận


Nguyễn Thị Yến Linh

chói chang quá✨

Trả lời 607d

Đăng nhập để tham gia bình luận


Phạm Quỳnh Như

🧎‍♀️vẫn cứ nà ô kê

Trả lời 607d

Đăng nhập để tham gia bình luận


Đăng nhập để tham gia bình luận

Thông tin tác giả

Địa vị cộng đồng: Nông dân

Sản phẩm liên quan

    Không có sản phẩm nào

Bạn muốn thử làm game không?