本文整理汇总了Python中Ball.Ball.draw方法的典型用法代码示例。如果您正苦于以下问题:Python Ball.draw方法的具体用法?Python Ball.draw怎么用?Python Ball.draw使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ball.Ball
的用法示例。
在下文中一共展示了Ball.draw方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Breakout
# 需要导入模块: from Ball import Ball [as 别名]
# 或者: from Ball.Ball import draw [as 别名]
class Breakout(object):
def __init__(self):
# Initilaize pygame and the display/window
pygame.init()
self.width, self.height = 600, 800
self.screen = pygame.display.set_mode((self.width, self.height)) #, pygame.FULLSCREEN)
pygame.display.set_caption('Breakout')
# background = pygame.image.load("PiInvaders/background.png").convert();
# Create the game objects
self.ball = Ball(self.width / 2, self.height - 32)
self.paddle = Paddle(self.width / 2, self.height - 16, 80, 16)
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.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, centering both."""
self.round += 1
self.ball_is_moving = False
self.ball.x_velocity = random.randint(-3, 3)
self.paddle.x = self.width / 2
self.ball.y = self.height - 32
def play(self):
"""Start Breakout game
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
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 not self.ball_is_moving and event.key == pygame.K_SPACE:
self.ball_is_moving = True
if event.key == pygame.K_LEFT:
self.paddle.x_velocity = -4
elif event.key == pygame.K_RIGHT:
self.paddle.x_velocity = 4
# 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()
if self.ball_is_moving:
self.ball.update()
else:
self.ball.x = self.paddle.x
self.screen.fill((0, 0, 0))
self.paddle.draw(pygame, self.screen)
self.ball.draw(pygame, self.screen)
pygame.display.flip()
pygame.quit()
示例2: Paddle
# 需要导入模块: from Ball import Ball [as 别名]
# 或者: from Ball.Ball import draw [as 别名]
paddle = Paddle(canvas, 'blue')
# Creates an object named 'ball' of the Ball class that we created in Ball.py
ball = Ball(canvas, paddle, 'red')
current_score = 0
# Creates the label for the score
score = Tkinter.Label(canvas, text= ball.score())
# Displays that window
canvas.create_window(10, 20, window=score, anchor='w')
# Tells the canvas to not loop through the listed command until the user close the window
while 1:
# Calls the draw function on the ball as long as hit_bottom is False
ball.draw()
# Calls the draw function on the paddle as long as hit_bottom is False
paddle.draw()
# Updates the score
score['text'] = (ball.score())
# Both of these update the canvas
top.update_idletasks()
top.update()
#Tells the loop to sleep for 1/100th of a second before looping again
time.sleep(0.01)
示例3: __init__
# 需要导入模块: from Ball import Ball [as 别名]
# 或者: from Ball.Ball import draw [as 别名]
class BrickBreakGame:
def __init__(self, w=800, h=600):
if platform.system() == 'Windows':
os.environ['SDL_VIDEODRIVER'] = 'windib'
os.environ['SDL_VIDEO_WINDOW_POS'] = "100,100"
pygame.init()
self.clock = pygame.time.Clock()
self.window = pygame.display.set_mode((w, h), pygame.DOUBLEBUF|pygame.HWSURFACE)
pygame.display.set_caption('Brick Break!')
self.ship = Ship(w/2, h-14)
self.ball = Ball((self.ship.x, self.ship.y-self.ship.height/2), (400., 0.))
self.backgroundColor = pygame.Color(255,255,255)
pygame.mouse.set_visible(False)
pygame.mouse.set_pos(100+w/2, 100+h/2)
self.blocks = BlockArray(16,11)
self.shipx = pygame.mouse.get_pos()[0]
self.goLeft = self.goRight = False
def run(self):
while True:
self.mainLoop()
def mainLoop(self):
#EVENTS
for event in pygame.event.get():
if event.type == MOUSEMOTION:
self.shipx = event.pos[0]
elif event.type == KEYDOWN:
if event.key == K_LEFT:
self.goLeft = True
elif event.key == K_RIGHT:
self.goRight = True
elif event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
elif event.type == KEYUP:
if event.key == K_LEFT:
self.goLeft = False
elif event.key == K_RIGHT:
self.goRight = False
elif event.type == QUIT:
pygame.quit()
sys.exit(0)
if self.goLeft:
self.shipx -= self.clock.get_time()*2/3
elif self.goRight:
self.shipx += self.clock.get_time()*2/3
#LOGIC
self.ball.update(self.clock.get_time()/1000., self.window)
self.ship.update(self.shipx)
self.blocks.update(self.ball)
self.ship.checkBallCollision(self.ball)
if self.ball.y > self.window.get_height():
pygame.event.post(pygame.event.Event(QUIT))
#DRAW
self.window.fill(self.backgroundColor)
self.blocks.draw(self.window)
self.ship.draw(self.window)
self.ball.draw(self.window)
pygame.display.update()
self.clock.tick(60)