Schlangenkopf bewegt sich nichtPython

Python-Programme
Anonymous
 Schlangenkopf bewegt sich nicht

Post by Anonymous »

Dieser Snake-Spielcode ist in Python-Turtle geschrieben. Ich möchte den Schlangenkopf verschieben, der mit dem Schildkrötenmodul gezeichnet wurde, aber leider funktioniert es nicht. Außerdem füge ich mithilfe der for-Schleife mehrere Futtermittel für die Schlange hinzu. Möglicherweise haben sie ein Kollisionsproblem, aber ich weiß nicht, wie ich das beheben kann...????
Module importieren

Code: Select all

import turtle
from turtle import *
import random
import time

delay = 0.1
level = 1
lives = 3

#score
score = 0
high_score = 0

#setting the screen
wn = turtle.Screen()
wn.title('Snake Game')
wn.bgcolor('black')
wn.bgpic('pic55.png')
#wn.addshape('snake head6.png')
wn.setup(width= 600, height= 600)
wn.tracer(0) #turns off the screen update
Schlangenkopf mit Schildkröte gezeichnet

Code: Select all

#snake head
head = turtle.Turtle()
def draw_circle(color, radius, x, y):
#head(turtle.Turtle())
head.penup()
head.fillcolor(color)
head.goto(x, y)
head.begin_fill()
head.circle(radius)
head.end_fill()
head.hideturtle()
draw_circle("#FF4500", 30, 0, -40)  #face OrangeRed #FF4500 green #2CD717
draw_circle("#ffffff", 10, -10, -5)      #left eye 9659BD purple
draw_circle("#ffffff", 10, 10, -5)      #right eye  B4BCE2 light blue

draw_circle("#4a70e3", 7, -8, -4)      #5e7ede 9eb1eb  4a70e3 royalblue light colors
draw_circle("#4a70e3", 7, 8, -4)

draw_circle("#17202A", 5, -10, -5)      ##17202A black
draw_circle("#17202A", 5, 10, -5)

#colors = random.choice(['green','black'])
#shapes = random.choice(['square'])

#head.shape(shapes)
#head.color(colors)
head.goto(0,0)
head.penup()
head.speed(0) #animation speed
head.direction = 'stop'

#segment = []
Mehrfaches Essen

Code: Select all

#max food
maxfoods =10
foods = []

#snake food
for count in range(maxfoods):
foods.append(turtle.Turtle())
foods[count].color('red')
foods[count].shape('square')
#food[count].shape(shapes)
#food[count].color(colors)
foods[count].penup()
foods[count].speed()
foods[count].speed(0)#animation speed
foods[count].setposition(random.randint(-300, 300) ,random.randint(-300, 300))

segment = []

#pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape('square')
pen.color('white')
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0 High Score: 0 Level: 1 Lives: 3",align = "center", font=("courier", 16, "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 binding
wn.listen()
wn.onkeypress(go_up, 'Up')
wn.onkeypress(go_down, 'Down')
wn.onkeypress(go_left, 'Left')
wn.onkeypress(go_right, 'Right')
While-Schleife

Code: Select all

while True:
wn.update()
#check the border collision
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
time.sleep(1)
head.goto(0, 0)
head.direction = 'stop'
for segments in segment:
segment.goto(1000, 1000)
segment.clear()

#reset the score
score = 0

#reset the delay
delay = 0.1

#reset level
level = 1

pen.clear()
pen.write("Score: {} High Score: {} Level: {} Lives: {}".format(score, high_score, level, lives),align = "center", font=("courier", 16, "normal"))

#check for head collision with the food

for count in range(maxfoods):
#maxfood.forward(3)
if head.distance(foods) <  20:
#MOve the head random
x = random.randint(-290, 290)
y = random.randint(-290, 290)
foods.goto(x, y)

#add body to snake
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape('square')
new_segment.color('green')
new_segment.penup()
segment.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: {} High Score: {} Level: {} Lives: {}".format(score, high_score, level, lives),align = "center", font=("courier", 16, "normal"))

#Move the end body in reverse order
for index in range(len(segment)-1, 0, -1):
x = segment[index-1].xcor()
y = segment[index-1].ycor()
segment[index].goto(x, y)

#move the body 0 t where the head is
if len(segment) > 0:
x = head.xcor()
y = head.ycor()
segment[0].goto(x, y)

move()
#check for head collision with body/segment
for segments in segment:
if segments.distance(head) < 20:
time.sleep(1)
head.goto(0, 0)
head.direction = 'stop'

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

#clear the segment list
segment.clear()

#reset the score
score = 0

#reset the level
level = 1

#update the score display
pen.clear()
pen.write("Score: {} High Score: {} Level: {} Lives: {}".format(score, high_score, level, lives),align = "center", font=("courier", 16, "normal"))

#levels
if level == 1 and score == 50:
level += 1
delay *= 0.9
if level == 2 and score == 100:
level += 1
delay *= 0.9
if level == 3 and score == 250:
level += 1
delay *= 0.9
if level == 4 and score == 350:
level += 1
delay *= 0.9
if level == 5 and score == 450:
level += 5
delay *= 0.9

time.sleep(delay)

wn.maingameloop()

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post