python海龟漂亮图案代码大全_Python游戏海龟图案

编程知识 更新时间:2023-04-04 07:12:23

所以我有一个项目,我必须创建一个海龟图形游戏,而不使用任何其他模块,除了海龟和随机模块。我正在创造传统的蛇游戏,除了当蛇“吃”的对象,它不会变大。我的游戏的问题是,当“蛇”到达目标时,对象并没有消失,我希望它消失并重新出现在另一个位置。请帮忙。代码如下:from turtle import *

import random

setup(700, 700)

title("Snake Game!")

bgcolor("blue")

# Boundary

bPen = Pen()

bPen.color("Orange")

bPen.up()

bPen.goto(-300, -300)

bPen.down()

for i in range(4):

bPen.ht()

bPen.fd(600)

bPen.lt(90)

# Player

playerPen = Pen()

playerPen.color("Orange")

playerPen.up()

playerPen.width(4)

# Create Circle

circle = Pen()

circle.shape("circle")

circle.color("Red")

circle.up()

circle.speed(0)

circle.goto(-100, 100)

# Speed

speed = 2

# Movement functions

def left():

playerPen.lt(90)

def right():

playerPen.rt(90)

def speedBoost():

global speed

speed += 1

# Key Prompts

onkey(left, "Left")

onkey(right, "Right")

onkey(speedBoost, "Up")

listen()

# Infinity loop and player knockback

while True:

playerPen.fd(speed)

if playerPen.xcor() < -300 or playerPen.xcor() > 300:

playerPen.rt(180)

elif playerPen.ycor() < - 300 or playerPen.ycor() > 300:

playerPen.rt(180)

# Collision with circle

if playerPen.xcor() == circle.xcor() and playerPen.ycor() == circle.ycor():

circle.goto(random.randint(-300, 300), random.randint(-300, 300))

done()

更多推荐

python海龟漂亮图案代码大全_Python游戏海龟图案

本文发布于:2023-04-04 07:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/a4f6c220e01d9fcd8e665ca88e105fb1.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:海龟   图案   漂亮   代码   大全

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 41615文章数
  • 14阅读数
  • 0评论数