[英]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.