parent
4f9d832a39
commit
ccfe1604ea
@ -1,2 +1,7 @@
|
||||
# python_billiards
|
||||
# python-台球
|
||||
|
||||
## 文件结构
|
||||
|
||||
### common
|
||||
|
||||
该文件用于存放一些游戏中的基类和继承类,
|
@ -1,15 +1,11 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class Category(Enum):
|
||||
CUE_BALL = 0
|
||||
RED_BALL = 1
|
||||
YELLOW_BALL = 2
|
||||
GREEN_BALL = 3
|
||||
BROWN_BALL = 4
|
||||
BLUE_BALL = 5
|
||||
PINK_BALL = 6
|
||||
BLACK_BALL = 7
|
||||
COLORED_BALLS = [_ for _ in range(2, 8)]
|
||||
pass
|
||||
RED_BALL = 1
|
||||
YELLOW_BALL = 2
|
||||
CLAN_BALL = 3
|
||||
COFFEE_BALL = 4
|
||||
BLUE_BALL = 5
|
||||
PINK_BALL = 6
|
||||
BLACK_BALL = 7
|
||||
CUE_BALL = 8
|
||||
BALL = 9
|
||||
|
||||
COLORED_BALLS = [_ for _ in range(2, 8)]
|
||||
|
@ -1,76 +1,4 @@
|
||||
import pygame
|
||||
import pymunk
|
||||
from common.game.game import Game
|
||||
|
||||
from common import drawableItem
|
||||
from common.game.billiards.baseBilliard import BaseBilliard
|
||||
from common.game.cue import Cue
|
||||
from common.game.table import Table
|
||||
from config.sys import *
|
||||
from enums.Color import *
|
||||
from enums.category import *
|
||||
|
||||
|
||||
def new_ball(space, pos):
|
||||
ball = BaseBilliard(0, Category.BLACK_BALL, BLACK, pos)
|
||||
body = ball.rigid_body
|
||||
shape = ball.shape
|
||||
space.add(body, shape)
|
||||
balls.append(ball)
|
||||
return shape
|
||||
|
||||
|
||||
table = Table()
|
||||
balls: list[BaseBilliard] = [BaseBilliard(0, Category.CUE_BALL, WHITE, (200, 150))]
|
||||
cue = Cue(balls[0])
|
||||
|
||||
pygame.init()
|
||||
window = pygame.display.set_mode(SCREEN_SIZE)
|
||||
pygame.display.set_caption(TITLE)
|
||||
clock = pygame.time.Clock()
|
||||
space = pymunk.Space()
|
||||
space.gravity = (0, 0)
|
||||
|
||||
for _ in range(6):
|
||||
space.add(table.cushing_body[_], table.cushing_shape[_])
|
||||
space.add(balls[0].rigid_body, balls[0].shape)
|
||||
|
||||
force = 5
|
||||
add_force = False
|
||||
|
||||
while True:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
pygame.quit()
|
||||
exit()
|
||||
if event.type == pygame.MOUSEMOTION:
|
||||
cue.mouse_pos = event.pos
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
if event.button == 1:
|
||||
add_force = True
|
||||
if event.type == pygame.MOUSEBUTTONUP:
|
||||
if event.button == 1:
|
||||
pos_x = event.pos[0] - (SCREEN_WIDTH - TABLE_WIDTH) / 2
|
||||
pos_y = event.pos[1] - (SCREEN_HEIGHT - TABLE_HEIGHT) / 2
|
||||
balls[0].rigid_body.velocity = pymunk.Vec2d(pos_x - balls[0].rigid_body.position.x,
|
||||
pos_y - balls[0].rigid_body.position.y).scale_to_length(force)
|
||||
add_force = False
|
||||
force = 5
|
||||
if event.button == 3:
|
||||
pos_x = event.pos[0] - (SCREEN_WIDTH - TABLE_WIDTH) / 2
|
||||
pos_y = event.pos[1] - (SCREEN_HEIGHT - TABLE_HEIGHT) / 2
|
||||
new_ball(space, (pos_x, pos_y))
|
||||
# balls[0].rigid_body.velocity = (256, 0)
|
||||
if add_force:
|
||||
force += 5
|
||||
if force >= 512:
|
||||
force = 512
|
||||
window.fill(WHITE)
|
||||
table.goal(space, balls)
|
||||
table.draw(window)
|
||||
cue.draw(window)
|
||||
for _ in balls:
|
||||
_.resistance()
|
||||
_.draw(window)
|
||||
space.step(1 / FPS)
|
||||
pygame.display.update()
|
||||
clock.tick(FPS)
|
||||
game = Game()
|
||||
game.start()
|
||||
|
Loading…
Reference in new issue