Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何让多个球在pygame中使用self和other随机移动?_Python_Pygame - Fatal编程技术网

Python 如何让多个球在pygame中使用self和other随机移动?

Python 如何让多个球在pygame中使用self和other随机移动?,python,pygame,Python,Pygame,好的,我对Stack Overflow和Python都是新手,我正在使用Pygame制作一个游戏,其中生成了一堆随机颜色的球,它们将随机移动,当它们碰撞时,它们将变得更大,并通过使用r g和b值的整数取颜色的“平均值”。我目前的问题是,我有球产卵,但我不能让球移动。我试图让它们在我的ball类中的更新函数中移动。我想让它们在移动时随机移动,而无需用户输入 这是我的主要任务 import pygame import random import ballClass WORLD_WIDTH = 80

好的,我对Stack Overflow和Python都是新手,我正在使用Pygame制作一个游戏,其中生成了一堆随机颜色的球,它们将随机移动,当它们碰撞时,它们将变得更大,并通过使用r g和b值的整数取颜色的“平均值”。我目前的问题是,我有球产卵,但我不能让球移动。我试图让它们在我的ball类中的更新函数中移动。我想让它们在移动时随机移动,而无需用户输入

这是我的主要任务

import pygame
import random
import ballClass

WORLD_WIDTH = 800
WORLD_HEIGHT = 600


WIN = pygame.display.set_mode((WORLD_WIDTH, WORLD_HEIGHT))

paused = False

def main():
    global numberOfBalls, WIN, paused

    pygame.init()

    clock = pygame.time.Clock()

    running = True
    WHITE = (255, 255, 255)

    #ballClass.Ball().initialize(10)
    

    while running:

        for event in pygame.event.get():
          
          if event.type == pygame.QUIT:
              running = False

          elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_1:
              paused = not paused

          if not paused:
            WIN.fill(WHITE)
            
            ballClass.Ball().draw(WIN)
            ballClass.Ball().update(WIN)
              #drawWorld(WIN)


        clock.tick(1)

        pygame.display.update()
    

def setInitialBallAttributes():
    pass

#def checkForCollision(self): #iterate through list of objects and check each one against , tell it to do averaging
#     ball1 = self.ball
#     ball2 = self.ball
#     ball3 = self.ball
#     ball4 = self.ball
#     ball5 = self.ball

#     balls = [ball1, ball2, ball3, ball4, ball5]

#     #from Mr. Sharick
    # for i in range(len(balls)):
    #     for j in range(i, len(balls)):  # start this loop at i+1 so you don't check collisions twice
    #         balls[i].collision(balls[j])



#def drawWorld(WIN, self):
#     ball1 = self.ball
#     ball2 = self.ball
#     ball3 = self.ball
#     ball4 = self.ball
#     ball5 = self.ball
  #pygame.draw.rect(WIN, 'black', width=0)
  #pass



if __name__ == "__main__":
     main()
这是我的棒球课

import pygame
import random
import math

balls = []

center1 = random.randint(0,600)
center2 = random.randint(0, 600)

balls.append((center1, center2))

class Ball():

    WORLD_WIDTH = 600
    WORLD_HEIGHT = 600

    def __init__(self, centerX=None, centerY=None, direction=None, r=None, g=None, b=None, radius= None, speed= None):
        self.centerX = 10
        self.centerY = 20
        self.r = r
        self.g = g
        self.b = b
        self.direction = direction  
        self.radius = radius
        self.speed = 3 #Set as default 3; change later

    
    def initialize(self, num):
      global balls
      for i in range(num):

        self.centerX = random.randint(0, 600)
        self.centerY = random.randint(0,600)

        balls.append((self.centerX, self.centerY))


    def draw(self, WIN):
        global ball, radius, centerX, centerY, r, g, b, balls
        self.radius = 10

        self.direction = random.randint(0, 600), random.randint(0, 600)
        self.speed = 3

        #self.centerX = random.randint(self.radius, 600 - self.radius)
        #self.centerY = random.randint(self.radius, 600 - self.radius)

        r = random.randint(0, 255)
        g = random.randint(0, 255)
        b = random.randint(0, 255)

        #self.ball = Ball(centerX, centerY, radius, WIN)
        # needs all 8 
        for ball in balls:
          
          pygame.draw.circle(WIN, ((r,g,b)), (ball[0],ball[1]),self.radius)

          print(ball)

        #balls.append((self.centerX, self.centerY))

    # def collision(self, other): #pass in another ball object as another parameter
    #     distance = math.sqrt((self.centerX - self.centerY)^2 + (other.centerX - other.centerY)^2)

    #     if distance < self.radius + other.radius:
    #         (self.r + other.r) / 2 == self.r
    #         (self.g + other.g) / 2 == self.g
    #         (self.b + other.b) / 2 == self.b
    #         if self.radius > other.radius:
    #             self.radius = other.radius + self.radius
    #             other.radius = 0

    #         if other.radius > self.radius:
    #             other.radius = other.radius + self.radius
    #             self.radius = 0

    #         else:
    #             #self.radius = other.radius + self.radius
    #             self.centerX = (other.centerX + self.centerX) / 2
    #             self.centerY = (other.centerY + self.centerY) / 2
    #             other.radius = 0

    def update(self, win):
        WORLD_WIDTH = 600
        WORLD_HEIGHT = 600
        
        # if len(balls):

        #   for i in range(len(balls)):

        #     balls[i] = (center1+10, center2 +10)

      newBallCoords
        
        

        # if self.right >= WORLD_WIDTH or ball.left <= 0:
        #     self.direction = -self.centerX, self.centerX

        # if self.bottom >= WORLD_HEIGHT or ball.top <= 0:
            #self.direction = self.centerX, -self.centerX
