程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

100 lines -python table tennis games

編輯:Python

In today's b I saw a good and interesting video on the station ,《 use Python Development of two pairs of table tennis games 》, Ha ha ha , So I finished it quickly , Then he wrote a .

Blog :Hzy The blog of

Portal

use Python Development of two pairs of table tennis games _ Bili, Bili (゜-゜)つロ Let me propose a toast to ~-bilibili

design sketch :

Here is the complete code , Just look at the notes .

import turtle as t
# Add points
def add_score(who):
pen.clear()
who.score += 1
score_text = 'p1:{},p2:{}'.format(p1.score, p2.score)
pen.write(score_text, align='center', font={"Arial", 20, 'bold'})
# Create players
class player(t.Turtle):
def __init__(self, color, shape, goto):
super(player, self).__init__()
self.ht() # Hide animation
self.up() # Lift up the brush.
self.score = 0 # Set default grades
self.color(color) # Set the color
self.speed(0) # set speed
self.goto(goto) # Set the default location
self.shape(shape) # Set shape
self.shapesize(5, 1) # Set shape scale
self.st() # Show animation
# Move up
def set_up(self):
y = self.ycor()
y = y + 20
self.sety(y)
# Move down the
def set_down(self):
y = self.ycor()
y = y - 20
self.sety(y)
game = t.Screen()
game.title(" Table tennis for two ")
game.bgcolor('black')
game.setup(800, 600)
p1 = player(color="blue", shape="square", goto=(-390, 0))
p2 = player(color="red", shape="square", goto=(380, 0))
# Set ball
pp = t.Turtle()
pp.ht()
pp.up()
pp.speed(0)
pp.color('yellow')
pp.shape('circle')
pp.dx = 5
pp.dy = 5
pp.st()
# Set brush , Painting achievement
pen = t.Turtle()
pen.ht()
pen.up()
pen.color("white")
pen.goto(-50, 250)
score_text = 'p1:{},p2:{}'.format(p1.score, p2.score)
pen.write(score_text, align='center', font={"Arial", 20, 'bold'})
# Monitor button
game.listen()
game.onkey(p1.set_up, 'w')
game.onkey(p1.set_down, 's')
game.onkey(p2.set_up, 'Up')
game.onkey(p2.set_down, 'Down')
# Circular game
while True:
# game.update()
pp.setx(pp.xcor() + pp.dx)
pp.sety(pp.ycor() + pp.dy)
if pp.ycor() > 290 or pp.ycor() < -290: # The upper and lower boundaries bounce after catching
pp.dy *= -1
# Allowable catching area
p1_y_range_up = p1.ycor() + 50
p1_y_range_down = p1.ycor() - 50
p2_y_range_up = p2.ycor() + 50
p2_y_range_down = p2.ycor() - 50
# The left and right borders catch the bounce , Or the opponent scores
if pp.xcor() < -370:
if p1_y_range_up > pp.ycor() > p1_y_range_down:
pp.dx *= -1
else:
print("p1 lose")
print("p2 score ")
add_score(p2)
pp.goto(0, 0)
elif pp.xcor() > 360:
if p2_y_range_up > pp.ycor() > p2_y_range_down:
pp.dx *= -1
else:
print("p2 lose")
print("p1 score ")
add_score(p1)
pp.goto(0, 0)
game.mainloop()

Ha ha ha , I think this library is very interesting , Come and study it next time , Leave a hole first !!!!


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved