Reputation: 21
I have been making this pong game, but I can't figure out how to make the hits not register to the bat when it underneath it. Can someone help me fix it?
Also, if you move the bat back and forth you basically will hit every shot.
So can someone please help me fix both of those problems?
Code:
import pygame
import sys
import pygame.freetype
circle_x= 250
circle_y= 250
pygame.init()
pygame.mixer.init()
mp3=pygame.mixer.music.load("nana.mp3")
pygame.mixer.music.play()
font = pygame.freetype.Font("msyh.ttc")
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("game")
move_x = 2
move_y = 2
score = 0
deaths = 0
level = 1
minus1 = -1
minus2 = -1
plus1 = 1
plus2 = 1
minus3 = -1
while True:
if score >= 50:
level = 2
minus1 = -2
minus2 = -2
plus1 = 2
plus2 = 2
minus3 = -2
mouse_x,mouse_y = pygame.mouse.get_pos()
screen.fill((255,255,255))
pygame.draw.circle(screen,(100,100,100),(circle_x,circle_y),20)
pygame.draw.rect(screen,(255,0,0),(mouse_x,500,100,10))
circle_x = circle_x+move_x
circle_y = circle_y+move_y
font_image,font_rect = font.render('Score: '+str(score),fgcolor=(0,0,0),size=30)
font_image1,font_rect1 = font.render('Deaths: '+str(deaths),fgcolor=(0,0,0),size=30)
font_image2,font_rect2 = font.render('Level: '+str(level),fgcolor=(0,0,0),size=30)
screen.blit(font_image,(20,10))
screen.blit(font_image1,(600,10))
screen.blit(font_image2,(300,10))
if circle_x>800:
move_x = minus1
if circle_y>600:
score = 0
deaths += 1
move_y = -50
if circle_y<0:
move_y= plus1
if circle_x<0:
move_x = plus2
if circle_y >= 500 and mouse_x <= circle_x <= mouse_x+100:
if move_y > 0:
score += 5
move_y = -1
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
pygame.display.update()
Upvotes: 2
Views: 705
Reputation: 27557
It's actually super esy, barely an inconvenience. See this part of your code:
if circle_y >= 500 and mouse_x <= circle_x <= mouse_x+100:
if move_y > 0:
score += 5
move_y = -1
You make the ball bounce if the ball's y
coordinate is at or past the pedal's y
coordinates.
Instead, only make it bounce if the ball's y
coordinate is at the pedal's y
coordinate, including the width of the pedal.
Your pedal is 10
pixels wide, so add another condition 500 + 10 >= circle_y
:
if 510 >= circle_y >= 500 and mouse_x <= circle_x <= mouse_x+100:
if move_y > 0:
score += 5
move_y = -1
Upvotes: 3