Я пытаюсь воссоздать ballz, используя pygame для суммирования для моего класса ics.За исключением того, что я не знаю, как заставить шар (который является изображением) перемещаться туда, где пользователь нажимает.
Это для Pygame, я пытался обновить местоположение, за исключением того, что мяч просто мерцает в одном и том же месте.
def level_2():
class Ball(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self) #construct the parent component
self.image = pygame.image.load("ball.png").convert_alpha()
self.image.set_colorkey(self.image.get_at( (0,0) ))
self.rect = self.image.get_rect() #loads the rect from the image
#set the position, direction, and speed of the ball
self.rect.left = 300
self.rect.top = 600
self.speed = 4
self.dir = 0
def update(self):
click = pygame.mouse.get_pressed()
#Handle the walls by changing direction(s)
if self.rect.left < 0 or self.rect.right >= screen.get_width():
self.dir_x *= -1
if self.rect.top < 0:
self.dir_y *= -1
####CHECK TO SEE IF BALL HITS THE BLOCK AND WILL BOUNCE####
if pygame.sprite.groupcollide(ball_group, block_group, False, True):
self.rect.move_ip(self.speed*self.dir_x, self.speed*self.dir_y)
if self.rect.bottom >= screen.get_height():
speed = 0
self.dir_y = 0
self.dir_x = 0
self.rect.left = 300
self.rect.top = 600
self.rect.move_ip(self.speed*self.dir_x, self.speed*self.dir_y)
#Move the ball to where the user clicked
if ev.type == MOUSEBUTTONDOWN:
(x, y) = pygame.mouse.get_pos()
#ASK MS WUN HOW TO DO #
if self.rect.left != x and self.rect.top != y:
#self.rect.move_ip(x, y)
self.rect.move_ip(self.speed*self.dir_x, self.speed*self.dir_y)
Нет сообщений об ошибках,единственное, что происходит, это либо шар будет двигаться в заданном направлении (если пользователь нажимает на правую сторону, шар будет двигаться вправо, а если пользователь нажимает на левую, шар все равно будет двигаться вправо),
Или мяч будет просто мерцать в одном и том же месте