您现在的位置是:首页 > 正文

python turtle 做贪吃蛇(原创)(我是个初学者,不太行,敬请谅解)

2024-01-30 19:53:28阅读 2

今天,咱用Python的turtle来做贪吃蛇。

首先,导入包~~~

import turtle  #主包
import time   #换帧
import random  #随机

因为在本代码中,内置turtle只是个克隆机,因此隐藏~~~

#Set turtle
turtle.clear()
turtle.ht()

在本程序中,蛇是有生命值的、分数的,生命值在15~25条命之间。本程序中每秒10帧,因此Delay = 0.1。

#Lives
lives = random.randint(15,25)

#Delay
delay = 0.1

# Score
score = 0
high_score = 0

接下来,初始化开始,包括主角们和配角们;medicineif是决定medicine的是否存在的变量~~~

#Medicineif
medicineif = 0

# Set up the screen
wn = turtle.Screen()
wn.title("The Hungry Snake Game    By 胡辰")
wn.bgcolor("#008000")
wn.setup(width=600, height=600)
wn.tracer(0)  # Turns off the screen updates

# Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("purple")
head.penup()
head.goto(0, 0)
head.direction = "stop"

# Snake foods
rasberry = turtle.Turtle()
rasberry.speed(0)
rasberry.shape("circle")
rasberry.color("red")
rasberry.penup()
rasberry.goto(0, 100)

strawberry = turtle.Turtle()
strawberry.speed(0)
strawberry.shape("triangle")
strawberry.left(270)
strawberry.color("#BB0000")
strawberry.penup()
strawberry.goto(20,190)

blueberry = turtle.Turtle()
blueberry.speed(0)
blueberry.shape("circle")
blueberry.color("#5050FF")
blueberry.penup()
blueberry.goto(100, 100)

#Bombs
bomb = turtle.Turtle()
bomb.speed(0)
bomb.shape('circle')
bomb.color('black')
bomb.penup()
bomb.goto(random.randint(-100,100),random.randint(-100,100))
bomb2 = turtle.Turtle()
bomb2.speed(0)
bomb2.shape('circle')
bomb2.color('black')
bomb2.penup()
bomb2.goto(random.randint(-100,100),random.randint(-100,100))
bomb3 = turtle.Turtle()
bomb3.speed(0)
bomb3.shape('circle')
bomb3.color('black')
bomb3.penup()
bomb3.goto(random.randint(-100,100),random.randint(-100,100))
bomb4 = turtle.Turtle()
bomb4.speed(0)
bomb4.shape('circle')
bomb4.color('black')
bomb4.penup()
bomb4.goto(random.randint(-100,100),random.randint(-100,100))


#Life medicine
turtle.begin_poly()
turtle.color('#00FFC8')
turtle.begin_fill()
turtle.circle(15)
turtle.end_fill()
turtle.goto(-7,20)
turtle.begin_fill()
turtle.goto(-7,50)
turtle.goto(7,50)
turtle.goto(7,20)
turtle.end_fill()
turtle.end_poly()
medicine=turtle.get_poly()
turtle.register_shape('medicine',medicine)
turtle.clear()
medicine = turtle.Turtle()
medicine.shape('medicine')
medicine.color('#00FFC8')
medicine.turtlesize(0.625)
medicine.lt(90)
medicine.ht()

#Segments
segments = []

# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 230)
pen.write("Score: 0  Highest Score: 0  \n     Lives: {}".format(lives), align="center",
          font=("Courier", 18, "normal"))

接下来慢慢进入主场了!

定义蛇的移动并键盘监听:

# Functions


def go_up():
    if head.direction != "down":
        head.direction = "up"


def go_down():
    if head.direction != "up":
        head.direction = "down"


def go_left():
    if head.direction != "right":
        head.direction = "left"


def go_right():
    if head.direction != "left":
        head.direction = "right"


def move():
    if head.direction == "up":
        y = head.ycor()
        head.sety(y + 20)

    if head.direction == "down":
        y = head.ycor()
        head.sety(y - 20)

    if head.direction == "left":
        x = head.xcor()
        head.setx(x - 20)

    if head.direction == "right":
        x = head.xcor()
        head.setx(x + 20)


# Keyboard bindings
wn.listen()
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")

现在,进入主循环~~~

