예제 #1
0
    def play(self):
        GameBoard(SCREEN_WIDTH)

        snake = Snake(3)
        snake.registerKeyListener(self.screen)

        food = Food()

        scoreBoard = ScoreBoard()

        while self.gameRunns:
            start = time.time()

            if not self.gamePaused:
                scoreBoard.update_scoreboard()
                snake.show()
                food.show()

                snake.move()

                if self.recognize_collision(snake, food, scoreBoard):
                    snake.hide()
                    food.hide()
                    scoreBoard.game_over()
                    self.gameRunns = False

            else:
                scoreBoard.pause()
                snake.hide()
                food.hide()

            self.screen.update()
            time.sleep(max(1.0 / snake.speed - (time.time() - start), 0))

        self.screen.exitonclick()
class SnakeGame:
    def __init__(self):
        self.wall = Wall()
        self.snake = Snake()
        self.scoreboard = ScoreBoard()
        self.food = Food()
        self.screen = game_screen.screen
        self.game_is_on = True
        self.start_game()

# starting snake

    def start_game(self):
        self.game_is_on = True
        while self.game_is_on:
            self.screen.update()
            time.sleep(0.1)
            self.snake.start_snake()
            # Detect collision with food
            if self.snake.head.distance(self.food) < 15:
                self.snake.extend()
                self.food.refresh()
                self.scoreboard.add_score()
            # Detect collision with wall
            x_cor = self.snake.head.xcor()
            y_cor = self.snake.head.ycor()
            if x_cor > 250 or x_cor < -250 or y_cor > 250 or y_cor < -250:
                self.game_is_on = False
                self.scoreboard.game_over()
            # Collision with tail
            for segment in self.snake.snake_body[1:]:
                if self.snake.head.distance(segment) < 10:
                    self.game_is_on = False
                    self.scoreboard.game_over()
예제 #3
0
파일: main.py 프로젝트: jnisarg/Pong
def pong():
    screen = Screen()
    screen.setup(width=900, height=1000)
    screen.bgcolor("black")
    screen.title("Pong: The Famous Arcade Game")
    screen.tracer(0)

    print_logo()

    l_paddle = Paddle((-380, -100))
    r_paddle = Paddle((380, -100))

    ball = Ball()

    scoreboard = ScoreBoard()

    game_graphics()

    def restart():
        screen.clear()
        pong()

    screen.listen()
    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")
    screen.onkey(restart, "r")

    game_off = False
    while not game_off:
        time.sleep(ball.move_speed)
        screen.update()
        ball.move()

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

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

        if ball.xcor() > 395:
            ball.refresh()
            l_paddle.refresh()
            r_paddle.refresh()
            scoreboard.l_point()

        if ball.xcor() < -395:
            ball.refresh()
            l_paddle.refresh()
            r_paddle.refresh()
            scoreboard.r_point()

        game_off = scoreboard.game_over()

    screen.exitonclick()
예제 #4
0
screen.onkey(key="Left", fun=snake.left)
screen.onkey(key="Right", fun=snake.right)

game_over = False

while not game_over:
    screen.update()
    sleep(0.1)
    snake.move()

    # eat food
    if snake.head.distance(food) < 15:
        snake.stretch()
        food.move()
        scoreboard.increase_score()

    # collision with tail
    for obj in snake.snake[1:]:
        if snake.head.distance(obj) < 15:
            scoreboard.game_over()
            game_over = True
            break

    # collision with wall
    if (snake.head.xcor() > 280 or snake.head.xcor() < -280
            or snake.head.ycor() > 280 or snake.head.ycor() < -280):
        scoreboard.game_over()
        game_over = True

screen.exitonclick()
예제 #5
0
screen.listen()  # listen for keybordstokes

snake = Snake()  # created an snake object
food = Food()  # created a food class object
scoreborad = ScoreBoard()

