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
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

Sản phẩm cùng tác giả
Sản phẩm liên quan
Không có sản phẩm nào
🌟🌟🌟 xin cậu đừng giỏi nữa
Đăng nhập để tham gia bình luận
Chất như nước cất ♥️♥️
Đăng nhập để tham gia bình luận
Holly Waka Molly gì mà đỉnh quá vậy bạn eiii 🔥🌞
Đăng nhập để tham gia bình luận
chói chang quá✨
Đăng nhập để tham gia bình luận
🧎♀️vẫn cứ nà ô kê
Đăng nhập để tham gia bình luận
Đăng nhập để tham gia bình luận