当前位置: 首页>>代码示例>>Python>>正文


Python Ball.draw方法代码示例

本文整理汇总了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()
开发者ID:artlogic,项目名称:breakout,代码行数:80,代码来源:breakout.py

示例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)
开发者ID:NTomtishen,项目名称:src,代码行数:33,代码来源:Bounce.py

示例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)
开发者ID:rhaps0dy,项目名称:pyBrickBreak,代码行数:71,代码来源:brickbreak.py


注:本文中的Ball.Ball.draw方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。