本文整理汇总了Python中ball.Ball.bounce方法的典型用法代码示例。如果您正苦于以下问题:Python Ball.bounce方法的具体用法?Python Ball.bounce怎么用?Python Ball.bounce使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ball.Ball
的用法示例。
在下文中一共展示了Ball.bounce方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import bounce [as 别名]
class Game:
# initialize resource files
def __init__(self):
# initialize pygame stuff
pygame.init()
pygame.mixer.init()
self.sound_player_collide = pygame.mixer.Sound(SOUND_PLAYER_COLLIDE)
self.sound_brick_collide = pygame.mixer.Sound(SOUND_BRICK_COLLIDE)
self.sound_start = pygame.mixer.Sound(SOUND_GAME_START)
self.sound_gameover = pygame.mixer.Sound(SOUND_GAME_OVER)
self.sound_win = pygame.mixer.Sound(SOUND_WIN)
self.display = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption(NAME + " v" + VERSION)
self.clock = pygame.time.Clock()
# initialize objects
self.player = Player(SCREEN_WIDTH/2)
self.ball = Ball(self.player.posX(), self.player.posY())
self.grid = [[None for i in xrange(GRID_WIDTH)] for j in xrange(GRID_HEIGHT)]
self.running = False
self.state = STATES["IDLE"]
self.points = 0
self.background = pygame.image.load(IMAGE_BACKGROUND).convert_alpha()
self.font = pygame.font.Font(None, 36)
self.smallFont = pygame.font.Font(None, 16)
self.largeFont = pygame.font.Font(None, 64)
self.mouseMode = False
# returns list of all elements in grid that are non-empty
def objects(self):
ret = []
for i in xrange(len(self.grid)):
ret += filter(lambda x: x is not None, self.grid[i])
return ret
# Generate the bricks. Fill the grid for now. Todo: randomly generate or load from file
def generateStage(self):
for row in xrange(GRID_HEIGHT):
for col in xrange(GRID_WIDTH):
# randomize chance for "special" brick
rand = random.randint(1, BRICK_RANDOM_CHANCE)
if rand == 1: # 1/10 for indestructable
self.grid[row][col] = Brick.CreateBrick(col, row, 0)
elif rand <= 5: # 1/2 for level 1
self.grid[row][col] = Brick.CreateBrick(col, row, 1)
else: # remaining is split evenly
self.grid[row][col] = Brick.CreateBrick(col, row, rand - (BRICK_RANDOM_CHANCE - MAX_BRICK_POWER))
def handleEvents(self, event):
if event.type == pygame.QUIT:
self.running = False
elif event.type == MOUSEBUTTONDOWN:
self.state = STATES["STARTED"]
self.mouseMode = True
elif event.type == KEYDOWN:
# handle keyboard inputs
if event.key == K_ESCAPE:
# self.state = STATES["MENU"]
self.running = False
elif event.key == K_SPACE:
self.state = STATES["STARTED"]
def handleKeyInput(self):
pressed = pygame.key.get_pressed()
if self.state == STATES["STARTED"] or self.state == STATES["IDLE"]:
if pressed[K_LEFT]:
if self.player.move(-PLAYER_SPEED) and self.state == STATES["IDLE"]:
self.ball.move(-PLAYER_SPEED, 0)
if pressed[K_RIGHT]:
if self.player.move(PLAYER_SPEED) and self.state == STATES["IDLE"]:
self.ball.move(PLAYER_SPEED, 0)
def updateState(self):
if self.state == STATES["STARTED"]: # started state - ball is moving
self.ball.update()
if (self.ball.y >= SCREEN_HEIGHT - BALL_RADIUS*2):
# game over
self.gameover()
# check collision with blocks and if any left
blocksLeft = False
for obj in self.objects():
if obj.getHealth() != 0 and not blocksLeft:
blocksLeft = True
result = obj.collision(self.ball)
if result["collided"]:
self.sound_brick_collide.play()
self.points += result["points"]
self.ball.bounce(0)
if not blocksLeft:
self.win()
# check collision with platform
if self.player.collision(self.ball):
self.sound_player_collide.play()
angleDiff = self.player.angleDiff(self.ball)
self.ball.bounce(angleDiff)
# print "Angle: " + str(angleDiff)
#.........这里部分代码省略.........
示例2:
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import bounce [as 别名]
if ball.x <= 0:
ball.x, ball.y = WIDTH/2, HEIGHT/2
rightScore += 1
scored = True
elif ball.x >= WIDTH - ball.radius:
ball.x, ball.y = WIDTH/2, HEIGHT/2
leftScore += 1
scored = True
if ball.y <= ball.radius or ball.y >= HEIGHT - ball.radius:
ball.set_yspeed( -ball.yspeed )
#ball - paddle collision detection
if ( rightPaddle.left() < (ball.x + ball.radius) and rightPaddle.right() > (ball.x - ball.radius) ) and ( rightPaddle.top() < (ball.y + ball.radius) and rightPaddle.bottom() > (ball.y - ball.radius) ):
ball.bounce( rightPaddle.top(), rightPaddle.bottom(), rightPaddle.height )
if ( leftPaddle.left() < (ball.x + ball.radius) and leftPaddle.right() > (ball.x - ball.radius) ) and ( leftPaddle.top() < (ball.y + ball.radius) and leftPaddle.bottom() > (ball.y - ball.radius) ):
ball.bounce( leftPaddle.top(), leftPaddle.bottom(), leftPaddle.height )
#render half-field line
pygame.draw.line( SURFACE, WHITE, ( WIDTH/2, 0 ), ( WIDTH/2, HEIGHT ) )
#render the paddles
leftPaddle.render(SURFACE)
rightPaddle.render(SURFACE)
#render the ball
ball.update( ball.xspeed, ball.yspeed )
if scored:
pygame.time.wait(2000)