Python,画布上的条增加大小,而不是上下移动

时间:2018-08-21 23:30:36

标签: python canvas pong

我正在创建游戏Pong,现在正在创建桨。

当我按下向下键移动拨片时,拨片会移动,但也会增大。

我不知道为什么它会增长。

我该如何解决这个问题?

代码尚未完成;我现在正专注于桨。

# Implementation of classic arcade game Pong

import simplegui
import random

# initialize globals - pos and vel encode vertical info for paddles
WIDTH = 600
HEIGHT = 400       
BALL_RADIUS = 20
PAD_WIDTH = 8
PAD_HEIGHT = 200
HALF_PAD_WIDTH = PAD_WIDTH / 2
HALF_PAD_HEIGHT = PAD_HEIGHT / 2
LEFT = False
RIGHT = True
ball_pos = [WIDTH / 2, HEIGHT / 2]
ball_vel = [3 , 4]
score1 = 0
score2 = 0
paddle1_pos = 100
paddle2_pos = 100
paddle1_vel = 0
paddle2_vel = 0


# initialize ball_pos and ball_vel for new bal in middle of table
# if direction is RIGHT, the ball's velocity is upper right, else upper left
def spawn_ball(direction):
    global ball_pos, ball_vel # these are vectors stored as lists
    if direction is RIGHT:
        ball_pos = [WIDTH / 2, HEIGHT / 2]
        ball_vel[0] = random.randrange(2,6)
        ball_vel[1] = random.randrange(-3,-2)
    if direction is LEFT:
        ball_pos = [WIDTH / 2, HEIGHT / 2]
        ball_vel[0] = random.randrange(-6,-2)
        ball_vel[1] = random.randrange(2,3)



# define event handlers
def new_game():
    global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel  # these are numbers
    global score1, score2  # these are ints
    score1 = 0
    score2 = 0
    paddle1_pos = 100
    paddle2_pos = 100
    return spawn_ball(RIGHT)

def draw(canvas):
    global score1, score2, paddle1_pos, paddle2_pos, ball_pos, ball_vel


    # draw mid line and gutters
    canvas.draw_line([WIDTH / 2, 0],[WIDTH / 2, HEIGHT], 1, "White")
    canvas.draw_line([PAD_WIDTH, 0],[PAD_WIDTH, HEIGHT], 1, "White")
    canvas.draw_line([WIDTH - PAD_WIDTH, 0 ],[WIDTH - PAD_WIDTH, HEIGHT], 1, "White")

    # update ball
    ball_pos[0] += ball_vel[0]
    ball_pos[1] += ball_vel[1]
    if ball_pos[1] >= HEIGHT - BALL_RADIUS:
        ball_vel[1] = random.randrange(- 5, -3)
        ball_vel[0] =  random.choice([-3,3])
    elif ball_pos[1] <= BALL_RADIUS:
        ball_vel[1] = random.randrange(3,5)
        ball_vel[0] = random.choice([-3,3])
    elif ball_pos[0] >= WIDTH - BALL_RADIUS:
        ball_vel[0] = random.randrange(- 5, -3)
        ball_vel[1] = random.choice([-3,3])
        score1 += 1
        return spawn_ball(LEFT)     
    elif ball_pos[0] <= BALL_RADIUS:
        ball_vel[0] = random.randrange(3,5)
        ball_vel[1] = random.choice([-3,3])
        score2 += 1
        return spawn_ball(RIGHT)

    # draw ball
    canvas.draw_circle(ball_pos, BALL_RADIUS, 2, "White", "White")

    # update paddle's vertical position, keep paddle on the screen
    paddle1_pos += paddle1_vel
    paddle2_pos += paddle2_vel

    # draw paddles
    canvas.draw_line((0,paddle1_pos),(0, paddle1_pos*2),PAD_WIDTH,"White")
    canvas.draw_line((WIDTH, paddle2_pos) ,(WIDTH, paddle2_pos*2),PAD_WIDTH,"White")


    # determine whether paddle and ball collide 





    # draw scores

def keydown(key):
    acc = 1
    global paddle1_vel, paddle2_vel, paddle1_pos
    if key == simplegui.KEY_MAP["s"]:
        paddle1_vel +=  acc
    if key == simplegui.KEY_MAP["down"]:
        paddle2_vel +=  acc

def keyup(key):
    acc = 1
    global paddle1_vel, paddle2_vel
    if key == simplegui.KEY_MAP["s"]:
        paddle1_vel -= acc
    if key == simplegui.KEY_MAP["down"]:
        paddle2_vel -= acc



# create frame
frame = simplegui.create_frame("Pong", WIDTH, HEIGHT)
frame.set_draw_handler(draw)

frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)
frame.add_button("New Game",new_game)


# start frame
new_game()
frame.start()

1 个答案:

答案 0 :(得分:0)

问题恰好在这两行

canvas.draw_line((0,paddle1_pos),(0, paddle1_pos*2),PAD_WIDTH,"White")
canvas.draw_line((WIDTH, paddle2_pos) ,(WIDTH, paddle2_pos*2),PAD_WIDTH,"White")

您在paddle1_pos*2paddle2_pos*2那里,所以上部以标准速度(0,paddle1_pos)下降,但是下部以两倍的速度(0, paddle1_pos*2)下降。

正确的方法是这样

canvas.draw_line((0,paddle1_pos),(0, paddle1_pos + 100),PAD_WIDTH,"White")
canvas.draw_line((WIDTH, paddle2_pos) ,(WIDTH, paddle2_pos + 100),PAD_WIDTH,"White")

因此paddle1_pos + 100paddle2_pos + 100,其中100表示您想要多高的拨片。