本文整理汇总了Python中ball.Ball.move方法的典型用法代码示例。如果您正苦于以下问题:Python Ball.move方法的具体用法?Python Ball.move怎么用?Python Ball.move使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ball.Ball
的用法示例。
在下文中一共展示了Ball.move方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import move [as 别名]
def main():
pygame.init()
FPS = 30
FPS_CLOCK = pygame.time.Clock()
# COLOR LIST
WHITE = pygame.Color(255, 255, 255)
# Code to create the initial window
window_size = (500, 500)
SCREEN = pygame.display.set_mode(window_size)
# set the title of the window
pygame.display.set_caption("Bouncing Ball Animation")
# change the initial background color to white
SCREEN.fill(WHITE)
b1 = Ball(0, 0, 4, 5)
b1.draw(SCREEN)
while True: # <--- main game loop
for event in pygame.event.get():
if event.type == QUIT: # QUIT event to exit the game
pygame.quit()
sys.exit()
SCREEN.fill(WHITE)
b1.move(SCREEN)
b1.draw(SCREEN)
pygame.display.update() # Update the display when all events have been processed
FPS_CLOCK.tick(FPS)
示例2: main
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import move [as 别名]
def main():
""" Main function for the game. """
pygame.init()
# Set the width and height of the screen [width,height]
size = [800, 600]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("CMSC 150 is cool")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
theBall = Ball()
theBall.x = 100
theBall.y = 100
theBall.change_x = 2
theBall.change_y = 1
theBall.color = [255,0,0]
# -------- Main Program Loop -----------
while not done:
# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT
# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT
# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
screen.fill(BLACK)
theBall.move()
theBall.draw(screen)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()
示例3: main
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import move [as 别名]
def main():
# Initialisation
pygame.init()
fps_clock = pygame.time.Clock()
width, height = size = 700, 700
black = 0, 0, 0
screen = pygame.display.set_mode(size)
ball = Ball()
boxes = []
rows = 5
row = 0
columns = 17
column = 0
while row < rows:
row = row + 1
while column < columns:
column = column + 1
boxes.append(Box(column,row))
column = 0
# Game Loop Start
while 1:
#Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#Logic
ball.move()
broken_box = ball.check_collision(size, boxes)
if broken_box is not None:
i = 0
new_boxes = []
for each in boxes:
if broken_box != i:
new_boxes.append(each)
i = i + 1
boxes = new_boxes
#Render
for each in boxes:
screen.blit(each.image, each.rect)
screen.blit(ball.image, ball.rect)
pygame.display.flip()
screen.fill(black)
fps_clock.tick(60)
示例4: Game
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import move [as 别名]
class Game(BaseLoop):
def __init__(self, screen: pygame.Surface):
super(Game, self).__init__(screen)
self.FPSCLOCK = None
self.racket_p1 = Racket(self.screen)
self.racket_p2 = None
self.ball = Ball(self.screen)
def setup(self):
super(Game, self).setup()
pygame.key.set_repeat(100, 20)
self.FPSCLOCK = pygame.time.Clock()
self.racket_p1 = Racket(self.screen)
self.ball = Ball(self.screen)
self.racket_p2 = Racket(self.screen, mode='auto')
def update_screen(self):
super(Game, self).update_screen()
pygame.draw.rect(self.screen, WHITE, ((self.screen.get_width() // 2) - 1, 0, 2,
self.screen.get_height()))
self.racket_p1.render()
self.racket_p2.render()
self.ball.render()
def process_event(self, event: pygame.event):
if event.type == KEYDOWN:
if event.key == CONTROLS[UP]:
self.racket_p1.move(UP)
elif event.key == CONTROLS[DOWN]:
self.racket_p1.move(DOWN)
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
self.LOOP_RUN = False
print('Ending the game...')
def start(self, game_type="one_player"):
print('Starting the game...')
self.game_type = game_type
super(Game, self).start()
while self.LOOP_RUN:
for event in pygame.event.get():
self.process_event(event)
self.update_screen()
self.ball.move(self.racket_p1, self.racket_p2)
self.racket_p2.auto_move(self.ball)
pygame.display.flip()
self.FPSCLOCK.tick(FPS)
示例5: update
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import move [as 别名]
def update(self):
hit = []
hit.extend(pygame.sprite.spritecollide(self, self.scene.paddle, False, pygame.sprite.collide_rect))
resx, resy = self.scene.engine.getResolution()
if hit or self.rect.centery - 9 > resy:
self.gone = True
if hit:
self.sound.play()
if self.type == 1:
new = []
balls = random.randint(1, 4)
for b in self.scene.balls:
for _ in range(balls):
ball = Ball(self.scene, b)
ball.rect.centery += 16
ball.vx = b.vx + random.randint(-7, 7)
ball.vy = b.vy + random.randint(-7, 7)
ball.inplace = False
ball.move()
new.append(ball)
for ball in new:
self.scene.balls.add(ball)
if self.type == 2:
width, height = self.scene.paddle.sprite.image.get_size()
temp = self.scene.paddle.sprite.rect.centerx, self.scene.paddle.sprite.rect.centery
self.scene.paddle.sprite.image = pygame.transform.scale(self.scene.paddle.sprite.image, (width / 2, height))
self.scene.paddle.sprite.rect = self.scene.paddle.sprite.image.get_rect()
self.scene.paddle.sprite.rect.centerx, self.scene.paddle.sprite.rect.centery = temp
if self.type == 0:
self.scene.engine.player['lives'] += 1
self.move()
示例6: test_move
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import move [as 别名]
def test_move(self):
ball = Ball((40,50), (10,10), 10)
ball.move()
self.assertEqual(ball.pos, (50,60))
示例7: Tk
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import move [as 别名]
from tkinter import *
from ball import Ball
master = Tk()
canvas = Canvas(master, width = 400, height = 400)
canvas.pack()
canvas. create_rectangle(0, 0, 400, 400, fill ='#ffffff')
ball = Ball((150, 250), (2, -2), 25)
while True:
ball.move()
bbox = ball.get_bbox()
canvas. create_rectangle(0, 0, 400, 400, fill ='#ffffff')
canvas.create_oval(bbox, fill='#000000')
# (same as previous line) canvas.create_oval(bbox[0], bbox[1], bbox[3], bbox[4], fill='#000000')
canvas.after(1000 // 60)
canvas.update()
mainloop()
示例8: __init__
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import move [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)
#.........这里部分代码省略.........