コード例 #1
0
ファイル: snake_game.py プロジェクト: MrXisOnline/snake-game
screen.setup(width=640, height=640)
screen.bgcolor("black")
screen.tracer(0)
mySnake = Snake()
screen.onkey(mySnake.up, "Up")
screen.onkey(mySnake.down, "Down")
screen.onkey(mySnake.left, "Left")
screen.onkey(mySnake.right, "Right")
f = Food()
screen.update()
score = 0
b_score = Score(score)
run = True
while run:
    if mySnake.check_food(f.n_pos):
        score += 1
        b_score.update_score(score)
        f.respawn()
    else:
        screen.update()
        time.sleep(0.2)
        mySnake.move()
        if mySnake.check_wall():
            run = False
            b_score.game_over()
        head = mySnake.tur[0]
        for t in mySnake.tur[1:]:
            if head.distance(t) < 10:
                b_score.game_over()

screen.exitonclick()
コード例 #2
0
    screen.update()
    time.sleep(0.02)
    ball.move()

    # Goal detection
    if ball.xcor() > 580:
        ball.reset("right")
        bat1.reset()
        bat2.reset()
        score_left.increase_score()
        goal = True
        goal_left += 1
        rally = 0
        ball.decrease_speed()
        if goal_left > 9:
            score_right.game_over()
            game_is_on = False

    if ball.xcor() < -580:
        ball.reset("left")
        bat1.reset()
        bat2.reset()
        score_right.increase_score()
        goal = True
        goal_right += 1
        rally = 0
        ball.decrease_speed()
        if goal_right > 9:
            score_right.game_over()
            game_is_on = False
コード例 #3
0
screen.onkey(key="Right", fun=snake.right)
screen.onkey(key="Left", fun=snake.left)

food = Food()

score = Score()

is_continue = True
while is_continue:
    time.sleep(0.09)
    screen.update()
    snake.move()

    if snake.head.distance(food) < 10:
        food.refresh()
        snake.extend()
        score.increase_score()
    if snake.head.xcor() > 490 or snake.head.xcor() < -490 or snake.head.ycor(
    ) > 390 or snake.head.ycor() < -390:
        is_continue = False
        score.game_over()

    for turtle in snake.turtles:
        if turtle == snake.head:
            pass
        elif snake.head.distance(turtle) < 7.5:
            is_continue = False
            score.game_over()

screen.exitonclick()
コード例 #4
0
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")

is_game_on = True

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

    # detect collision with food
    if snake.head.distance(foodie) < 15:
        foodie.refresh()
        snake.extend()
        score1.increase_score()
        score1.update_scoreboard()
    # detect collision with wall

    if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor(
    ) > 280 or snake.head.ycor() < -280:
        is_game_on = False
        score1.game_over()
    # detect collision with wall
    for segment in snake.segments[1:]:
        if snake.head.distance(segment) < 10:
            is_game_on = False
            score1.game_over()

screen.exitonclick()