Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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 用鼠标移动球的游戏_Python_Pygame - Fatal编程技术网

Python 用鼠标移动球的游戏

Python 用鼠标移动球的游戏,python,pygame,Python,Pygame,我有一个任务,当我点击鼠标时,一个球将被发射并最终摧毁一个盒子。我无法用鼠标点击移动球。“打印初始值行”之后定义的变量必须是这些值。我对pygame不太熟悉,也不知道我应该画在哪里,也不知道我是否应该在画一个新球之前把球移走 import pygame, sys from Drawable import * from Ball import * from Block import * from Text import * from pygame.locals import * pygame.i

我有一个任务,当我点击鼠标时,一个球将被发射并最终摧毁一个盒子。我无法用鼠标点击移动球。“打印初始值行”之后定义的变量必须是这些值。我对pygame不太熟悉,也不知道我应该画在哪里,也不知道我是否应该在画一个新球之前把球移走

import pygame, sys
from Drawable import *
from Ball import *
from Block import *
from Text import *
from pygame.locals import *

pygame.init()
surface = pygame.display.set_mode((500,500))
surface.fill((255,255,255))



class Line(Drawable):
    def __init__(self,x=0, y=0,color=(0,255,0)):
        super().__init__(x, y, color)
        self.position = x , y
        self.visible = False

    def draw(self):
        if self.visible == True:
            pygame.draw.line(surface,(0,0,0),(0,400),(500,400))
    def get_rect(self):
        pass



ground = Line()
ground.visible = True
ground.draw()
ball = Ball()
ball.visible = True
ball.draw(surface)
block = Block()
block.visible = True
block.draw(surface)
text = Text()
text.visible = True
text.draw(surface)

print("Initial Ball Location:", ball.position)

dt = 0.1
g = 6.67
R = 0.7
eta = 0.5

mouespos1 = 0
mousepos2 = 0

xv = 1
yv = 1

def mousedown():
    global mousepos1
    mousepos1 = pygame.mouse.get_pos()


def mouseup():
    global xv
    global yv
    mousepos2 = pygame.mouse.get_pos()
    xv = mousepos2[0] - mousepos1[0]
    print("XV in mouseup:", xv)
    yv = -1 * (mousepos2[1] - mousepos1[1])
    print("YV in mouesup:", yv)

def updateballpos():
    global xv, yv
    print("Ran Update")
    moveX = ball.x + (dt * xv)
    ball.moveX(moveX)
    moveY = ball.y - (dt * yv)
    ball.moveY(moveY)

    print("new x", ball.x)
    print("new y", ball.y)

    if ball.y > 400:
        yv = -R * yv
        xv = eta * xv
    else:
        yv = yv - g * dt
    ball.draw(surface)
    pygame.display.update()







while(True):

    for event in pygame.event.get():

        if (event.type == pygame.QUIT) or \
            (event.type == pygame.KEYDOWN and event.__dict__['key'] == pygame.K_q):
            pygame.quit()
            exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            mousedown()
        if event.type == pygame.MOUSEBUTTONUP:
            mouseup()
        print("xv in while", xv)
        print("yv in while", yv)

    if yv > 0 and xv > 0:
        updateballpos()



    pygame.display.update()
这是球类和拖拉类

import pygame
import abc
import random

class Drawable(metaclass = abc.ABCMeta):
    def __init__(self,x,y,color):
        self.x = x
        self.y = y
        self.color = color
        self.position = (self.x,self.y)
        self.visible = False

    def getLoc(self):
        return (self.x, self.y)

    def setLoc(self,p):
        self.x = p[0]
        self.y = p[1]

    def getColor(self):
        return self.__color

    def getX(self):
        return self.__x

    def getY(self):
        return self.__y

    @abc.abstractmethod
    def draw(self,surface):
        pass
    @abc.abstractmethod
    def get_rect(self):
        pass





from Drawable import *
import pygame, sys
from pygame.locals import *

