本文整理汇总了Python中ball.Ball类的典型用法代码示例。如果您正苦于以下问题:Python Ball类的具体用法?Python Ball怎么用?Python Ball使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Ball类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Game
class Game():
def __init__(self):
self.window = curses.initscr()
self.window.nodelay(True)
self.window.keypad(True)
curses.cbreak()
SCRH, SCRW = self.window.getmaxyx()
self.ball = Ball(2, 2)
self.paddle1 = Paddle(0, 0, 5, [curses.KEY_LEFT, curses.KEY_RIGHT])
self.paddle2 = Paddle(0, SCRH - 1, 5, [97, 100])
self.scorex = 0
self.scorey = 0
self.score = Label(1, 1, "Score: 0:0")
def loop(self):
while True:
key = self.window.getch()
self.ball.update(self.window, [self.paddle1, self.paddle2])
self.paddle1.update(self.window, key)
self.paddle2.update(self.window, key)
self.window.clear()
draworder = sorted([self.ball, self.paddle1, self.paddle2, self.score], key=lambda o: o.x)
for o in draworder:
o.draw(self.window)
self.window.refresh()
curses.flushinp()
curses.napms(1000 / TICKRATE)
示例2: add_ball
def add_ball(self, release_instantly=False):
ball = Ball(self.ball_image, (230+82, 625), Vector(-3,-5), self.is_time_distortion_field_active, attached=True, attach_pos=self.balls_on_paddle)
self.balls.add(ball)
self.balls_on_paddle += 1
if release_instantly:
ball.update(1, self.paddle, self.balls, self.blocks, self.invulnerable_blocks)
self.release_a_ball()
示例3: handle_event
def handle_event(self, event):
if event.type == pygame.QUIT:
self._running = False
if event.type == pygame.MOUSEBUTTONDOWN:
ball = Ball(self.screen.get_width()/2,self.screen.get_height()/2,20,20,self)
ball.setSpeed(random.random()*14-7, random.random()*14-7)
self.balls.append(ball)
示例4: __init__
def __init__(self, stage, pos_x, pos_y, circle_mass, radius, circle_elasticity, color):
Ball.__init__(self, stage, pos_x, pos_y, circle_mass, radius, circle_elasticity, color)
self.circle.collision_type = 1
self.point_value = 0
self.gun_bullet = pygame.image.load(self.stage.graphics_path + "gunbullet.png").convert_alpha()
self.cannon_bullet = pygame.image.load(self.stage.graphics_path + "cannonbullet.png").convert_alpha()
示例5: __init__
class Game:
'''
Main game class
'''
def __init__ (self):
pygame.init()
pygame.key.set_repeat(10,10)
res = ConfigManager.get("resolution")
self.screen = pygame.display.set_mode(res, pygame.DOUBLEBUF)
self.ball = Ball()
self.space = Space(res)
def catchUserInput(self):
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
exit()
elif e.type == pygame.KEYDOWN:
if e.key == pygame.K_ESCAPE:
pygame.quit()
exit()
elif e.key == pygame.K_DOWN:
self.ball.decreaseSpeed()
elif e.key == pygame.K_UP:
self.ball.increaseSpeed()
elif e.key == pygame.K_LEFT:
self.ball.moveLeft()
elif e.key == pygame.K_RIGHT:
self.ball.moveRight()
def updateGameState(self):
self.ball.updateState()
def updateDisplay(self):
self.screen.fill(BLACK)
self.screen.blit(self.space, self.space.rect)
self.screen.blit(self.ball.image, self.ball.rect)
if __debug__:
#print "Debug mode: Draw sprite's rects"
pygame.draw.rect(self.screen, WHITE, self.ball.rect, 1)
#print "Debug mode: Draw statistics"
font = pygame.font.Font(None, 24)
speedMsg = "Spaceship speed: " + str(self.ball.speed)
posMsg = "Spaceship position: " + str(self.ball.rect)
textS = font.render(speedMsg, 1, GREEN)
textP = font.render(posMsg, 1, GREEN)
self.screen.blit(textS, (10,10))
self.screen.blit(textP, (10,25))
pygame.display.flip()
pygame.time.wait(1000 / 60)
def run(self):
while True:
self.catchUserInput()
self.updateGameState()
self.updateDisplay()
示例6: main
def main():
""" Main function for the game. """
pygame.init()
# Set the width and height of the screen [width,height]
size = [800, 600]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("CMSC 150 is cool")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
theBall = Ball()
theBall.x = 100
theBall.y = 100
theBall.change_x = 2
theBall.change_y = 1
theBall.color = [255,0,0]
# -------- Main Program Loop -----------
while not done:
# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT
# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT
# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
screen.fill(BLACK)
theBall.move()
theBall.draw(screen)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()
示例7: checkball
def checkball(img_list):
"""
检测有没有球,若有返回 True
"""
ball_checker = Ball(img_list)
if ball_checker.getR() != 0:
return True
else:
return False
示例8: new_random_ball
def new_random_ball(self, canvas=None):
while True:
i1 = randint(0, CELLS - 1)
j1 = randint(0, CELLS - 1)
if not self.find(i1, j1):
break
new_ball = Ball(i1, j1, choice(colors))
self.data.append(new_ball)
if canvas:
new_ball.draw(canvas)
示例9: __init__
def __init__(self, x = 0.0, y = 0.0):
Ball.__init__(self, x, y)
if Bar.image is None:
# This is the first time loading the sprite,
# so we need to load the image data
Bar.image = pg.image.load(os.path.join("data","bar.png")).convert()
Bar.image.set_colorkey(0)
self.image = Bar.image
(self.rect.w, self.rect.h) = (self.image.get_rect().w, self.image.get_rect().h)
self.infiniteMass = True
self.moveable = False
示例10: PongPlayers
class PongPlayers(NetworkSingletonActor):
def birth(self):
self.players = []
self.ball_location = -1
self.ball = Ball()
PictureInPictureManager().setball(self.ball)
def add(self, player):
self.players.append(player)
if self.ball_location == -1:
self.ball_location = 0
player.incoming(self.ball)
self.playerchange()
def leaving(self):
if len(self.players) == 0:
self.ball_location = -1
self.ball.park_at(here())
return
self.ball_location = (self.ball_location + 1) % len(self.players)
self.players[self.ball_location].incoming(self.ball)
def current_player(self):
if self.ball_location >= 0 and self.ball_location < len(self.players):
return self.players[self.ball_location]
return None
def remove(self, actor):
ball_on_dying_theatre = False
if actor in self.players:
if self.ball_location >= 0 and self.ball_location < len(self.players):
ball_on_dying_theatre = self.players[self.ball_location] == actor
self.players.remove(actor)
if ball_on_dying_theatre:
self.leaving()
self.playerchange()
def getall(self):
return self.players
def tellall(self, theatres):
for player in self.players:
player.availabletheatres(theatres)
def actorlost(self, actor, message):
self.remove(actor)
def playerchange(self):
PictureInPictureManager().players_are(self.players)
def theatreclosing(self):
migrate_or_die()
示例11: create_balls
def create_balls():
magic_ball = Ball(BALL_POS, 50)
magic_ball.speed = BALL_SPEED * random_vector()
result = [magic_ball]
for x in (-1, 0, 1):
for y in (-1, 0, 1):
if x == 0 and y == 0:
continue
result.append(Ball(BALL_POS + Vec2d(150 * x, 150 * y), random.randint(10, 40)))
return result
示例12: __init__
def __init__(self, screen):
self.balls = ScatteringsHandler()
ball = Ball(50, screen.get_height()/2)
ball.velocity = Vector(3.0, -1.0)
self.balls.add(ball)
bar = Bar(screen.get_width()/2, screen.get_height()/2)
self.balls.add(bar)
self.blobble1 = Blobble(screen.get_width()/2, screen.get_height()/2)
self.balls.add(self.blobble1)
self.screen = screen
self.background = screen.copy()
self.background.fill((0,0,0))
self.lastTicks = pg.time.get_ticks()
示例13: main
def main():
# Initialisation
pygame.init()
fps_clock = pygame.time.Clock()
width, height = size = 700, 700
black = 0, 0, 0
screen = pygame.display.set_mode(size)
ball = Ball()
boxes = []
rows = 5
row = 0
columns = 17
column = 0
while row < rows:
row = row + 1
while column < columns:
column = column + 1
boxes.append(Box(column,row))
column = 0
# Game Loop Start
while 1:
#Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#Logic
ball.move()
broken_box = ball.check_collision(size, boxes)
if broken_box is not None:
i = 0
new_boxes = []
for each in boxes:
if broken_box != i:
new_boxes.append(each)
i = i + 1
boxes = new_boxes
#Render
for each in boxes:
screen.blit(each.image, each.rect)
screen.blit(ball.image, ball.rect)
pygame.display.flip()
screen.fill(black)
fps_clock.tick(60)
示例14: Game
class Game(BaseLoop):
def __init__(self, screen: pygame.Surface):
super(Game, self).__init__(screen)
self.FPSCLOCK = None
self.racket_p1 = Racket(self.screen)
self.racket_p2 = None
self.ball = Ball(self.screen)
def setup(self):
super(Game, self).setup()
pygame.key.set_repeat(100, 20)
self.FPSCLOCK = pygame.time.Clock()
self.racket_p1 = Racket(self.screen)
self.ball = Ball(self.screen)
self.racket_p2 = Racket(self.screen, mode='auto')
def update_screen(self):
super(Game, self).update_screen()
pygame.draw.rect(self.screen, WHITE, ((self.screen.get_width() // 2) - 1, 0, 2,
self.screen.get_height()))
self.racket_p1.render()
self.racket_p2.render()
self.ball.render()
def process_event(self, event: pygame.event):
if event.type == KEYDOWN:
if event.key == CONTROLS[UP]:
self.racket_p1.move(UP)
elif event.key == CONTROLS[DOWN]:
self.racket_p1.move(DOWN)
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
self.LOOP_RUN = False
print('Ending the game...')
def start(self, game_type="one_player"):
print('Starting the game...')
self.game_type = game_type
super(Game, self).start()
while self.LOOP_RUN:
for event in pygame.event.get():
self.process_event(event)
self.update_screen()
self.ball.move(self.racket_p1, self.racket_p2)
self.racket_p2.auto_move(self.ball)
pygame.display.flip()
self.FPSCLOCK.tick(FPS)
示例15: __init__
def __init__(self, x = 0.0, y = 0.0):
Ball.__init__(self)
if Blobble.image is None:
# This is the first time loading the sprite,
# so we need to load the image data
Blobble.image = pg.image.load(os.path.join("data","blobble.png")).convert()
Blobble.image.set_colorkey(0)
self.image = Blobble.image
self.rect = self.image.get_rect()
#self.radius = self.rect.w / 2.0
self._setPosition(Vector(x, y))
self.infiniteMass = False
self.moveable = True
self.xAccel = 0.0
self.doJump = False