video as you can see there the bullets arent appending at my player paddle x or why I am not sure what the problem is my code lets me shoot at the player x and y
for shootss in shootsright:
shootss.x += shootss.xspeed
shootss.y += shootss.yspeed
if shootss.x > 700 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0:
shootsright.pop(shootsright.index(shootss))
if len(shootsright) < 1:
for enemyshoot in enemyshooting:
BULLET_SPEED = 10
start_x = round(enemyshoot.x+enemyshoot.width+-35)
start_y = round(enemyshoot.y + enemyshoot.height+-25)
target_x = player1.x+player1.width//2
target_y = player1.y+player1.width//2
delta_x, delta_y = target_x - start_x, target_y - start_y
distance = math.sqrt(delta_x ** 2 + delta_y ** 2)
dir_x = BULLET_SPEED * delta_x / distance
dir_y = BULLET_SPEED * delta_y / distance
distance = math.sqrt(dir_x**2 + dir_y**2)
if distance > 0:
shootsright.append(enemyboolss(start_x,start_y,(0,0,0),dir_x, dir_y))
its class
# this part is the enemyshooting at the player
class enemyshoot:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.health = 10
self.hitbox = (self.x + -20, self.y + 30, 5, 5)
#-------------------------------------------------------
# Make a Reference Copy of the bitmap for later rotation
self.shootsright = pygame.image.load("dsds.png")
self.shootsright = pygame.transform.smoothscale(self.shootsright, (100,100))
self.image = self.shootsright
self.rect = self.image.get_rect(center = (self.x, self.y))
self.look_at_pos = (self.x, self.y)
self.hitbox = (self.x + 20, self.y, 18,10)
self.isLookingAtPlayer = False
self.look_at_pos = (x,y)
def draw(self):
self.rect.topleft = (self.x,self.y)
self.rect = self.shootsright.get_rect(center = (self.x, self.y))
dx = self.look_at_pos[0] - self.rect.centerx
dy = self.look_at_pos[1] - self.rect.centery
angle = (180/math.pi) * math.atan2(dx, dy)
self.image = pygame.transform.rotate(self.shootsright, angle)
self.rect = self.image.get_rect(center = self.rect.center)
window.blit(self.image, self.rect)
self.hitbox = (self.x +-60 , self.y + 5, 38,10)
pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 60, 100, 10)) # NEW
pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 60, 100 - (5 * (10 - self.health)), 10))
def lookAt( self, coordinate ):
self.look_at_pos = coordinate
enemyshoot1 = enemyshoot(300,300,10,10,white)
enemyshooting = [enemyshoot1]
my player class
class Player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.speed = Speed(0,5)
self.rect = pygame.Rect(x,y,height,width)
self.hits = (self.x + 20, self.y, 28,60)
self.health = 10
# the hitbox our projectiles will be colliding with
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
my full code
import pygame
import random
import math
WIDTH, HEIGHT = 600, 500
pygame.init()
window = pygame.display.set_mode((WIDTH,HEIGHT))
# title
pygame.display.set_caption("pong")
def quitlol():
pygame.quit()
quit()
#---------------------------------------------------
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(window, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(window, ic,(x,y,w,h))
smallText = pygame.font.SysFont("lolfont.ttf",60)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
window.blit(textSurf, textRect)
#------------------------------------------------------
def text_objects(text, font):
black = (0,0,0)
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def options_menu():
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
option = True
while option:
for event in pygame.event.get():
if event.type == pygame.QUIT:
option = False
window.fill((0,0,0))
button("Mute Music",220,270,190,40,green,bright_green,)
button("Collisions Mute",220,340,190,40,green,bright_green)
button("Disable Snow",220,200,190,40,green,bright_green)
def game_intro():
snow_list=[]
no_of_circles=100;
clock = pygame.time.Clock()
FPS = 60
clock.tick(FPS)
for i in range(no_of_circles):
x = random.randrange(0, 600)
y = random.randrange(0, 500)
snow_list.append([x,y])
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
fps = 460
clock = pygame.time.Clock()
intro = True
while intro:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
intro = False
pygame.quit()
window.fill((0,0,0))
for point in snow_list:
point[1]+=1
pygame.draw.circle(window, (255,255,255), point, 2)
if(point[1] >= 600):
point[0] = random.randrange(0, 600)
point[1] = random.randrange(-10, -5)
clock.tick(FPS)
font = pygame.font.Font("Candarali.ttf", 60)
loltext = font.render("CrimSon Syndrome", True,(255,255,255))
lolrect = loltext.get_rect()
lolrect.center = ((300,100))
window.blit(loltext,lolrect)
font = pygame.font.Font("lolfont.ttf", 40)
loltext = font.render("Classic Pong Game", True,(255,255,255))
lolrect = loltext.get_rect()
lolrect.center = ((300,160))
window.blit(loltext,lolrect)
font = pygame.font.Font("lolfont.ttf", 40)
loltext = font.render("By:Habib I.", True,(255,255,255))
lolrect = loltext.get_rect()
lolrect.center = ((300,460))
window.blit(loltext,lolrect)
button("Quit Game",220,270,190,40,green,bright_green,quitlol)
button("Options",220,340,190,40,green,bright_green,options_menu)
button("2 Players",220,200,190,40,green,bright_green,game_loop)
# ---------------------------------------------------------------------
pygame.display.update()
def game_loop():
class Speed:
def __init__(self,x,y):
self.x = x
self.y = y
#-------------------------------- enemy shoots left and right
class Player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.speed = Speed(0,5)
self.rect = pygame.Rect(x,y,height,width)
self.hits = (self.x + 20, self.y, 28,60)
self.health = 10
# the hitbox our projectiles will be colliding with
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
def pad_top(self):
return pygame.Rect(self.rect.x,self.rect.y,
self.rect.width,self.rect.height//4)
def pad_middle(self):
return pygame.Rect(self.rect.x,self.rect.y+rect.height//4,
self.rect.width,self.rect.height//2)
def pad_bottom(self):
return pygame.Rect(self.rect.x,self.rect.y+3*self.rect.height//4,
self.rect.width,self.rect.height//4)
def up(self):
if self.rect.y < self.speed.y:
return
self.rect.move_ip(0, -self.speed.y)
def down(self):
if self.rect.y + self.speed.y > WIDTH:
return
self.rect.move_ip(0, self.speed.y)
def draw(self):
pygame.draw.rect(window,self.color,self.rect)
class Ball:
def __init__(self,x,y,height,width,color):
self.color = color
self.speed = Speed(5,5)
self.x = x
self.y = y
self.height = height
self.width = width
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
pygame.draw.circle(window, self.color, self.rect.center,10)
def move(self):
nextx = self.rect.x + self.speed.x
nexty = self.rect.y + self.speed.y
if nextx <= 0 or nextx >= WIDTH: self.speed.x *= -1
if nexty <= 0 or nexty >= HEIGHT: self.speed.y *= -1
self.rect.move_ip(self.speed.x,self.speed.y)
white = (255,255,255)
player1 = Player(30,150,10,120,white)
player2 = Player(570,150,10,120,white)
ball1 = Ball(290,200,20,20,white)
FPS = 60
# enemys bullets
class enemyboolss(object):
def __init__(self, x, y,color, xspeed, yspeed):
self.x = x
self.y = y
self.xspeed = xspeed
self.yspeed = yspeed
self.ksud = pygame.image.load("BOM.png")
self.hitbox = self.ksud.get_rect()
self.rect = self.ksud.get_rect()
self.rect.topleft = (self.x,self.y)
self.ksud = pygame.transform.scale(self.ksud,(self.ksud.get_width()//25,self.ksud.get_height()//25))
self.color = color
self.hitbox = (self.x + 57, self.y + 33, 29, 52) # NEW
def draw(self, window):
self.rect.topleft = (self.x,self.y)
player_rect = self.ksud.get_rect(center = self.rect.center)
player_rect.centerx += 0 # 10 is just an example
player_rect.centery += 0 # 15 is just an example
self.hitbox = (self.x + 10, self.y + 10, 15, 15) # NEW
window.blit(self.ksud,self.rect)
# the rect in the middile
class tinyrect:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
tiny1 = tinyrect(300,0,1,500,white)
#------------------------------------
# collision things
# fps class
clock = pygame.time.Clock()
pygame.event.get()
# ReDraw The WIndow
# -----------------------------------------------------------------------------------------------
# score 1 for player 1
font = pygame.font.Font("lolfont.ttf", 49)
score = 0
loltext = font.render("Score: "+str(score), True,(255,255,255))
lolrect = loltext.get_rect()
lolrect.center = ((200,100))
# score for player 2
font = pygame.font.Font("Candarai.ttf", 49)
player2score = 0
text = font.render("Score: "+str(player2score), True,(255,255,255))
rects = text.get_rect()
rects.center = ((400,100))
#-------------------------------- enemy shoots left and right
class health:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.health = 10
self.rect = pygame.Rect(x,y,height,width)
self.hitbox = (self.x + -20, self.y + 30, 5, 5)
self.hitbox = (self.x + 20, self.y, 18,10)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
self.hitbox = (self.x +-60 , self.y + 5, 38,10)
pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 60, 150, 15)) # NEW
pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 60, 150 - (5 * (10 - self.health)), 15))
enemy1 = health(200,100,0,0,white)
enemy2 = health(390,100,0,0,white)
healthing = [enemy1,enemy2]
# this part is the enemyshooting at the player
class enemyshoot:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.health = 10
self.hitbox = (self.x + -20, self.y + 30, 5, 5)
#-------------------------------------------------------
# Make a Reference Copy of the bitmap for later rotation
self.hitbox = (self.x + 20, self.y, 18,10)
self.isLookingAtPlayer = False
self.look_at_pos = (x,y)
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
self.hitbox = (self.x +-60 , self.y + 5, 38,10)
pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 60, 100, 10)) # NEW
pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 60, 100 - (5 * (10 - self.health)), 10))
enemyshoot1 = enemyshoot(300,300,10,10,white)
enemyshooting = [enemyshoot1]
#-------------------------------- enemy shoots left and right
class collisions:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.health = 10
self.rect = pygame.Rect(x,y,height,width)
self.hitbox = (self.x + -20, self.y + 30, 5, 5)
self.hitbox = (self.x + 20, self.y, 18,10)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
collision1 = collisions(3,0,5,1225,white)
collision2 = collisions(599,0,5,1225,white)
collisionss = [collision1,collision2]
#-------------------------------- enemy shoots left and right
class enemyshoot:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.health = 10
self.hitbox = (self.x + -20, self.y + 30, 5, 5)
#-------------------------------------------------------
# Make a Reference Copy of the bitmap for later rotation
self.hitbox = (self.x + 20, self.y, 18,10)
self.isLookingAtPlayer = False
self.look_at_pos = (x,y)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
dx = self.look_at_pos[0] - self.rect.centerx
dy = self.look_at_pos[1] - self.rect.centery
angle = (180/math.pi) * math.atan2(dx, dy)
self.image = pygame.transform.rotate(self.shootsright, angle)
self.rect = self.image.get_rect(center = self.rect.center)
window.blit(self.image, self.rect)
self.hitbox = (self.x +-60 , self.y + 5, 38,10)
pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 60, 100, 10)) # NEW
pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 60, 100 - (5 * (10 - self.health)), 10))
enemyshoot1 = enemyshoot(300,300,10,10,white)
# -----------------------------------------------------------------------------------------------
def ReDrawWindow():
for collisions in collisionss:
collisions.draw()
window.fill((0,0,0))
player2.draw()
player1.draw()
ball1.draw()
tiny1.draw()
window.blit(loltext,lolrect)
window.blit(text,rects)
for bull in bulls:
bull.draw(window)
for health in healthing:
health.draw()
for shootss in shootsright:
shootss.draw(window)
for enemyshoot in enemyshooting:
enemyshoot.draw()
snow_list=[]
no_of_circles=100;
clock.tick(FPS)
for i in range(no_of_circles):
x = random.randrange(0, 600)
y = random.randrange(0, 500)
snow_list.append([x,y])
clock = pygame.time.Clock()
#Initialize values for color (RGB format)
WHITE=(255,255,255)
RED=(255,0,0)
GREEN=(0,255,0)
BLUE=(0,0,255)
BLACK=(0,0,0)
# score lol
bulls = []
shootsright = []
playing = True
while playing:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
playing = False
break;
for shootss in shootsright:
shootss.x += shootss.xspeed
shootss.y += shootss.yspeed
if shootss.x > 700 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0:
shootsright.pop(shootsright.index(shootss))
if len(shootsright) < 1:
for enemyshoot in enemyshooting:
BULLET_SPEED = 10
start_x = round(enemyshoot.x+enemyshoot.width+-35)
start_y = round(enemyshoot.y + enemyshoot.height+-25)
target_x = player1.x+player1.width//2
target_y = player1.y+player1.width//2
delta_x, delta_y = target_x - start_x, target_y - start_y
distance = math.sqrt(delta_x ** 2 + delta_y ** 2)
dir_x = BULLET_SPEED * delta_x / distance
dir_y = BULLET_SPEED * delta_y / distance
distance = math.sqrt(dir_x**2 + dir_y**2)
if distance > 0:
shootsright.append(enemyboolss(start_x,start_y,(0,0,0),dir_x, dir_y))
for point in snow_list:
point[1]+=1
pygame.draw.circle(window, (255,255,255), point, 2)
if(point[1] >= 600):
point[0] = random.randrange(0, 600)
point[1] = random.randrange(-10, -5)
pygame.display.flip()
clock.tick(FPS)
ball1.move()
if ball1.rect.colliderect(collision1.rect):
if enemy1.health > -22:
enemy1.health -= 5
ball1.speed.y += 5
ball1 = Ball(290,200,20,20,white)
player2score += 1
text = font.render("Score:" + str(player2score), True, (255,255,255))
rects.center = ((400,100))
font1 = pygame.font.SysFont('comicsans', 50)
detexto = font1.render('~Player One Has Scored!!!~', 1, (255,0,0))
window.blit(detexto, (290 - (detexto.get_width()/2),200))
pygame.display.update()
i = 0
while i < 300:
pygame.time.delay(10)
i += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
i = 301
pygame.quit()
if ball1.rect.colliderect(collision2.rect):
if enemy2.health > -22:
enemy2.health -= 5
ball1.speed.y += 5
ball1 = Ball(290,200,20,20,white)
player2score += 1
loltext = font.render("Score:" + str(player2score), True, (255,255,255))
lolrect.center = ((200,100))
font1 = pygame.font.SysFont('comicsans', 50)
detexto = font1.render('~Player Two Has Scored!!!~', 1, (255,0,0))
window.blit(detexto, (290 - (detexto.get_width()/2),200))
pygame.display.update()
i = 0
while i < 300:
pygame.time.delay(10)
i += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
i = 301
pygame.quit()
# ball hit paddle
for player in [player1, player2]:
if ball1.rect.colliderect(player.rect):
ball1.speed.x *= -1
ball1.speed.x += 0.2 * abs(ball1.speed.x) / ball1.speed.x
# Ball collide upper part of paddle
if ball1.rect.colliderect(player.pad_top()):
# Ball go up
ball1.speed.y = -abs(ball1.speed.y)
# Ball collide lower part of paddle
elif ball1.rect.colliderect(player.pad_bottom()):
# Ball fo down
ball1.speed.y = abs(ball1.speed.y)
# key event
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]: player2.up()
if keys[pygame.K_DOWN]: player2.down()
if keys[pygame.K_w]: player1.up()
if keys[pygame.K_s]: player1.down()
ReDrawWindow()
pygame.display.update()
pygame.quit()
game_intro()
game_loop()