class Ball(Drawable):
    def __init__(self, x=20, y=400,color=(0, 0,0)):
        super().__init__(x, y,color)
        self.x = x
        self.y = y
        self.position = (self.x,self.y)
        self.visible = False

    def draw(self,s):
        if self.visible == True:
            pygame.draw.circle(s,(255,0,0),(int(self.x), int(self.y)),8)

    def get_rect(self):
        pass

    def getLoc(self):
        return (self.x, self.y)

    def setLoc(self, x, y):
        self.x = x
        self.y = y

    def moveX(self, inc):
        self.x = self.x + inc

    def moveY(self, inc):
        self.y = self.y + inc

我建议添加
updateballpos
作为
Ball
的一种方法,因为它只更新Ball属性。
xv
yv
变量也应该是球的属性,然后你可以给每个球一个不同的速度

要生成新球,您只需创建
Ball
实例并将其附加到列表中,然后使用
for
循环更新并绘制此列表中的球。在绘制球之前,可以使用
fill
方法清除屏幕(或blit背景曲面)

对于弹弓效果,您可以存储
pygame.MOUSEMOTION
事件的
rel
属性(鼠标的相对运动,以像素为单位),在实例化它们时将其传递给球,并将其分配给
xv
yv
属性

下面是一个简单完整的示例:

import sys
import pygame


pygame.init()
screen = pygame.display.set_mode((500,500))


class Ball:

    # Pass the xv, yv as arguments as well.
    def __init__(self, x=20, y=400, xv=0, yv=0, color=(0, 0,0)):
        self.x = x
        self.y = y
        # Give the objects xv and yv attributes.
        self.xv = xv
        self.yv = yv
        self.position = (self.x,self.y)
        self.visible = False

    def draw(self,s):
        if self.visible == True:
            pygame.draw.circle(s,(255,0,0),(int(self.x), int(self.y)),8)

    def get_rect(self):
        pass

    def getLoc(self):
        return (self.x, self.y)

    def setLoc(self, x, y):
        self.x = x
        self.y = y

    def moveX(self, dt):
        self.x += dt * self.xv

    def moveY(self, dt):
        self.y += dt * self.yv

    # Add a method to update the position and other attributes.
    # Call it every frame.
    def update(self, dt):
        self.moveX(dt)
        self.moveY(dt)

        if self.y > 400:
            self.yv = -R * self.yv
            self.xv = eta * self.xv
        else:
            self.yv = self.yv - g * dt

dt = 0.1
g = 6.67
R = 0.7
eta = 0.5

balls = []
clock = pygame.time.Clock()
rel_x = 0
rel_y = 0

while True:
    for event in pygame.event.get():
        if (event.type == pygame.QUIT or
            event.type == pygame.KEYDOWN and event.key == pygame.K_q):
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONUP:
            # Create a new ball instance and append it to the list.
            # Pass the rel (the relative mouse movement) as well.
            ball = Ball(xv=rel_x*10, yv=rel_y*10)  # * 10 to make the balls faster.
            ball.visible = True
            balls.append(ball)
        if event.type == pygame.MOUSEMOTION:
            # event.rel is the relative movement of the mouse.
            rel_x = event.rel[0]
            rel_y = event.rel[1]

    # Call the update methods of all balls.
    for ball in balls:
        ball.update(dt)

    # Clear the screen with fill (or blit a background surface).
    screen.fill((255,255,255))
    # Draw the balls.
    for ball in balls:
        ball.draw(screen)

    pygame.display.update()
    dt = clock.tick(30) / 1000

你能解释一下当鼠标按键被按下然后被释放时会发生什么吗?是否要为每次鼠标单击创建一个新球?球是否应该沿随机方向移动?当按下鼠标按钮时,我只想存储鼠标点,然后一旦释放鼠标点,我希望它沿用户在释放鼠标之前移动鼠标的方向移动。几乎像弹弓一样的效果。我已经编辑了答案。最好在问题中添加您的评论。感谢您抽出时间回答我的问题。这花了我一段时间,但我得到了足够好的工作提交它。再次非常感谢!:)