所以我发现了这个叫法拉利的人创造的伟大的乒乓球比赛,我被赋予了让两人打出两分但没有高分的任务。我已经尝试了很多东西,除了当我制作第二个控制运动的代码块时,它要么被完全忽略,要么我会按下它分配的向上按钮,它会一直向上并且不会回来。
from decimal import Decimal, getcontext
getcontext().prec = 20
import time,random,os,sys
from Tkinter import *
from tkFileDialog import *
import tkFont
import os,sys
## decimal is for numbers and arithmatic that use irrational numbers
## Tkinter is the graphical interface for the program
## tkFont is the window that pops up
## os is operating system
## sys is system
## there are 2 different variables for player 1 and player 2 because one variable is a variable for a number, the other creates the rectangle(the actual paddle)
## these are just variables that represent numbers
refreshrate=100
do = 1
done=0
ballX=320
ballY=240
playerone=180
py2=180
pause=0
up=0
down=0
root = Tk()
root.title("Pong")
c = Canvas(root,width=640,height=480,bg='black')
c.pack()
xc = float(2)
yc = float(random.randint(-3,3))
while(yc==0):
yc = float(random.randint(-3,3))
ball = c.create_oval(ballX-10,ballY-10,ballX+10,ballY+10,fill='white')
player = c.create_rectangle(610,playerone,625,playerone+120,fill='blue')
playertwo = c.create_rectangle(15,playerone,30,playerone+120,fill='blue')
c.create_rectangle(318,0,322,480,fill='white')
c.create_rectangle(318,0,322,30,fill='white')
score = 0
escore= 0
lives = 5
font = tkFont.Font(family = "Book Antiqua", size = 15, weight = "bold")
## to change the keys in which you press to control the paddles, change the letter or arrow key between the quotation marks next to str a
## player1 controls
def up(event):
global playerone, player, py2, playertwo, high, low
up = 1
def down(event):
global playerone, player, py2, playertwo, high, low
down = 1
def escape(event):
global do, lives
lives = -1
do = 0
root.quit
root.quit()
def onkeyrelease(event):
global up, down, do, lives, pause
key = event.keysym
if (str(key)=="w"):
up = 0
if (str(key)=="s"):
down = 0
if (str(key)=="Escape" and done==1):
root.quit
do = 0
lives = -1
root.quit()
if (str(key)=="p"):
if(pause==1):
pause=0
elif(pause==0):
pause=1
def buttoninit():
root.bind("<w>",up)
root.bind("<s>",down)
root.bind("`",escape)
root.bind('<KeyRelease>', onkeyrelease)
buttoninit()
## player2 controls
def high(event):
global playerone, player, py2, playertwo, up, down
up = 1
def low(event):
global playerone, player, py2, playertwo, up, down
down = 1
def leave(event):
global do, lives
lives = -1
do = 0
root.quit
root.quit()
def offkeyrelease(event):
global up, down, do, lives, pause
key = event.keysym
if (str(key)=="Up"):
up = 0
if (str(key)=="Down"):
down = 0
if (str(key)=="Escape" and done==1):
root.quit
do = 0
lives = -1
root.quit()
if (str(key)=="p"):
if(pause==1):
pause=0
elif(pause==0):
pause=1
def buttononit():
root.bind("<Up>",high)
root.bind("<Down>",low)
root.bind("`",leave)
root.bind('<KeyRelease>', offkeyrelease)
buttononit()
c.create_text(475,10,text='Lives:',fill='white',font=font)
livestext = c.create_text(525,10,text=lives,fill='white',font=font)
c.create_text(475,25,text='Score:',fill='white',font=font)
scoretext = c.create_text(525,25,text=score,fill='white',font=font)
print "Push 'p' to pause"
var=1
while (do):
## player movement controls
if(playerone>0 and up==1 and down!=1 and pause==0):
playerone=playerone-5
if(playerone<360 and down==1 and up!=1 and pause==0):
playerone=playerone+5
## player collision detection
if(ballX>=605 and ballY+10>=playerone and ballX<=616 and ballY+10>=playerone and ballY-10<=playerone+120):
xc*=-1
ballX=605
if(xc>-10):
xc=float(xc)-float(0.4)
if(yc>0):
yc=float(random.randint(15,70))
yc=float(yc)/float(10)
else:
yc=float(random.randint(-70,-15))
yc=float(yc)/float(10)
## playertwo movement controls
if (playertwo>0 and high==1 and low!=1 and pause==0):
playertwo=playertwo+5
if (playertwo<360 and low==1 and high!=1 and pause==0):
playertwo=playertwo-5
## playertwo collision detection
if(ballX<=40 and ballX>=29 and ballY+py2 and ballY-10<=py2+120):
xc*=-1
ballX=40
if(xc<10):
xc=float(xc)+float(0.4)
if(yc>0):
yc=float(random.randint(15,70))
yc=float(yc)/float(10)
else:
yc=float(random.randint(-70,-15))
yc=float(yc)/float(10)
## left and right bounds collision detection (aka missed paddle)
if(ballX>=630 and xc>0):
ballX=320
ballY=240
xc = 2
yc = random.randint(-3,3)
while(yc==0):
yc = random.randint(-3,3)
lives = lives-1
escore=escore+1
if(ballX<=10 and xc<0):
ballX=320
ballY=240
xc = 2
yc = random.randint(-3,3)
while(yc==0):
yc = random.randint(-3,3)
score=score+1
## top and bottom bounds colliison detection
if(playerone<0):
playerone=0
if(playerone>360):
playerone=360
if(ballY>=470 or ballY<=10):
yc*=-1
## AI(artificial intelligence(ball)) movement controls
if(py2+60<ballY and py2<360 and xc<0 and pause==0):
py2=py2+4
if(py2+60>ballY and py2>0 and xc<0 and pause==0):
py2=py2-4
if(pause==0):
ballX=ballX+xc
ballY=ballY+yc
c.delete(ball)
ball = c.create_oval(ballX-10,ballY-10,ballX+10,ballY+10,fill='white')
c.delete(player)
player = c.create_rectangle(610,playerone,625,playerone+120,fill='blue')
c.delete(playertwo)
# to change the game to single player, remove the playerone's in the following line and replace them with py2's
# this are the coordinates for the paddle
# the first playerone, tracks the position of the thing that the paddle is following
# 1#= how wide it is, 2# tracks the other object, 3# the width, 4# how tall the paddle is, 5# the color
playertwo = c.create_rectangle(15, playerone ,30, playerone + 120 ,fill='blue')
c.delete(livestext)
livestext = c.create_text(525,10,text=lives,fill='white',font=font)
c.delete(scoretext)
scoretext = c.create_text(525,25,text=score,fill='white',font=font)
c.update()
time.sleep(Decimal("1")/Decimal("100"))
if(lives==0):
do=0
if(score > 0):
print score
done = 1
try:
c.update()
except:
print "Error"
root.quit
while(lives==0):
c.update()
try:
c.update()
except:
print "Error!!!"
root.quit