导入pygame
随机输入
输入数学
球=[]
center1=random.randint(0600)
center2=random.randint(0600)
球。附加((中心1,中心2))
类Ball():
世界宽度=600
世界高度=600
def u uu初始(self,centerX=None,centerY=None,direction=None,r=None,g=None,b=None,radius=None,speed=None):
self.centerX=10
self.centerY=20
self.r=r
self.g=g
self.b=b
方向
自半径=半径
self.speed=3#设为默认值3;改天
def初始化(self,num):
全球球
对于范围内的i(num):
self.centerX=random.randint(0600)
self.centerY=random.randint(0600)
balls.append((self.centerX,self.centerY))
def抽签(自我,赢):
全局球,半径,中心X,中心Y,r,g,b,球
自半径=10
self.direction=random.randint(0600),random.randint(0600)
自身速度=3
#self.centerX=random.randint(self.radius,600-self.radius)
#self.centerY=random.randint(self.radius,600-self.radius)
r=random.randint(0255)
g=random.randint(0255)
b=random.randint(0255)
#self.ball=球(centerX、centerY、radius、WIN)
#需要全部8个
对于球中球:
pygame.draw.circle(赢,((r,g,b)),(球[0],球[1]),自半径)
打印(球)
#balls.append((self.centerX,self.centerY))
#def碰撞(自身、其他):#将另一个球对象作为另一个参数传递
#距离=数学.sqrt((self.centerX-self.centerY)^2+(other.centerX-other.centerY)^2)
#如果距离<自身半径+其他半径:
#(self.r+other.r)/2==self.r
#(self.g+other.g)/2==self.g
#(self.b+other.b)/2==self.b
#如果self.radius>other.radius:
#self.radius=其他半径+self.radius
#其他半径=0
#如果other.radius>self.radius:
#other.radius=other.radius+self.radius
#自半径=0
#其他:
##self.radius=其他半径+self.radius
#self.centerX=(other.centerX+self.centerX)/2
#self.centerY=(other.centerY+self.centerY)/2
#其他半径=0
def更新(自我,赢):
世界宽度=600
世界高度=600
#如果是len(球):
#对于范围内的i(len(balls)):
#球[i]=(中心1+10,中心2+10)
纽巴尔库德

#如果self.right>=WORLD\u WIDTH或ball.left=WORLD\u HEIGHT或ball.top如果希望球以各自独立的方向移动,则每个球都应具有自己的X和Y速度,这些速度应在初始化时随机生成

看一下你所包括的程序,我建议你考虑把球类与控制所有球的逻辑分开。

我已经包含了你的程序的一个修改版本,其中包含了这个想法

主要内容:


在我提供的代码中,球的更新方法仅更新单个球,并使用其各自的速度,主程序告诉所有球需要更新。

可能缺少缩进,暂停期间仍在更新球。
import pygame
import random
import math
import ball as ballClass

WORLD_WIDTH = 800
WORLD_HEIGHT = 600

def main():
    paused = False
    WIN = pygame.display.set_mode((WORLD_WIDTH, WORLD_HEIGHT))

    pygame.init()

    clock = pygame.time.Clock()

    running = True
    WHITE = (255, 255, 255)

    balls = [ballClass.Ball() for _ in range(8)]

    while running:

        for event in pygame.event.get():
          
          if event.type == pygame.QUIT:
              running = False

          elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_1:
              paused = not paused

        if not paused:
            WIN.fill(WHITE)
            
        for ball in balls:
            ball.draw(WIN)
            ball.update(WIN)   
      
        clock.tick(1) 
        pygame.display.update()

if __name__ == "__main__":
     main()
import random, pygame

class Ball():

    WORLD_WIDTH = 600
    WORLD_HEIGHT = 600

    def __init__(self, centerX=None, centerY=None, direction=None, r=None, g=None, b=None, radius= None, speed= None):
        self.centerX = random.randint(0, 600)
        self.centerY = random.randint(0,600)
        self.r = random.randint(0, 255)
        self.g = random.randint(0, 255)
        self.b = random.randint(0, 255)
        self.velocityX = random.randint(-10, 10)
        self.velocityY = random.randint(-10, 10)
        self.radius = 10

    def draw(self, WIN):
        pygame.draw.circle(WIN, ((self.r,self.g,self.b)), (self.centerX, self.centerY), self.radius)


    def update(self, win):
        self.centerX += self.velocityX
        self.centerY += self.velocityY