首页 > 解决方案 > 什么错误“没有显示名称和没有 $DISPLAY 环境变量”

问题描述

我是 Python 的初学者,我试图在http://christianthompson.com/sites/default/files/Pong/pong.py上重新利用这个人的代码

但是当我尝试运行它时,我收到错误消息 在此处输入图像描述

代码是:

import turtle #turtle is a library for beginners to understand python and develop games.
import os

space = turtle.Screen()
space.title("Pong - CUHK")
space.bgcolor("black")
space.setup(width=1280, height=720)
space.tracer(0)

# Score
score_c = 0
score_u = 0

#player C #set the basic data about player
player_c = turtle.Turtle()
player_c.speed(0)
player_c.shape('square') 
player_c.shapesize(stretch_wid=5, stretch_len=1) #stretches the square into a rectantle
player_c.color('white') 
player_c.penup()
player_c.goto(-620, 0)

#player U #set the basic data about player
player_u = turtle.Turtle()
player_u.speed(0)
player_u.shape('square')
player_u.shapesize(stretch_wid=5, stretch_len=1)
player_u.color('white') 
player_u.penup()
player_u.goto(620, 0)

# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 2
ball.dy = 2

#Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Player C: 0  Player U: 0", align="center", font=("Courier", 24, "normal"))

#Function
def player_c_up():
  y = player_c.ycor()
  y += 20 #add pixel
  player_c.sety(y)

def player_c_down():
  y = player_c.ycor()
  y -= 20 #minus pixel
  player_c.sety(y)

def player_u_up():
  y = player_u.ycor()
  y += 20 
  player_u.sety(y)

def player_u_down():
  y = player_u.ycor()
  y -= 20 
  player_u.sety(y)

#moving
space.listen()
space.onkeypress(player_c_up,'w')
space.onkeypress(player_c_down,'s')
space.onkeypress(player_u_up,'Up')
space.onkeypress(player_u_down,'Down')

#Game loop
while True:
   space.update()

# Move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)


#Border set
if ball.ycor()>350:
   ball.sety(350)
   ball.dy *= -1 #change the direction
   os.system("afplay bounce.wav&")
 
elif ball.ycor()<-350:
   ball.sety(-350)
   ball.dy *= -1 #change the direction
   os.system("afplay bounce.wav&")

if ball.xcor()>630:
  score_c += 1
  pen.clear()
  pen.write("Player C: {}  Player U: {}".format(score_c, score_u), align="center", font=("Courier", 24, "normal"))
  ball.goto(0, 0)
  ball.dx *= -1 #change the direction

elif ball.xcor()<-630:
   score_u += 1
   pen.clear()
   pen.write("Player C: {}  Player U: {}".format(score_c, score_u), align="center", font=("Courier", 24, "normal"))
   ball.goto(0,0)
   ball.dx *= -1 #change the direction

#paddle and ball collisions
if (ball.xcor()>610 and ball.xcor()<620) and (ball.ycor()<player_u.ycor() + 40 and ball.ycor()> player_u.ycor()-40):
  ball.setx(620)
  ball.dx *= -1
  os.system("afplay bounce.wav&")

if (ball.xcor()<-610 and ball.xcor()>-620) and (ball.ycor()<player_c.ycor() + 40 and ball.ycor()> player_c.ycor()-40):
  ball.setx(-610)
  ball.dx *= -1
  os.system("afplay bounce.wav&")

那么错误消息与什么有关,我必须更改的代码在哪里?

标签: python

解决方案


推荐阅读