screen.onkey(key="Up", fun=snake.move_up)
screen.onkey(key="Down", fun=snake.move_down)
screen.onkey(key="Left", fun=snake.move_left)
screen.onkey(key="Right", fun=snake.move_right)
is_game_is_on = True
while is_game_is_on:
    screen.update()  # update a screen to show a snake moving
    time.sleep(0.2)
    snake.move()
    #     Detect a collosion of snake with food .we use distance method for this
    if snake.head.distance(food) < 15:
        food.food_new_location()
        snake.extend()
        scoreborad.increment_score()
    # Detect a collosion if snake with wall.
    if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor() > 280 or snake.head.ycor() < -280:
        is_game_is_on = False
        scoreborad.game_over()
    #     Detect snake head collison
    for segment in snake.segment[1:]:
        if snake.head.distance(segment) < 10:
            is_game_is_on = False
            scoreborad.game_over()
screen.exitonclick()
예제 #6
0
# Keyboard functionality
screen.onkey(score.speak_score, 's')
screen.onkey(snake.up, 'Up')
screen.onkey(snake.down, 'Down')
screen.onkey(snake.left, 'Left')
screen.onkey(snake.right, 'Right')
screen.onkey(food.speak_pos, 'f')

game = True
while game:
  screen.update()
  sleep(0.1)
  snake.move()
  if snake.head.distance(food) < 15:
    s.speak("You ate the food!")
    food.refresh()
    score.increase()
    snake.extend()
  if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor() > 280 or snake.head.ycor() < -280:
    s.speak("Game over! You crashed into a wall!")
    score.game_over()
    game = False
  for segment in snake.segments[1:]:
    if segment.distance(snake.head) < 10:
      s.speak("Your snake is tangled! Game over!")
      score.game_over()
      game = False

screen.exitonclick()
예제 #7
0
    if game_ball.ycor() >= 280 or game_ball.ycor() <= -280:
        ball_heading = game_ball.heading()
        game_ball.setheading(-ball_heading)

    # bounce on paddle
    if left_paddle.distance(game_ball) <= 30 or right_paddle.distance(
            game_ball) <= 30:
        ball_heading = game_ball.heading()
        game_ball.setheading(180 - ball_heading)

    # if misse paddle
    if game_ball.xcor() > 400:
        game_ball.reset()
        left_score.write_score()
    elif game_ball.xcor() < -400:
        game_ball.reset()
        right_score.write_score()

    # check score
    score = left_score.get_score()
    if (score > 10):
        game_on = False
        left_score.game_over("Left")

    score = right_score.get_score()
    if (score > 10):
        game_on = False
        right_score.game_over("Right")

screen.exitonclick()
예제 #8
0
파일: main.py 프로젝트: aak-301/snake-game

sc.listen()
sc.onkey(snake.up, "Up")
sc.onkey(snake.down, "Down")
sc.onkey(snake.left, "Left")
sc.onkey(snake.right, "Right")

game = True
while game:
    sc.update()
    time.sleep(0.1)
    snake.move()

    if snake.head.distance(food) < 15:
        food.refresh()
        snake.extend()
        scores.update_score()

    if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor() > 280 or snake.head.ycor() < -280:
        game = False
        scores.game_over()

    for segments in snake.turtle_list[1:]:
        if snake.head.distance(segments) < 10:
            game = False
            scores.game_over()


sc.exitonclick()
예제 #9
0
screen = Screen()
screen.setup(width=WIDTH, height=HEIGHT)
screen.tracer(0)

player = Player()
car_manager = CarManager()
scoreBoard = ScoreBoard()

screen.listen()
screen.onkeypress(player.go_up, "w")

game_is_on = True
while game_is_on:
    time.sleep(0.05)
    screen.update()
    car_manager.create_car()
    car_manager.move_cars()

    for car in car_manager.all_cars:
        if car.distance(player) < 20:
            game_is_on = False
            scoreBoard.game_over()

    if player.is_at_finish_line():
        player.go_to_start()
        car_manager.level_up()
        scoreBoard.increase_level()
        car_manager.reset()

screen.exitonclick()