У меня есть это задание, где, когда я щелкаю мышью, шарик запускается и в итоге разрушает ящик.У меня проблемы с перемещением мяча с помощью мыши.Переменные, которые определены после «печати строки начального значения», должны быть этими значениями.Я не слишком знаком с 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()
А это класс Ball и класс Drawable
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