while True:
    lives = random.randint(15, 25)
    # Main game loop
    while lives > 0:
        wn.update()
        if random.randint(0,100)>99:
            medicine.pu()
            medicine.goto(random.randint(-290,290),random.randint(-290,290))
            medicine.st()
            medicineif = 1
        # Check for a collision with the border
        if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290 or head.distance(bomb) < 20 or head.distance(bomb2) < 20 or head.distance(bomb3) < 20 or head.distance(bomb4) < 20:
            time.sleep(1)
            head.goto(0, 0)
            head.direction = "stop"
            lives -= 1
            x = random.randint(-290,290)
            y = random.randint(-290, 290)
            bomb.goto(x,y)
            x = random.randint(-290, 200)
            y = random.randint(-290, 200)
            bomb2.goto(x, y)
            x = random.randint(-290, 290)
            y = random.randint(-290, 290)
            bomb3.goto(x, y)
            x = random.randint(-290, 290)
            y = random.randint(-290, 290)
            bomb4.goto(x, y)
            # Hide the segments
            for segment in segments:
                segment.goto(1000, 1000)

            # Clear the segments list
            segments.clear()

            # Reset the score
            score = 0

            # Reset the delay
            delay = 0.1

            pen.clear()
            pen.write("Score: {}  Highest Score: {}  \n     Lives: {}".format(score, high_score,lives),
                      align="center", font=("Courier", 18, "normal"))

        # Check for a collision with the food
        if head.distance(rasberry) < 20:
            # Move the food to a random spot
            x = random.randint(-290, 290)
            y = random.randint(-290, 290)
            rasberry.goto(x, y)

            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#FF0070")
            new_segment.penup()
            segments.append(new_segment)


            # Shorten the delay
            delay -= 0.001

            # Increase the score
            score += 10

            if score > high_score:
                high_score = score

            pen.clear()
            pen.write("Score: {}  Highest Score: {}  \n     Lives: {}".format(score, high_score, lives),
                      align="center", font=("Courier", 18, "normal"))
            # Check for a collision with the food
        if head.distance(blueberry) < 20:
            # Move the food to a random spot
            x = random.randint(-290, 290)
            y = random.randint(-290, 290)
            blueberry.goto(x, y)

            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#0000FF")
            new_segment.penup()
            segments.append(new_segment)
            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#0000FF")
            new_segment.penup()
            segments.append(new_segment)

            # Shorten the delay
            delay -= 0.001

            # Increase the score
            score += 50

            if score > high_score:
                high_score = score

            pen.clear()
            pen.write("Score: {}  Highest Score: {}  \n     Lives: {}".format(score, high_score,lives),
                      align="center", font=("Courier", 18, "normal"))
        if head.distance(strawberry) < 20:
            # Move the food to a random spot
            x = random.randint(-290, 290)
            y = random.randint(-290, 290)
            strawberry.goto(x, y)

            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#BB002E")
            new_segment.penup()
            segments.append(new_segment)
            delay -= 0.001

            # Increase the score
            score += 20

            if score > high_score:
                high_score = score

            pen.clear()
            pen.write("Score: {}  Highest Score: {}  \n     Lives: {}".format(score, high_score, lives),
                      align="center", font=("Courier", 18, "normal"))

        if medicineif == 1 and head.distance(medicine) < 25:
            #Add lives
            lives += 1
            # Move the medicine to a random spot
            x = random.randint(-290, 290)
            y = random.randint(-290, 290)
            medicine.goto(x, y)
            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)
            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)# Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)
            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)
            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)
            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)  # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)
            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)

            delay -= 0.005

            score+=100

            if score > high_score:
                high_score = score

            pen.clear()
            pen.write("Score: {}  Highest Score: {}  \n     Lives: {}".format(score, high_score, lives),
                      align="center", font=("Courier", 18, "normal"))
        # Move the end segments first in reverse order
        for index in range(len(segments) - 1, 0, -1):
            x = segments[index - 1].xcor()
            y = segments[index - 1].ycor()
            segments[index].goto(x, y)

        # Move segment 0 to where the head is
        if len(segments) > 0:
            x = head.xcor()
            y = head.ycor()
            segments[0].goto(x, y)

        move()

        # Check for head collision with the body segments
        for segment in segments:
            if segment.distance(head) < 20:
                time.sleep(1)
                head.goto(0, 0)
                head.direction = "stop"

                # Hide the segments
                for segment in segments:
                    segment.goto(1000, 1000)

                # Clear the segments list
                segments.clear()

                # Reset the score
                score = 0

                # Reset the delay
                delay = 0.1

                # Update the score display
                pen.clear()
                pen.write("Score: {}  Highest Score: {}  \n     Lives: {}".format(score, high_score, lives),
                          align="center", font=("Courier", 18, "normal"))

        time.sleep(delay)
    pen.clear()
    pen.write("Your Highest Score: {}".format(high_score),
              align="center", font=("Courier", 24, "normal"))
