[英]Ball in pong style game not hitting paddles
并不是说它不起作用,而是它真的很受欢迎。 查找球是否在桨 x 内的代码可以完美运行,但 y 不能。 我认为它与桨或球的边缘有关,它从桨区域外进入桨,但请帮忙,或者至少不要对我大喊大叫,说我愚蠢,谢谢!
import pygame,sys,time,random
pygame.init()
width,height =1000,600
win = pygame.display.set_mode((width,height))
pygame.display.set_caption("Hello World")
p1 = 40
p1width = 100
p1score = 0
p2 = 560
p2width = 100
p2score = 0
speed = 4
ballvx=random.randint(1,2)
ballvy=random.randint(1,2)
ballx=300
bally=50
font = pygame.font.Font('CursedTimerUlil-Aznm.ttf', 100)
p1text = font.render(str(p1score), True,(255,255,255))
p1textRect = p1text.get_rect()
p1textRect.center = (width/2/2, 250)
p2text = font.render(str(p1score), True,(255,255,255))
p2textRect = p2text.get_rect()
p2textRect.center = (width/2+(width/2/2), 250)
while True:
win.fill((0,0,0))
#line
pygame.draw.rect(win, (40,40,40), pygame.Rect(width/2-2, 0, 4, 600))
#p1
pygame.draw.rect(win, (255,255,255), pygame.Rect(p1, 550, p1width, 10))
#p2
pygame.draw.rect(win, (255,255,255), pygame.Rect(p2, 550, p2width, 10))
#ball
pygame.draw.rect(win, (255,255,255), pygame.Rect(ballx, bally, 20, 20))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and p1-speed>=0:
p1-=speed
if keys[pygame.K_d] and p1+speed<=width/2-p1width-2:
p1+=speed
if keys[pygame.K_LEFT] and p2-speed>=width/2+2:
p2-=speed
if keys[pygame.K_RIGHT] and p2+speed<=width-p2width:
p2+=speed
ballx+=ballvx
bally+=ballvy
if ballx+ballvx>=width-20:
ballvx*= -1
ballx+=0.1
bally+=0.1
if ballx+ballvx<=0:
ballvx *= -1
ballx+=0.1
bally+=0.1
if bally+ballvy>=height-20:
print(width/2)
print(ballx)
if ballx <= width/2:
p2score+=1
print("score")
else:
p1score+=1
print("is this even running???")
bally = 50
ballx = random.randint(200,800)
ballvx=random.randint(1,2)
ballvy=random.randint(1,2)
if bally+ballvy<=0:
ballx+=0.1
ballvy*=-1
bally+=0.1
if ballx+ballvx in range(p1,p1+p1width) and bally+20 + ballvy >=550 :
ballvy*=-1
ballx+=0.1
bally+=0.1
if ballx+ballvx in range(p2,p2+p2width) and bally+20 + ballvy >=550:
ballvy*=-1
ballx+=0.1
bally+=0.1
time.sleep(0.01)
p1text = font.render(str(p1score), True,(255,255,255))
p1textRect = p1text.get_rect()
p1textRect.center = (width/2/2, 250)
win.blit(p1text, p1textRect)
p2text = font.render(str(p2score), True,(255,255,255))
p2textRect = p2text.get_rect()
p2textRect.center = (width/2+(width/2/2), 250)
win.blit(p2text,p2textRect)
pygame.display.update()
ballx+ballvx in range(p1,p1+p1width)
不符合您的预期。 range
function 产生一个整数值序列。 因此, x in range(a, b)
检查x
是否恰好是范围[a, b)
中的整数之一。 如果x
是浮点数,则它不是范围内的 integer。
例如: range(5, 8)
生成序列5, 6, 7
。 整数6
在这个序列中。 但是,浮点数6.5
不在此序列中。
如果要检查浮点数x
是否在[a, b]
范围内,则必须使用比较运算符(例如a <= x <= b
):
while True:
# [...]
if p1 <= ballx+ballvx <= p1+p1width and bally+20 + ballvy >=550:
ballvy*=-1
ballx+=0.1
bally+=0.1
if p2 <= ballx+ballvx <= p2+p2width and bally+20 + ballvy >=550:
ballvy*=-1
ballx+=0.1
bally+=0.1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.