本文整理汇总了Python中ball.Ball.serve方法的典型用法代码示例。如果您正苦于以下问题:Python Ball.serve方法的具体用法?Python Ball.serve怎么用?Python Ball.serve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ball.Ball
的用法示例。
在下文中一共展示了Ball.serve方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Game
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import serve [as 别名]
class Game(object):
"""Main program"""
def __init__(self):
#Initialize pygame and window
pygame.init()
self.screen_width, self.screen_height = 400, 600
self.screen = pygame.display.set_mode((self.screen_width, self.screen_height), 0, 32)
self.screen.fill(blue)
pygame.display.update()
#Clock
self.clock = pygame.time.Clock()
#Bricks
self.blocks = list()
for w in range(1,self.screen_width,50):
for h in range(23, 198, 22):
self.blocks.append(Block(self.screen_width, self.screen_height, w, h, orange, blue_shadow))
#Paddle
self.paddle = Paddle(self.screen_width, self.screen_height, purple, blue_shadow)
#Ball
self.ball = Ball(self.screen_width, self.screen_height, green, blue_shadow)
def update(self):
self.clock.tick(game_speed)
self.paddle.update()
self.ball.update(self.paddle)
def draw(self):
#Redraw Background
self.screen.fill(blue)
for block in self.blocks:
block.draw_shadow(self.screen)
self.ball.draw_shadow(self.screen)
self.paddle.draw_shadow(self.screen)
for block in self.blocks:
block.draw(self.screen)
self.ball.draw(self.screen)
self.paddle.draw(self.screen)
def new_game(self):
self.game_over = False
self.round = 0
self.play()
def new_round(self):
pass
def play(self):
while not self.game_over:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.ball.serve()
if event.key == pygame.K_LEFT:
self.paddle.x_vel = -4
elif event.key == pygame.K_RIGHT:
self.paddle.x_vel = 4
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and self.paddle.x_vel < 0:
self.paddle.x_vel = 0
if event.key == pygame.K_RIGHT and self.paddle.x_vel > 0:
self.paddle.x_vel = 0
self.update()
self.draw()
pygame.display.update()
示例2: Breakout
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import serve [as 别名]
class Breakout(object):
def __init__(self):
# Initilaize pygame and the display/window
pygame.init()
self.screen_width, self.screen_height = 600, 800
self.screen = pygame.display.set_mode((self.screen_width, self.screen_height)) # , pygame.FULLSCREEN)
pygame.display.set_caption('Breakout')
# Create the game objects
self.paddle = Paddle(self.screen_width, self.screen_height)
self.ball = Ball(self.screen_width, self.screen_height)
self.bricks = list()
for i in range(0,600,60):
for j in range(42, 211, 42):
self.bricks.append(Brick(self.screen_width, self.screen_height, i, j))
# Let's control the frame rate
self.clock = pygame.time.Clock()
def new_game(self):
"""Start a new game of Breakout.
Resets all game-level parameters, and starts a new round.
"""
self.game_over = False
self.round = 0
self.paddle.reset()
self.new_round()
def new_round(self):
"""Start a new round in a Breakout game.
Resets all round-level parameters, increments the round counter, and
puts the ball on the paddle.
"""
self.round += 1
self.ball.reset(self.paddle)
def play(self):
"""Start Breakout program.
New game is started and game loop is entered.
The game loop checks for events, updates all objects, and then
draws all the objects.
"""
self.new_game()
while not self.game_over: # Game loop
self.clock.tick(50) # Frame rate control
font = pygame.font.SysFont("monospace", 15)
round_counter = font.render("Round " + str(self.round), 2, (255,255,0))
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.MOUSEBUTTONDOWN:
self.game_over = True
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.ball.serve()
if event.key == pygame.K_LEFT:
self.paddle.x_velocity = -4
elif event.key == pygame.K_RIGHT:
self.paddle.x_velocity = 4
if event.key == pygame.K_a:
self.ball.x_velocity = -3
elif event.key == pygame.K_d:
self.ball.x_velocity = 3
# This starts a new round, it's only here for debugging purposes
if event.key == pygame.K_r:
self.new_round()
# This starts a new game, it's only here for debugging purposes
if event.key == pygame.K_g:
self.new_game()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and self.paddle.x_velocity < 0:
self.paddle.x_velocity = 0
if event.key == pygame.K_RIGHT and self.paddle.x_velocity > 0:
self.paddle.x_velocity = 0
else:
self.paddle.update()
contact = self.ball.update(self.paddle, self.bricks)
for brick in self.bricks:
if contact == brick:
self.bricks.remove(brick)
self.screen.fill((0, 0, 0))
self.screen.blit(round_counter, (0, 0))
ball_loc = font.render(str(self.ball.x) + "," + str(self.ball.y), 2, (255,255,0))
self.screen.blit(ball_loc, (0, 14))
self.paddle.draw(self.screen)
self.ball.draw(self.screen)
pygame.display.flip()
for brick in self.bricks:
brick.draw(self.screen)
pygame.display.flip()
#.........这里部分代码省略.........