wn.mainloop()

总代码:

# Simple Snake Game in Python 3 for Beginners
import turtle
import time
import random

#Set turtle
turtle.clear()
turtle.ht()

#Lives
lives = random.randint(15,25)

#Delay
delay = 0.1

# Score
score = 0
high_score = 0

#Medicineif
medicineif = 0

# Set up the screen
wn = turtle.Screen()
wn.title("The Hungry Snake Game    By 胡辰")
wn.bgcolor("#008000")
wn.setup(width=600, height=600)
wn.tracer(0)  # Turns off the screen updates

# Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("purple")
head.penup()
head.goto(0, 0)
head.direction = "stop"

# Snake foods
rasberry = turtle.Turtle()
rasberry.speed(0)
rasberry.shape("circle")
rasberry.color("red")
rasberry.penup()
rasberry.goto(0, 100)

strawberry = turtle.Turtle()
strawberry.speed(0)
strawberry.shape("triangle")
strawberry.left(270)
strawberry.color("#BB0000")
strawberry.penup()
strawberry.goto(20,190)

blueberry = turtle.Turtle()
blueberry.speed(0)
blueberry.shape("circle")
blueberry.color("#5050FF")
blueberry.penup()
blueberry.goto(100, 100)

#Bombs
bomb = turtle.Turtle()
bomb.speed(0)
bomb.shape('circle')
bomb.color('black')
bomb.penup()
bomb.goto(random.randint(-100,100),random.randint(-100,100))
bomb2 = turtle.Turtle()
bomb2.speed(0)
bomb2.shape('circle')
bomb2.color('black')
bomb2.penup()
bomb2.goto(random.randint(-100,100),random.randint(-100,100))
bomb3 = turtle.Turtle()
bomb3.speed(0)
bomb3.shape('circle')
bomb3.color('black')
bomb3.penup()
bomb3.goto(random.randint(-100,100),random.randint(-100,100))
bomb4 = turtle.Turtle()
bomb4.speed(0)
bomb4.shape('circle')
bomb4.color('black')
bomb4.penup()
bomb4.goto(random.randint(-100,100),random.randint(-100,100))


#Life medicine
turtle.begin_poly()
turtle.color('#00FFC8')
turtle.begin_fill()
turtle.circle(15)
turtle.end_fill()
turtle.goto(-7,20)
turtle.begin_fill()
turtle.goto(-7,50)
turtle.goto(7,50)
turtle.goto(7,20)
turtle.end_fill()
turtle.end_poly()
medicine=turtle.get_poly()
turtle.register_shape('medicine',medicine)
turtle.clear()
medicine = turtle.Turtle()
medicine.shape('medicine')
medicine.color('#00FFC8')
medicine.turtlesize(0.625)
medicine.lt(90)
medicine.ht()

#Segments
segments = []

# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 230)
pen.write("Score: 0  Highest Score: 0  \n     Lives: {}".format(lives), align="center",
          font=("Courier", 18, "normal"))


# Functions


def go_up():
    if head.direction != "down":
        head.direction = "up"


def go_down():
    if head.direction != "up":
        head.direction = "down"


def go_left():
    if head.direction != "right":
        head.direction = "left"


def go_right():
    if head.direction != "left":
        head.direction = "right"


def move():
    if head.direction == "up":
        y = head.ycor()
        head.sety(y + 20)

    if head.direction == "down":
        y = head.ycor()
        head.sety(y - 20)

    if head.direction == "left":
        x = head.xcor()
        head.setx(x - 20)

    if head.direction == "right":
        x = head.xcor()
        head.setx(x + 20)


# Keyboard bindings
wn.listen()
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")

