[英]Ball in pong style game not hitting paddles
Its not that it just doesn't work, but its really hit or miss.并不是说它不起作用,而是它真的很受欢迎。 The code for finding if the ball is within the paddle x works Perfectly, but they y does not.
查找球是否在桨 x 内的代码可以完美运行,但 y 不能。 I think it has something to do with the edges of the paddles or ball, and it passing into the paddle from outside of the paddle area, but please help, or at least not yell at me and call me stupid, thanks!
我认为它与桨或球的边缘有关,它从桨区域外进入桨,但请帮忙,或者至少不要对我大喊大叫,说我愚蠢,谢谢!
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)
does not do what you expect. ballx+ballvx in range(p1,p1+p1width)
不符合您的预期。 The range
function produces a sequence of integral values. range
function 产生一个整数值序列。 Therefore, x in range(a, b)
checks if x
is exactly one of the integers in the range [a, b)
.因此,
x in range(a, b)
检查x
是否恰好是范围[a, b)
中的整数之一。 If x
is a floating point number, it is not an integer in the range.如果
x
是浮点数,则它不是范围内的 integer。
eg: range(5, 8)
generates the sequence 5, 6, 7
.例如:
range(5, 8)
生成序列5, 6, 7
。 The integral number 6
is in this sequence.整数
6
在这个序列中。 However, the floating point number 6.5
is not in this sequence.但是,浮点数
6.5
不在此序列中。
If you want to check whether a floating point number x
is in a range [a, b]
, you must use the comparison operators (eg a <= x <= b
):如果要检查浮点数
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.