Python 3.x 为什么圆在pygame中是直线而不是角度移动?

Python 3.x 为什么圆在pygame中是直线而不是角度移动?,python-3.x,Python 3.x,我在工作的空闲时间学习pygame的基础知识。我想将程序的底部边界向上移动,但当我更改边界碰撞条件时,球会沿直线而不是角度移动 当我在bounceCircle定义中删除条件的525部分时,它会按预期移动。当我把它放回去时,它会水平移动 导入pygame 导入系统 随机输入 输入数学 #初始化游戏引擎 pygame.init() #定义常用颜色: 白色=(255,255,255) 蓝色=(0,0255) #设置窗口大小、标题和背景色 (宽度、高度)=(900600) screen=pygame.d

我在工作的空闲时间学习pygame的基础知识。我想将程序的底部边界向上移动,但当我更改边界碰撞条件时,球会沿直线而不是角度移动

当我在bounceCircle定义中删除条件的525部分时,它会按预期移动。当我把它放回去时,它会水平移动

导入pygame
导入系统
随机输入
输入数学
#初始化游戏引擎
pygame.init()
#定义常用颜色:
白色=(255,255,255)
蓝色=(0,0255)
#设置窗口大小、标题和背景色
(宽度、高度)=(900600)
screen=pygame.display.set_模式((宽度、高度))
pygame.display.set_标题(“球场”)
屏幕填充(白色)
#用于管理屏幕更新的速度
clock=pygame.time.clock()
#球类
类粒子():
定义初始(自身、位置、半径):
self.x=位置[0]
self.y=位置[1]
自半径=半径
self.color=(蓝色)
#厚度=0表示已填充
#厚度>0较厚的边框
#厚度<0无
自身厚度=1
自身速度=0
自转角=0
#绘制圆的定义
def drawCircle(自):
pygame.draw.circle(屏幕,self.color,(int(self.x),int(self.y)),self.radius,self.thickness)
#移动圆的定义
def移动圆(自身):
self.x+=数学sin(self.angle)*self.speed
self.y-=数学cos(self.angle)*self.speed
#曲面反弹的定义
def bounceCircle(自身):
如果(self.x>width-self.radius)或(self.x高度-self.radius)或(self.y<525-self.radius):
self.angle=math.pi-self.angle
球=粒子((450300),40)
球速=2
ball.angle=随机均匀(0,数学pi*2)
#------主程序循环----------
尽管如此:
#---主事件循环
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
屏幕填充(白色)
#---博弈逻辑
ball.moveCircle()
ball.bounceCircle()
ball.drawCircle()
#---图纸
pygame.draw.line(屏幕,黑色,[0525],[900525],2)
#打印微小的对角线来标记表面
x1=0
x2=5
对于范围内的i(0,宽度):
pygame.draw.line(屏幕,黑色,[x1530],[x2525],2)
x1+=5
x2+=5
pygame.display.flip()
时钟滴答(60)

您需要编辑
bounceCircle
功能以:

    def bounceCircle(self):
        if (self.x + self.radius > width ) or (self.x - self.radius < 0):
            self.angle = - self.angle
        elif (self.y + self.radius > (525)) or (self.y - self.radius < 0):
            self.angle = math.pi - self.angle
def bounceCircle(自):
如果(self.x+self.radius>宽度)或(self.x-self.radius<0):
self.angle=-self.angle
elif(self.y+self.radius>(525))或(self.y-self.radius<0):
self.angle=math.pi-self.angle
整个代码(修复了一些bug):

导入pygame
导入系统
随机输入
输入数学
#初始化游戏引擎
pygame.init()
#定义常用颜色:
白色=(255,255,255)
蓝色=(0,0255)
#设置窗口大小、标题和背景色
(宽度、高度)=(900600)
screen=pygame.display.set_模式((宽度、高度))
pygame.display.set_标题(“球场”)
屏幕填充(白色)
#用于管理屏幕更新的速度
clock=pygame.time.clock()
#球类
类粒子():
定义初始(自身、位置、半径):
self.x=位置[0]
self.y=位置[1]
自半径=半径
self.color=(蓝色)
#厚度=0表示已填充
#厚度>0较厚的边框
#厚度<0无
自身厚度=1
自身速度=0
自转角=0
#绘制圆的定义
def drawCircle(自):
pygame.draw.circle(屏幕,self.color,(int(self.x),int(self.y)),self.radius,self.thickness)
#移动圆的定义
def移动圆(自身):
self.x+=数学sin(self.angle)*self.speed
self.y-=数学cos(self.angle)*self.speed
打印(self.angle、self.x、self.y)
#曲面反弹的定义
def bounceCircle(自身):
如果(self.x>width-self.radius)或(self.x(高度-100))或(self.y-self.radius<0):
self.angle=math.pi-self.angle
球=粒子((450300),40)
球速=20
ball.angle=随机均匀(0,数学pi*2)
打印(球角)
#------主程序循环----------
尽管如此:
#---主事件循环
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
屏幕填充(白色)
#---博弈逻辑
ball.moveCircle()
ball.bounceCircle()
ball.drawCircle()
#---图纸
pygame.draw.line(屏幕,蓝色,[0525],[900525],2)
#打印微小的对角线来标记表面
x1=0
x2=5
对于范围内的i(0,宽度):
pygame.draw.line(屏幕,蓝色,[x1530],[x2525],2)
x1+=5
x2+=5
pygame.display.flip()
时钟滴答(60)
import pygame
import sys
import random
import math

# Initalize the game engine
pygame.init()

# Define common colors:
WHITE = (255, 255, 255)

BLUE = (0, 0, 255)

# Set window size, title, and background color
(width, height) = (900, 600)

screen = pygame.display.set_mode((width, height))

pygame.display.set_caption("Ball Playground")

screen.fill(WHITE)

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

# Ball class
class Particles():

    def __init__(self, position, radius):
        self.x = position[0]
        self.y = position[1]
        self.radius = radius
        self.color = (BLUE)
        # thickness = 0 means filled
        # thickness > 0 thicker border
        # thickness < 0 nothing
        self.thickness = 1
        self.speed = 0
        self.angle = 0

    # Definition for drawing circle
    def drawCircle(self):
        pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius, self.thickness)

    # Definition for moving the circle
    def moveCircle(self):
        self.x += math.sin(self.angle) * self.speed
        self.y -= math.cos(self.angle) * self.speed
        print(self.angle, self.x, self.y)

    # Definition for bouncing off of surfaces
    def bounceCircle(self):
        if (self.x > width - self.radius) or (self.x < self.radius):
            self.angle = - self.angle
        elif (self.y + self.radius > (height-100)) or (self.y - self.radius < 0):
            self.angle = math.pi - self.angle

ball = Particles((450, 300), 40)

ball.speed = 20

ball.angle = random.uniform(0, math.pi*2)
print(ball.angle)
# --------- Main Program Loop ----------
while True:
    # --- Main Event Loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        pygame.display.update()

    screen.fill(WHITE)

    #--- Game Logic
    ball.moveCircle()
    ball.bounceCircle()
    ball.drawCircle()

    #--- Drawings
    pygame.draw.line(screen, BLUE, [0, 525], [900, 525], 2)

    # Prints tiny diaginal lines to mark surface
    x1 = 0
    x2 = 5
    for i in range(0, width):
        pygame.draw.line(screen, BLUE, [x1, 530], [x2, 525], 2)
        x1 += 5
        x2 += 5

    pygame.display.flip()
    clock.tick(60)