Reputation: 41
I am currently trying to make a pong game, but I am currently struggling on a part of it. I need to make the ball bounce if the edge of the screen, but for some reason, it keeps going forward and does not detect a collision. Here is my code:
import sys, pygame, random
pygame.init()
width, height = 1200, 675
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
clock = pygame.time.Clock()
FPS = 120
xmb1 = False
xmf1 = False
ymb1 = False
ymf1 = False
xmb2 = False
xmf2 = False
ymb2 = False
ymf2 = False
squareh = 275
squarew = 35
squares = 3
x1 = 75
y1 = (height / 2) - (squareh / 2)
x2 = width - 75 - squarew
y2 = (height / 2) - (squareh / 2)
BLACK = (0,0,0)
WHITE = (255,255,255)
font = pygame.font.SysFont("arial", 30)
text = font.render("Press Space to start", True, WHITE)
text3 = font.render("3", True, WHITE)
text2 = font.render("2", True, WHITE)
text1 = font.render("1", True, WHITE)
startt = font.render("Start!", True, WHITE)
text3b = False
text2b = False
text1b = False
starttb = False
start = False
startballmove = False
bx = width / 2
by = height / 2
br = 40
bxm = random.randint(-6, 6)
bym = random.randint(-6, 6)
btc = by
blc = bx
bbc = by + br + br
brc = bx + br + br
circle=pygame.Surface((br * 2, br * 2))
circle.fill((0, 0, 0))
pygame.draw.circle(circle, WHITE , (br, br), br, 0)
circle.set_colorkey((0, 0, 0))
while 1:
if start and not text1b and not text2b and not text3b and not starttb and not startballmove:
text3b = True
pygame.time.set_timer(pygame.USEREVENT, 1000)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
# pad 2 check if key is down
if event.key == pygame.K_UP:
ymb2 = True
if event.key == pygame.K_DOWN:
ymf2 = True
# pad 1 check if key is down
if event.key == pygame.K_w:
ymb1 = True
if event.key == pygame.K_s:
ymf1 = True
if event.key == pygame.K_SPACE:
start = True
elif event.type == pygame.KEYUP:
#pad 2 check if key goes up
if event.key == pygame.K_UP:
ymb2 = False
if event.key == pygame.K_DOWN:
ymf2 = False
# pad 1 check if key goes up
if event.key == pygame.K_w:
ymb1 = False
if event.key == pygame.K_s:
ymf1 = False
# check if window has been resized
if event.type == pygame.VIDEORESIZE:
width = event.dict['size'][0]
height = event.dict['size'][1]
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
if event.type == pygame.USEREVENT:
# check if start should be hidden
if starttb:
starttb = False
startballmove = True
# check if start should be showed
if text1b and not text2b and not text3b:
text1b = False
text2b = False
text3b = False
starttb = True
# check if 1 should be showed
if text2b and not text3b and not text1b:
text3b = False
text2b = False
text1b = True
# check if 2 should be showed
if text3b and not text2b and not text1b:
text3b = False
text2b = True
text1b = False
# check if pad 1 is out of bounds and move it
if ymb1 and not (y1 <= 0): y1 -= squares
if ymf1 and not (y1 + squareh >= height): y1 += squares
if y1 > (height - squareh) + 1: y1 -= squares
# check if pad 2 is out of bounds and move it
if ymb2 and not (y2 <= 0): y2 -= squares
if ymf2 and not (y2 + squareh >= height): y2 += squares
if y2 > (height - squareh) + 1: y2 -= squares
# put pads in center if game has not started
if not start:
# pad 1
x1 = 75
y1 = (height / 2) - (squareh / 2)
# pad 2
x2 = width - 75 - squarew
y2 = (height / 2) - (squareh / 2)
#ball
bx = width / 2 - br
by = height / 2 - br
# put pads in center in x if game has started
else:
# pad 1
x1 = 75
# pad 2
x2 = width - 75 - squarew
# if ball has not started moving center it
if not startballmove:
bx = width / 2 - br
by = height / 2 - br
# check if movement variables are 0
while bxm == 0 or bym == 0:
if bxm == 0:
bxm = random.randint(-6, 6)
if bym == 0:
bym = random.randint(-6, 6)
screen.fill(BLACK)
# draw starting text if game has not started
if not start:
screen.blit(text,((width / 2) - text.get_width() // 2, (height / 4) - text.get_height() // 2))
# put 3 on screen
if start and text3b:
screen.blit(text3,((width / 2) - 15, (height / 4) - (text.get_height() / 2)))
# put 2 on screen
if start and text2b:
screen.blit(text2,((width / 2) - 15, (height / 4) - (text.get_height() / 2)))
# put 1 on screen
if start and text1b:
screen.blit(text1,((width / 2) - 15, (height / 4) - (text.get_height() / 2)))
# put start on screen
if start and starttb:
screen.blit(startt,((width / 2) - (text.get_width() / 8), (height / 4) - (text.get_height() / 2)))
# check if ball is out of bounds
if start and startballmove:
if btc <= 0:
by = by * -1
print("top side")
if bbc >= height:
by = by * -1
print("bottom side")
if blc <= 0:
bx = bx * -1
print("left side")
if brc >= width:
bx = bx * -1
print("right side")
# move ball
if start and startballmove:
bx += bxm
by += bym
# draw circle if game start
if start:
screen.blit(circle, (int(bx), int(by)))
# draw pad 1
pygame.draw.rect(screen, WHITE, (x1, y1, squarew, squareh), 0)
# draw pad 2
pygame.draw.rect(screen, WHITE, (x2, y2, squarew, squareh), 0)
pygame.display.flip()
clock.tick(FPS)
Does anyone know why this is not working? I have just recently learned pygame.
Upvotes: 0
Views: 486
Reputation: 59436
The variables you check for finding out whether your "ball is out of bounds" (so the comment, thanks for that!) are never changed. E. g. btc
is assigned once in the beginning outside of the loop and then never again.
I guess you should change them whenever by
(etc.) changes.
This is not a good coding style, sure. But trying to fix that would open a whole can of worms ;-)
EDIT: Furthermore you should negate the speed variables instead of the position variables, i. e. bym
instead of by
: bym = bym * -1
. After fixing these two issues, my ball was constantly bouncing off all edges in the window. The pedals were of no influence yet, though:
# check if ball is out of bounds
btc = by
blc = bx
bbc = by + br + br
brc = bx + br + br
if start and startballmove:
if btc <= 0:
bym = bym * -1
print("top side")
if bbc >= height:
bym = bym * -1
print("bottom side")
if blc <= 0:
bxm = bxm * -1
print("left side")
if brc >= width:
bxm = bxm * -1
print("right side")
Upvotes: 1