コード例 #1
0
class Game():

  # delay to move objects, class attribute
  delay=0.1


  def __init__(self):

    # initialize the objects we are going to need
    self.window=Window() 

    self.body= Body()

    # attaches body to head
    self.head= Head(self.body)

    self.fruit=Fruit()

    self.scoreboard= ScoreBoard()

    # Main gmae loop
    while True:

      # updates the game constantly
      self.window.screen.update()

      # listen for key press
      self.window.screen.listen()

      # treats keypress
      self.window.screen.onkey(self.head.go_up, "Up")
      self.window.screen.onkey(self.head.go_down, "Down")
      self.window.screen.onkey(self.head.go_right, "Right")
      self.window.screen.onkey(self.head.go_left, "Left")  

      self.head.move()

      self.checks_collision()

      time.sleep(self.delay)

#checks  collisions
  def checks_collision(self):

    #checks collision between head and fruit
    if self.head.distance(self.fruit) <15:

     # move the fruit to a random position on screen
      x = random.randint(-370, 370)
      y = random.randint(-290, 290)
      self.fruit.goto(x, y)

     # add new segment to snake's body
      self.head.body.add_segment()

    #  increases score
      self.scoreboard.score+=10
      if self.scoreboard.score > self.scoreboard.top_score:
        self.scoreboard.top_score = self.scoreboard.score
      
        # update score
      self.scoreboard.clear()
      self.scoreboard.write("score: {} top score: {}".format(self.scoreboard.score, self.scoreboard.top_score), align="center", font=("Courier", 24, "normal"))


    
    
    #checks collision between snake and border
    elif self.head.xcor() > 370 or self.head.xcor() < -370 or self.head.ycor() > 290 or self.head.ycor() < -290:

      self.kill_snake()

    # checks collision between snake head and own body
    else:
      for segment in self.head.body.segments:
        if segment.distance(self.head)<20:
          self.kill_snake()



      
  def kill_snake(self):

    time.sleep(0.4)

     # color the snake body in red and stops it
    for segment in self.head.body.segments: 
      segment.direction="stop"
      segment.color("white")
      time.sleep(0.1)
      self.window.screen.update()

    time.sleep(0.3)
    self.head.direction="stop"
    time.sleep(0.3)

    self.head.goto(0,0)

    # hides the segments of the body, only erasing it doesnt clean the drawing, that's why we need to hide
    for segment in self.head.body.segments: 
      segment.hideturtle()

    # reinitializes body segments array
      self.head.body.segments=[]
    
    # resets the score 
    self.scoreboard.score = 0

    # update score
    self.scoreboard.clear()
    self.scoreboard.write("score: {} top score: {}".format(self.scoreboard.score, self.scoreboard.top_score), align="center", font=("Courier", 24, "normal"))
コード例 #2
0
def main():
    screen = Screen()
    screen.setup(width=800, height=600)
    screen.bgcolor('black')
    screen.title('Pong Game')
    screen.tracer(0)
    score_board = ScoreBoard()
    score_board.display_score()
    CenterLine()
    player1 = Paddle((350, 0))
    player2 = Paddle((-350, 0))
    ball = Ball()
    screen.onkey(player1.move_up, 'Up')
    screen.onkey(player1.move_down, 'Down')
    screen.onkey(player2.move_up, 'w')
    screen.onkey(player2.move_down, 's')
    screen.listen()

    def game_continue():
        if messagebox.askretrycancel("Game Over!!", "Wanna play again? "):
            screen.clear()
            main()

    running = True
    while running:
        time.sleep(0.05)
        screen.update()
        if score_board.check_win():
            score_board.final_result()
            ball.hideturtle()
            break
        if ball.ispoint_wait:
            if ball.point_wait < 50:
                ball.point_wait += 1
                continue
            else:
                ball.ispoint_wait = False
                ball.point_wait = 1
        screen.update()
        ball.move()
        # for not repeating collision if the distance is small
        if player1.is_collided:
            if player1.safe_count < 5:
                player1.safe_count += 1
            else:
                player1.is_collided = False
                player1.safe_count = 1
        elif player2.is_collided:
            if player2.safe_count < 5:
                player2.safe_count += 1
            else:
                player2.is_collided = False
                player2.safe_count = 1

        if ball.ycor() > 265 or ball.ycor() < -270:
            ball.wall_collision()

        if ball.xcor() < 370 and ball.distance(
                player1) < 45 and not player1.is_collided:
            ball.paddle_collision()
            ball.hit_count += 1
            ball.hit = True
            player1.is_collided = True
        elif ball.xcor() > -370 and ball.distance(
                player2) < 45 and not player2.is_collided:
            ball.paddle_collision()
            ball.hit_count += 1
            ball.hit = True
            player2.is_collided = True

        if ball.hit_count % 2 == 0 and ball.hit_count != 0 and ball.hit:
            if ball.bounce_speed < 0:
                ball.bounce_speed -= 1.5
            else:
                ball.bounce_speed += 1.5
            ball.bounce_x = ball.bounce_speed
            ball.hit = False

        if ball.xcor() > 380:
            score_board.clear()
            score_board.score1 += 1
            score_board.display_score()
            ball.ispoint_wait = True
            ball.ball_reset()

        elif ball.xcor() < -380:
            score_board.clear()
            score_board.score2 += 1
            score_board.display_score()
            ball.ispoint_wait = True
            ball.ball_reset()

    screen.update()
    time.sleep(1)
    game_continue()
    screen.bye()
    screen.exitonclick()
コード例 #3
0
ファイル: main.py プロジェクト: RNTejas/100DaysOfCode
screen.onkeypress(r_paddle.go_up, 'Up')
screen.onkeypress(r_paddle.go_down, 'Down')

screen.onkeypress(l_paddle.go_up, 'w')
screen.onkeypress(l_paddle.go_down, 's')


game_is_on = True
while game_is_on:
    time.sleep(ball.move_speed)
    screen.update()
    ball.move_ball()

    if ball.ycor() > 280 or ball.ycor() < -280:
        ball.bounce_y()

    if ball.distance(r_paddle) < 50 and ball.xcor() > 320 or ball.distance(l_paddle) < 50 and ball.xcor() > -340:
        ball.bounce_x()

    if ball.xcor() > 380:
        ball.reset_position()
        scoreboard.clear()
        scoreboard.l_point()

    if ball.xcor() < -380:
        ball.reset_position()
        scoreboard.clear()
        scoreboard.r_point()

screen.exitonclick()
コード例 #4
0
screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")
game_is_on = True

while game_is_on:
    screen.update()
    time.sleep(.1)
    snake.move()

    # detect collision with food
    if snake.head.distance(food) < 15:
        food.refresh()
        score.clear()
        snake.extend()
        score.increase_score()

# detect collision with wall
    if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor(
    ) > 280 or snake.head.ycor() < -280:
        score.reset()
        snake.reset()

# detect collision with tail
    for segment in snake.segment[1:]:
        if snake.head.distance(segment) < 10:
            score.reset()
            snake.reset()