如何让小球撞到砖块后反弹回来?
How to make the ball bounce back after hitting the brick?
我正在制作 Breakout 的克隆版。我已经制作了球和砖块,让球撞击砖块,但是当球和块碰撞时,球穿过而不是弹回。我应该怎么办?抱歉没有评论,我对此很陌生。
import pygame
from pygame.locals import QUIT
from pygame.locals import Rect
import math
pygame.init()
SURFACE = pygame.display.set_mode((400,300))
pygame.display.set_caption("Game Window")
FPSCLOCK = pygame.time.Clock()
class Block:
def __init__(self, color, rect):
self.color = color
self.rect = rect
def draw(self):
pygame.draw.rect(SURFACE, self.color, self.rect)
def delete(self):
blocks.remove(block)
class Ball:
def __init__(self,a,b,c,d):
self.color=a
self.rect=b
self.dir=c
self.speed=d
def draw(self):
pygame.draw.ellipse(SURFACE, ball_color, ball_rect)
def move(self):
ball_rect.centerx += math.cos(math.radians(self.dir))*self.speed
ball_rect.centery -= math.sin(math.radians(self.dir))*self.speed
if ball_rect.centerx <=0 or ball_rect.centerx >= 400:
self.dir = 180 - self.dir
if ball_rect.centery <= 0 or ball_rect.centery >= 300:
self.dir = -self.dir
left=6
top=30
width=45
height=20
color=(200,50,200)
blocks = []
for i in range(4):
for j in range(8):
rect = Rect(left, top, width, height)
blocks.append(Block(color, rect))
left += 49
left = 6
top += 24
ball_rect = Rect(150,100,10,10)
ball_color = (255,255,0)
ball = Ball(ball_color, ball_rect, 60, 5)
num=0
while True:
SURFACE.fill((0,0,0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
for block in blocks:
block.draw()
ball.draw()
ball.move()
for block in blocks:
if ball.rect.colliderect(block.rect)==True:
Block.delete(blocks)
print("{}回目の衝突".format(num))
num += 1
pygame.display.update()
FPSCLOCK.tick(60)
如果你想让小球弹起来,那就加个新方法-
def bounce(self):
self.dir -= 180
它会将球反弹回来。注意:这可能会出现故障,并且可能不会朝着正确的方向发展。尝试改变角度并添加条件以使其完美。
然后添加这个-
if ball.rect.colliderect(block.rect)==True:
Block.delete(blocks)
ball.bounce()
print("{}回目の衝突".format(num))
num += 1
我正在制作 Breakout 的克隆版。我已经制作了球和砖块,让球撞击砖块,但是当球和块碰撞时,球穿过而不是弹回。我应该怎么办?抱歉没有评论,我对此很陌生。
import pygame
from pygame.locals import QUIT
from pygame.locals import Rect
import math
pygame.init()
SURFACE = pygame.display.set_mode((400,300))
pygame.display.set_caption("Game Window")
FPSCLOCK = pygame.time.Clock()
class Block:
def __init__(self, color, rect):
self.color = color
self.rect = rect
def draw(self):
pygame.draw.rect(SURFACE, self.color, self.rect)
def delete(self):
blocks.remove(block)
class Ball:
def __init__(self,a,b,c,d):
self.color=a
self.rect=b
self.dir=c
self.speed=d
def draw(self):
pygame.draw.ellipse(SURFACE, ball_color, ball_rect)
def move(self):
ball_rect.centerx += math.cos(math.radians(self.dir))*self.speed
ball_rect.centery -= math.sin(math.radians(self.dir))*self.speed
if ball_rect.centerx <=0 or ball_rect.centerx >= 400:
self.dir = 180 - self.dir
if ball_rect.centery <= 0 or ball_rect.centery >= 300:
self.dir = -self.dir
left=6
top=30
width=45
height=20
color=(200,50,200)
blocks = []
for i in range(4):
for j in range(8):
rect = Rect(left, top, width, height)
blocks.append(Block(color, rect))
left += 49
left = 6
top += 24
ball_rect = Rect(150,100,10,10)
ball_color = (255,255,0)
ball = Ball(ball_color, ball_rect, 60, 5)
num=0
while True:
SURFACE.fill((0,0,0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
for block in blocks:
block.draw()
ball.draw()
ball.move()
for block in blocks:
if ball.rect.colliderect(block.rect)==True:
Block.delete(blocks)
print("{}回目の衝突".format(num))
num += 1
pygame.display.update()
FPSCLOCK.tick(60)
如果你想让小球弹起来,那就加个新方法-
def bounce(self):
self.dir -= 180
它会将球反弹回来。注意:这可能会出现故障,并且可能不会朝着正确的方向发展。尝试改变角度并添加条件以使其完美。
然后添加这个-
if ball.rect.colliderect(block.rect)==True:
Block.delete(blocks)
ball.bounce()
print("{}回目の衝突".format(num))
num += 1