本文整理汇总了Python中ball.Ball.reset方法的典型用法代码示例。如果您正苦于以下问题:Python Ball.reset方法的具体用法?Python Ball.reset怎么用?Python Ball.reset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ball.Ball
的用法示例。
在下文中一共展示了Ball.reset方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GameLayer
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import reset [as 别名]
class GameLayer(Layer):
is_event_handler = True
def __init__(self):
super(GameLayer, self).__init__()
# 添加一个 板子
self.paddle = Paddle('images/ash.png')
self.add(self.paddle.sprite)
self.ball = Ball('images/pokem.png')
self.add(self.ball.sprite)
# 添加一个 label 显示当前的游戏状态
self.hud = Label('皮卡丘: 0')
self.hud.position = (0, 450)
self.add(self.hud)
# 添加一个 label 显示当前游戏关卡
self.level = Label('Level 1')
self.level.position = (100, 450)
self.add(self.level)
# 添加一个变量来记录金币
self.gold = 0
# 设置 4 个按键状态
self.key_pressed_left = False
self.key_pressed_right = False
self.key_pressed_up = False
self.key_pressed_down = False
self.blocks = []
# 调用 reset 函数初始化状态
self.reset()
# 定期调用 self.update 函数
# FPS frame per second 每秒帧数
self.schedule(self.update)
def reset(self):
self.gold = 0
self.update_hud()
self.paddle.reset()
self.ball.reset()
# 添加砖块并且显示出来
# 先删除残存的砖块
for b in self.blocks:
self.remove(b)
# 再初始化新的砖块
self.blocks = []
levelfile = 'level.txt'
positions = level_from_file(levelfile)
number_of_blocks = len(positions)
for i in range(number_of_blocks):
b = Sprite('images/pikachu.png', anchor=(0, 0))
b.position = positions[i]
# b.position = (randint(0, 500), 400)
self.add(b)
self.blocks.append(b)
def game_over(self):
print('游戏结束,跪了。捉到了', self.gold, '只皮卡丘')
# 没接到球,跳转到结束画面(失败)
scene = Scene(GameOver())
director.replace(SplitColsTransition(scene))
def game_win(self):
# 打完所有皮卡丘,跳转到下一关或(成功)
scene = Scene(GameWin())
director.replace(SplitColsTransition(scene))
def update_hud(self):
self.hud.element.text = '皮卡丘:' + str(self.gold)
def update_blocks(self):
# 判断是否撞到了砖块
for b in self.blocks:
if collides(self.ball.sprite, b):
# self.ball_speedy = -self.ball_speedy
self.ball.hit()
self.remove(b)
self.blocks.remove(b)
self.gold += 1
# self.speed += 1
self.update_hud()
print('捉到', self.gold, '只皮卡丘')
break
if len(self.blocks) == 0:
self.game_win()
def update_ball(self):
if self.ball.fired:
self.ball.update()
else:
bx, by = self.ball.sprite.position
px, py = self.paddle.sprite.position
self.ball.sprite.position = (px, by)
collide = collides(self.ball.sprite, self.paddle.sprite)
if collide:
self.ball.hit()
if self.ball.dead():
self.game_over()
#.........这里部分代码省略.........
示例2: main
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import reset [as 别名]
def main():
pygame.init()
# Set the height and width of the screen
size = [constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Block Break Game :D ")
# Create the player
player = Player()
# Create the ball
ball = Ball()
# Create all the levels
level_list = init_levellist(player)
# Set the current level
current_level_no = 0
current_level = level_list[current_level_no]
active_sprite_list = pygame.sprite.Group()
player.level = current_level
player.rect.x = (constants.SCREEN_WIDTH - player.rect.width) /2
active_sprite_list.add(player)
# Loop until the user clicks the close button
done = False
game_over = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# ============= Main program loop ==============
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.move_left()
if event.key == pygame.K_RIGHT:
player.move_right()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.stop()
if event.key == pygame.K_RIGHT:
player.stop()
if event.type == pygame.MOUSEBUTTONDOWN:
if game_over:
ball.reset()
player.rect.x = (constants.SCREEN_WIDTH - player.rect.width) /2
level_list.clear()
level_list = init_levellist(player)
current_level = level_list[current_level_no]
player_level = current_level
game_over = False
# Update the player
active_sprite_list.update()
# Update items in the level
current_level.update()
ball.update()
if (ball.bottom >= player.rect.top) and (ball.x >= player.rect.x) and (ball.x <= player.rect.x+player.rect.w):
ball.change_y *= -1
ball.speed += 0.1
ball.y = player.rect.top - ball.size + ball.change_y
for block in current_level.block_list:
# right side of block
y = block.rect.y
x = block.rect.x + block.rect.w
if (ball.x >= x and ball.x <= x + ball.size and
ball.y >= y and ball.y <= y + block.rect.h):
ball.change_x *= -1
ball.speed += 0.1
if not hardblock_collision(block, current_level):
current_level.block_list.remove(block)
# left side of block
y = block.rect.y
x = block.rect.x
if (ball.x >= x-ball.size and ball.x <= x and
ball.y >= y and ball.y <= y + block.rect.h):
ball.change_x *= -1
ball.speed += 0.1
if not hardblock_collision(block, current_level):
current_level.block_list.remove(block)
# bottom of block
y = block.rect.y + block.rect.h
#.........这里部分代码省略.........
示例3: __init__
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import reset [as 别名]
class Arkanoid:
def __init__(self):
from utilities import pygame, load_stages
from text import Score, Timer, Name, Text
from paddle import Paddle
from ball import Ball
from brick import Brick
from stage import Stage
C = Constants()
self.C = C
self.pygame = pygame
# setup FPS governor
self.clock = pygame.time.Clock()
# set the window size
divTop = int(C.DIV_POS*C.SIZE[1]) # top of divider. Affects bounds for ball
divBot = divTop + C.DIV_WIDTH
self.screen = pygame.display.set_mode(C.SIZE)
area = pygame.Rect(0, divBot, C.SIZE[0], C.SIZE[1]-divBot)
# create game screen objects/sprites
self.score = Score(C.SCORE_POS, self.screen, C.SCORE_START, C.SCORE_COLOR, C.SCORE_SIZE)
self.scoreText = Text(C.S_LABEL_POS, self.screen, C.S_LABEL_TEXT, C.S_LABEL_COLOR, C.S_LABEL_SIZE)
self.timer = Timer(C.TIMER_POS, self.screen, C.TIMER_START, C.TIMER_COLOR, C.TIMER_SIZE)
self.timerText = Text(C.T_LABEL_POS, self.screen, C.T_LABEL_TEXT, C.T_LABEL_COLOR, C.T_LABEL_SIZE)
self.name = Name(C.NAME_POS, self.screen, '', C.NAME_COLOR)
self.paddle = Paddle(area)
self.ball = Ball(area)
self.stages = load_stages(self.screen)
self.divider = (self.screen, C.DIV_COLOR, [0, divTop], [C.SIZE[0], divTop], C.DIV_WIDTH)
self.sprites = pygame.sprite.RenderPlain((self.paddle, self.ball))
self.paused = True # flag to indicate if the stage is paused or moving
self.pausedReset = False # flag to indicate pause was due to reset, in which case dont pause timer
# stage_event() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def stage_event(self, event):
"""Handles stage events"""
C = self.C
# quit the game
if event.type == C.QUIT: return 2
elif event.type == C.KEYDOWN:
# quit the game
if event.key == C.K_ESCAPE: return 2
# move the paddle
else: self.paddle.move(event.key)
# key has been un-pressed
elif event.type == C.KEYUP:
# stop moving the paddle
self.paddle.move(None)
# reset the ball and paddle (dont reset stage)
if event.key == C.K_r:
self.ball.reset()
self.paddle.reset()
self.paused = True
self.pausedReset = True
# pause the game
elif event.key == C.K_p:
self.ball.pause()
self.paddle.pause()
if self.paused: self.paused = False
else: self.paused = True
elif event.key == C.K_SPACE:
# start the stage
if self.paused:
self.ball.pause()
if self.paddle.paused: self.paddle.pause()
self.paused = False
self.pausedReset = False
return 0
# update_ball() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def update_ball(self):
"""Updates the ball and does ball related actions"""
from brick import Brick
if self.ball.inbounds:
if (
isinstance(self.ball.bouncedOff, Brick) and
self.ball.bouncedOff.destroyed
):
self.score.change_by(self.ball.bouncedOff.points * self.timer.value)
#.........这里部分代码省略.........
示例4: ButtonSoccer
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import reset [as 别名]
class ButtonSoccer(World):
def __init__(self):
World.__init__(self)
self.add(Scene())
self.add_bounds(width=(W_DIFF, H_DIFF, W_DIFF, H_DIFF))
self.force = Vec2(0, 0)
self.create_teams()
self.create_goal()
self.create_scores()
self.moves = 0
self.ball = Ball()
self.add(self.ball)
self.movement_check = CHECK_IDLE
def create_teams(self):
self.team_red = Team(self, 'Red Team', 'red', team_red_POSITIONS, True)
self.team_blue = Team(self, 'Blue Team', 'blue', team_blue_POSITIONS, False)
self.current_team = self.team_red
self.teams = (self.team_red, self.team_blue)
self.team_red.add_listener('released', self.movement_started)
self.team_blue.add_listener('released', self.movement_started)
def create_goal(self):
self.goal_red = Goal("LEFT")
self.goal_red.team_owner = self.team_red
self.goal_red.team_enemy = self.team_blue
self.goal_blue = Goal("RIGHT")
self.goal_blue.team_owner = self.team_blue
self.goal_blue.team_enemy = self.team_red
self.goals = (self.goal_red, self.goal_blue)
for goal in self.goals:
for dash in goal.elements():
self.add(dash)
def create_scores(self):
self.score_red = Score('left', 'red')
self.score_blue = Score('right', 'blue')
self.team_red.score = self.score_red
self.team_blue.score = self.score_blue
self.add(self.score_red)
self.add(self.score_blue)
def check_turn(self):
print('Turn -> ' + str(self.current_team))
if self.moves + 1 >= MOVES:
self.change_turn()
self.current_team = self.team_red if self.team_red.turn else self.team_blue
self.moves = 0
else:
self.moves += 1
def change_turn(self):
self.team_red.change_turn()
self.team_blue.change_turn()
@listen('mouse-long-press', 'left')
def update_poiter(self, pos):
self.clear_pointer()
button = None
for team in self.teams:
if team.turn:
button = team.get_clicked_button()
if button != None:
try:
size = button.pos - Vec2(pos)
p = Pointer(button.pos.as_tuple(), size.as_tuple())
self.add(p)
except ZeroDivisionError:
pass
def clear_pointer(self):
for element in self.get_render_tree().walk():
if isinstance(element, Pointer):
self.remove(element)
def movement_started(self):
self.clear_pointer()
self.movement_check = CHECK_ACTIVE
def check_goal(self, goal):
if goal.is_goal(self.ball.pos):
goal.team_enemy.points += 1
goal.team_enemy.score.submit()
self.moves = 0
self.ball.reset()
if goal.team_enemy.points >= MAX_SCORE:
self.reset()
return
#.........这里部分代码省略.........
示例5: Breakout
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import reset [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()
#.........这里部分代码省略.........