如何在pygame中使球移动而不是拉伸

如何在pygame中使球移动而不是拉伸

本文介绍了如何在pygame中使球移动而不是拉伸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个乒乓球游戏,试图提高编程水平,但我无法移动球.当调用 move_right 方法时,椭圆会向右拉伸而不是向右移动.我已经尝试将球变量放在 init 方法中,但这只会使它根本不动,即使变量应该因 move_right 方法而改变.我还尝试将 x 和 y 位置设置为 Ball 类中的参数,但这也只是拉伸了它.我不明白为什么当我运行以下代码时,我试图移动的球会向右伸展而不是向右移动.有人可以解释为什么会这样吗?我已经尝试了我能想到的一切,但我无法让它做我想做的事.

I'm building a pong game trying to get better at programming but Im having trouble moving the ball. When the move_right method is called the ellipse stretches to the right instead of moving to the right. I've tried putting the ball variable in the init method but that just makes it not move at all even though the variables should be changing on account of the move_right method. I have also tried setting the x and y positions as parameters in the Ball class,but that just stretches it also.I don't understand why when I run the following code the ball I'm trying to move stretches to the right instead of moves to the right. Can someone explain why this is happening? I have tried everything I can think of but i can't get it to do what I want.

import pygame,sys
import random


class Ball:
    def __init__(self):
        self.size = 30
        self.color = light_grey
        self.x_pos = width/2 -15
        self.y_pos = height/2 -15
        self.speed = 1
        #self.ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)


    def draw_ball(self):
        ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
        pygame.draw.ellipse(screen,self.color,ball)

    def move_right(self):
        self.x_pos += self.speed

class Player:
    def __init__(self,x_pos,y_pos,width,height):
        self.x_pos = x_pos
        self.y_pos = y_pos
        self.width = width
        self.height = height
        self.color = light_grey

    def draw_player(self):
        player = pygame.Rect(self.x_pos,self.y_pos,self.width,self.height)
        pygame.draw.rect(screen,self.color,player)



class Main:
    def __init__(self):
        self.ball=Ball()
        self.player=Player(width-20,height/2 -70,10,140)
        self.opponent= Player(10,height/2-70,10,140)

    def draw_elements(self):
        self.ball.draw_ball()
        self.player.draw_player()
        self.opponent.draw_player()

    def move_ball(self):
        self.ball.move_right()





pygame.init()
size = 30
clock = pygame.time.Clock()
pygame.display.set_caption("Pong")
width = 1000
height = 600
screen = pygame.display.set_mode((width,height))
bg_color = pygame.Color('grey12')
light_grey = (200,200,200)

main = Main()
#ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
#player = pygame.Rect(width-20,height/2 -70,10,140)
#opponent = pygame.Rect(10,height/2-70,10,140)


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    #ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
    #pygame.draw.rect(screen,light_grey,player)
    #pygame.draw.rect(screen,light_grey,opponent)
    #pygame.draw.ellipse(screen,light_grey,ball)
    main.draw_elements()
    main.move_ball()
    main.ball.x_pos += main.ball.speed
    pygame.display.flip()
    clock.tick(60)

推荐答案

您必须使用 pygame.Surface.fill:

You have to clear the display in every frame with pygame.Surface.fill:

while True:
    # [...]

    screen.fill(0) # <---

    main.draw_elements()
    main.move_ball()
    main.ball.x_pos += main.ball.speed
    pygame.display.flip()

    # [...]

绘制的所有内容都绘制在目标表面上.整个场景在每一帧中重新绘制.因此,需要在应用程序循环中的每一帧开始时清除显示.典型的 PyGame 应用程序循环必须:

Everything that is drawn is drawn on the target surface. The entire scene is redraw in each frame. Therefore the display needs to be cleared at the begin of every frame in the application loop. The typical PyGame application loop has to:

这篇关于如何在pygame中使球移动而不是拉伸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:13