while True:
    lives = random.randint(15, 25)
    # Main game loop
    while lives > 0:
        wn.update()
        if random.randint(0,100)>99:
            medicine.pu()
            medicine.goto(random.randint(-290,290),random.randint(-290,290))
            medicine.st()
            medicineif = 1
        # Check for a collision with the border
        if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290 or head.distance(bomb) < 20 or head.distance(bomb2) < 20 or head.distance(bomb3) < 20 or head.distance(bomb4) < 20:
            time.sleep(1)
            head.goto(0, 0)
            head.direction = "stop"
            lives -= 1
            x = random.randint(-290,290)
            y = random.randint(-290, 290)
            bomb.goto(x,y)
            x = random.randint(-290, 200)
            y = random.randint(-290, 200)
            bomb2.goto(x, y)
            x = random.randint(-290, 290)
            y = random.randint(-290, 290)
            bomb3.goto(x, y)
            x = random.randint(-290, 290)
            y = random.randint(-290, 290)
            bomb4.goto(x, y)
            # Hide the segments
            for segment in segments:
                segment.goto(1000, 1000)

            # Clear the segments list
            segments.clear()

            # Reset the score
            score = 0

            # Reset the delay
            delay = 0.1

            pen.clear()
            pen.write("Score: {}  Highest Score: {}  \n     Lives: {}".format(score, high_score,lives),
                      align="center", font=("Courier", 18, "normal"))

        # Check for a collision with the food
        if head.distance(rasberry) < 20:
            # Move the food to a random spot
            x = random.randint(-290, 290)
            y = random.randint(-290, 290)
            rasberry.goto(x, y)

            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#FF0070")
            new_segment.penup()
            segments.append(new_segment)


            # Shorten the delay
            delay -= 0.001

            # Increase the score
            score += 10

            if score > high_score:
                high_score = score

            pen.clear()
            pen.write("Score: {}  Highest Score: {}  \n     Lives: {}".format(score, high_score, lives),
                      align="center", font=("Courier", 18, "normal"))
            # Check for a collision with the food
        if head.distance(blueberry) < 20:
            # Move the food to a random spot
            x = random.randint(-290, 290)
            y = random.randint(-290, 290)
            blueberry.goto(x, y)

            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#0000FF")
            new_segment.penup()
            segments.append(new_segment)
            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#0000FF")
            new_segment.penup()
            segments.append(new_segment)

            # Shorten the delay
            delay -= 0.001

            # Increase the score
            score += 50

            if score > high_score:
                high_score = score

            pen.clear()
            pen.write("Score: {}  Highest Score: {}  \n     Lives: {}".format(score, high_score,lives),
                      align="center", font=("Courier", 18, "normal"))
        if head.distance(strawberry) < 20:
            # Move the food to a random spot
            x = random.randint(-290, 290)
            y = random.randint(-290, 290)
            strawberry.goto(x, y)

            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#BB002E")
            new_segment.penup()
            segments.append(new_segment)
            delay -= 0.001

            # Increase the score
            score += 20

            if score > high_score:
                high_score = score

            pen.clear()
            pen.write("Score: {}  Highest Score: {}  \n     Lives: {}".format(score, high_score, lives),
                      align="center", font=("Courier", 18, "normal"))

        if medicineif == 1 and head.distance(medicine) < 25:
            #Add lives
            lives += 1
            # Move the medicine to a random spot
            x = random.randint(-290, 290)
            y = random.randint(-290, 290)
            medicine.goto(x, y)
            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)
            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)# Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)
            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)
            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)
            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)  # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)
            # Add a segment
            new_segment = turtle.Turtle()
            new_segment.speed(0)
            new_segment.shape("square")
            new_segment.color("#00FFC8")
            new_segment.penup()
            segments.append(new_segment)

            delay -= 0.005

            score+=100

            if score > high_score:
                high_score = score

            pen.clear()
            pen.write("Score: {}  Highest Score: {}  \n     Lives: {}".format(score, high_score, lives),
                      align="center", font=("Courier", 18, "normal"))
        # Move the end segments first in reverse order
        for index in range(len(segments) - 1, 0, -1):
            x = segments[index - 1].xcor()
            y = segments[index - 1].ycor()
            segments[index].goto(x, y)

        # Move segment 0 to where the head is
        if len(segments) > 0:
            x = head.xcor()
            y = head.ycor()
            segments[0].goto(x, y)

        move()

        # Check for head collision with the body segments
        for segment in segments:
            if segment.distance(head) < 20:
                time.sleep(1)
                head.goto(0, 0)
                head.direction = "stop"

                # Hide the segments
                for segment in segments:
                    segment.goto(1000, 1000)

                # Clear the segments list
                segments.clear()

                # Reset the score
                score = 0

                # Reset the delay
                delay = 0.1

                # Update the score display
                pen.clear()
                pen.write("Score: {}  Highest Score: {}  \n     Lives: {}".format(score, high_score, lives),
                          align="center", font=("Courier", 18, "normal"))

        time.sleep(delay)
    pen.clear()
    pen.write("Your Highest Score: {}".format(high_score),
              align="center", font=("Courier", 24, "normal"))
