0

I've been making this game, it's like bubble trouble, and I'm having trouble with the ball. I need help trying to append the ball into the list so it shows and start bouncing in the beginning of the game, but when I append the ball, it doesn't work.

Here is the code:

import math
import random as rand
import pygame
import time

pygame.init()

win = pygame.display.set_mode((900,900))
pygame.display.set_caption("Bubble Trouble")
clock = pygame.time.Clock()
bg = pygame.image.load("player1.gif")
background = pygame.image.load("Water.gif")
bg2 = pygame.image.load("player2.gif")
tri = pygame.image.load("triangle.png")
tri = pygame.transform.scale(tri, (50,30))
tri2 = pygame.image.load("triangle2.png")
tri2 = pygame.transform.scale(tri2, (50,30))
ball = pygame.image.load("ball.png")
ball = pygame.transform.scale(ball, (180,150))
jump = False
start = True
startb = True
jumpcount = 12
grav = 1

class p1():
  def __init__(self, x, y, width, height):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.vel = 10
    self.jumpcount = 10
    self.left = False
    self.right = False
    self.walkcount = 0
    
  def draw(self,win):
    if octo.walkcount + 1 >= 27:
      octo.walkcount = 0
    if left:
      win.blit(bg, (octo.x,octo.y))
      octo.walkcount -= 1
    elif right:
      win.blit(bg, (octo.x,octo.y))
      octo.walkcount += 1
    else:
      win.blit(bg, (octo.x,octo.y))

class bubble():
  def __init__(self, x, y, width, height):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.vel = 1
    self.velx = 4
    self.jumpcount = 14
    self.left = False
    self.right = False
    self.walkcount = 0
    self.max = 399
    self.facing = 5

  def drawb(self, win):
    bubbles.append(ball)

class p2():
  def __init__(self, x, y, width, height):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.vel = 10
    self.jumpcount = 10
    self.left = False
    self.right = False
    self.walkcount = 10

  def draw2(self,win):
    if self.walkcount + 1 >= 27:
      self.walkcount = 0
    if left:
      win.blit(bg2, (octo2.x,octo2.y))
      self.walkcount -= 1
    elif right:
      win.blit(bg2, (octo2.x,octo2.y))
      self.walkcount += 1
    else:
      win.blit(bg2, (octo2.x,octo2.y))

class projectile():
  def __init__(self, x, y, width, height):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.vel = 7

  def draw3(self,win):
    win.blit(tri, (self.x,self.y))

class projectile2():
  def __init__(self, x, y, width, height):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.vel = 7

  def draw4(self,win):
    win.blit(tri2, (self.x,self.y))

WHITE = (255, 255, 255)
BLACK = (  0,   0,   0)
CURSOR_SIZE = 20
FONT_SIZE = 20
FONT = ('Arial', FONT_SIZE, 'bold')

def pop():
  global ball
  bubbles.pop(bubbles.index(ball))
  ball = pygame.transform.scale(ball, (90,75))
  win.blit(ball, (b.x - 290, b.y - 300))

def redrawGameWindow():
  win.blit(background, (0, -150))
  for bullet in bullets:
    bullet.draw3(win)
  for arrow in arrows:
    arrow.draw4(win)
  octo.draw(win)
  if len(bubbles) < 10:
    print("created")
    bubbles.append(b)
  octo2.draw2(win)
  pygame.display.update()

run = True
b = bubble(300, 400, 64, 64)
octo2 = p2(300, 400, 64, 64)
octo = p1(300, 400, 64, 64)
bullets = []
arrows = []
bubbles = []
while run:
  clock.tick(27)
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      run = False

  for bullet in bullets:
    if bullet.y > -100:
      bullet.y -= bullet.vel
      if (abs(abs(b.y)-abs(bullet.y))) < 50:
        if (abs(abs(b.x)-abs(bullet.x))) < 50:
          print("pop")
    else:
      bullets.pop(bullets.index(bullet))

  for arrow in arrows:
    if arrow.y > -100:
      arrow.y -= arrow.vel
      d_x = abs(arrow.x) - abs(b.x) + 200
      d_y = abs(arrow.y) - abs(b.y) + 150
      d = math.sqrt(d_x**2 + d_y**2)
      if d < 100:
        print("popped")
    else:
      arrows.pop(arrows.index(arrow))
  
  for ball in bubbles:

    if b.velx >= 9:
        b.velx = 9

    if start:
      b.y += 9
      b.x += 3
      if b.y > 650:
        jump = True
        start = False

      if (b.facing == 5):
        direction = 1
      else:
        direction = -1
        
      if b.x > 1100:
        b.facing = 0
        grav += 0.2

      if b.x < 270:
        b.facing = 5
        grav += 0.2
        b.velx += 1

      if grav >= 7:
        grav = 7
        b.velx += 1

      if jumpcount >= -16 and jump and b.y < 650:
        b.x += b.velx * direction
        b.y -= ((jumpcount * abs(jumpcount)) * 0.25) / grav
        b.vel -= 1
        jumpcount -= .8
      else:
        if b.y >= 650:
          jumpcount = b.jumpcount
          b.y -= ((jumpcount * abs(jumpcount)) * 0.25) / grav
        jumpcount = b.jumpcount

  '''
  if b.velx >= 9:
    b.velx = 9

  if start:
    bubbles.append(b)
    b.y += 9
    b.x += 3
    if b.y > 650:
      jump = True
      start = False

  if (b.facing == 5):
    direction = 1
  else:
    direction = -1
    
  if b.x > 1100:
    b.facing = 0
    grav += 0.2

  if b.x < 270:
    b.facing = 5
    grav += 0.2
    b.velx += 1

  if grav >= 7:
    grav = 7
    b.velx += 1

  if jumpcount >= -16 and jump and b.y < 650:
    b.x += b.velx * direction
    b.y -= ((jumpcount * abs(jumpcount)) * 0.25) / grav
    b.vel -= 1
    jumpcount -= .8
  else:
    #b.vel = 20
    #b.jumpcount -= 1
    if b.y >= 650:
      jumpcount = b.jumpcount
      b.y -= ((jumpcount * abs(jumpcount)) * 0.25) / grav
    jumpcount = b.jumpcount
    #if b.jumpcount < 1:
      #b.jumpcount = 14
  '''
  keys = pygame.key.get_pressed()

  if keys[pygame.K_LEFT] and octo.x > octo.vel:
    octo.x -= octo.vel
    left = True
    right = False

  elif keys[pygame.K_RIGHT] and octo.x < 885 - octo.width - octo.vel:
    octo.x += octo.vel
    right = True
    left = False
  else:
    right = False
    left = False
    octo.walkcount = 0

  if keys[pygame.K_UP]:
    if len(bullets) < 1:
      bullets.append(projectile(round(octo.x + octo.width//2), round(octo.y + octo.height//2 + 30), 6, (0,0,0)))
  
  if keys[pygame.K_SPACE]:
    if len(arrows) < 1:
      arrows.append(projectile2(round(octo2.x + octo2.width//2), round(octo2.y + octo2.height//2 + 30), 6, (0,0,0)))

  if keys[pygame.K_a] and octo2.x > octo2.vel:
    octo2.x -= octo2.vel
    left = True
    right = False

  elif keys[pygame.K_d] and octo2.x < 885 - octo2.width - octo2.vel:
    octo2.x += octo2.vel
    right = True
    left = False
  else:
    right = False
    left = False
    octo2.walkcount = 0

  redrawGameWindow()

pygame.quit()
vinzee
  • 17,022
  • 14
  • 42
  • 60
Dajab
  • 1
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Dec 09 '21 at 17:31

0 Answers0