wn.mainloop()

由于我三升四,不大知道咋解读易懂,因此大家看注释就好了~~~~

下期预告:升级版Python turtle贪吃蛇

网站文章

  • 大话主席(superslide和 touchslide)插件的使用

      对于实现效果来说,插件的办事效率真的是太高了,而且里边也被处理过兼容性,用起来实在是特别方便,现在我们来说说大话主席中的slide插件,它分为pc端(superslide)和移动端(touchslide)。 superslide:   1、引用jQuery.js 和 jquery.SuperSlide.js 因为SuperSlide是基于jQuery的插件,所以前提...

    2024-01-30 19:53:19
  • 详解java定时任务 热门推荐

    在我们编程过程中如果需要执行一些简单的定时任务,无须做复杂的控制,我们可以考虑使用JDK中的Timer定时任务来实现。下面LZ就其原理、实例以及Timer缺陷三个方面来解析java Timer定时器。一、简介 在java中一个完整定时任务需要由Timer、TimerTask两个类来配合完成。 API中是这样定义他们的,Timer:一种工具,线程用其安排以后在后台线程中执行的任务。可安排任

    2024-01-30 19:53:11
  • 计算机网络第二章知识点复习

    计算机网络第二章知识点复习缩写HTTP:超文本传输协议 HTML:超文本标记语言 URL:统一资源定位符 RTT:往返时间 FTP:文件传输协议 SMTP:简单邮件传输协议 MIME:多媒体邮件扩展 POP:邮局协议 IMAP:因特网邮件访问协议 DNS:域名系统 TLD:顶级域 DHT:分布式散列表 API:应用程序编程接口 RR:资源记录术语**socket:套接字

    2024-01-30 19:52:41
  • java运算符

    java运算符

    java运算符的相关内容

    2024-01-30 19:52:34
  • 计算机远程桌面多用户连接,电脑远程连接如何打开并设置多用户登录

    计算机远程桌面多用户连接,电脑远程连接如何打开并设置多用户登录

    远程连接功能,对于普通用户来说,应该是用的比较少的功能之一,所以在win7系统上是默认关闭的一个功能,可是有部分用户,在某些时候,可能会用到远程桌面连接功能,那么究竟应该如何打开并设置多用户登录呢?针...

    2024-01-30 19:52:26
  • 库函数的模拟实现

    模拟实现strlen://方法1:计数器方式int my_strlen(const char *str){ int count = 0; assert(str); while (*str) { ++...

    2024-01-30 19:51:58
  • 正则表达式的反向引用

    1、反向引用(Backreferences)是Java正则表达式的重要特性。为了理解反向引用,我们先对解释Java正则表达式里面的组。组在正则表达式中意味着将多个字符作为一个单独的单元,通过将字符放在()中,每个()代表一个组。每一组都有一个行号,从1开始。Matcher类的groupCount方法返回模式(Pattern)关联Matcher实例组的个数。第0组指向整个表达式。2、

    2024-01-30 19:51:41
  • 宁波银行技术面经-2021.8

    宁波银行技术面经-2021.8校园招聘 校园招聘 1、自我介绍 2、创建线程有几种方式 3、抽象类和接口的区别 4、你的技术栈有哪些 5、介绍一个你做过的项目 6、项目中有没有遇到困难,讲一下 7、说...

    2024-01-30 19:51:35
  • nodejs基于express4+mysql+express-controller的mvc框架

    在用php开发app后台的时候获得了一些灵感,参考了一下php mvc设计,用express尝试搭建了一个微框架 框架目录: config ------------- 配置文件目录 controllers ------- 控制器 models ----------- 模型 public ------------- 静态文件目录 views -------------- 视图 app.

    2024-01-30 19:51:28
  • 【Spring Boot实战】在线学习平台

    本项目基于SpringMVC和MyBatis框架,实现了一个在线学习平台。该平台由博客模块和在线OJ模块组成。

    2024-01-30 19:50:58