CompSci Blogs

August 2023 to June 2024

View the Project on GitHub IshanCornick/new_student

28 September 2023

Pong

import pygame, sys, random
pygame.init()

sf = pygame.font.SysFont

WIN = pygame.display.set_mode((600, 380))
FPS = 30
pygame.display.set_caption('Pong')

leftscore = pygame.USEREVENT + 1
rightscore = pygame.USEREVENT + 2

hit_sound = pygame.mixer.Sound("../images/hit.ogg")
wall_sound = pygame.mixer.Sound("../images/wall.ogg")

BLACK = (0, 0, 0)
WHITE = (255,255,255)
score_font = sf('Sans-serif',100)
angel_range = [-5,-4,-3,-2,2,3,4,5]
def reset_ball(ball,ball_v):
  ball_v[0] =  random.choice(angel_range)
  ball_v[1] =  random.choice(angel_range)
  ball.x = 300
  ball.y = 200  
  
def move_ball(ball,ball_v,left,right):
  ball.x += ball_v[0]
  ball.y += ball_v[1]
  if ball.top <= 0 or ball.bottom >= 380:
    ball_v[1] = -ball_v[1]
    hit_sound.play()
  if left.colliderect(ball) or right.colliderect(ball):
    ball_v[0] = -ball_v[0]
    hit_sound.play()
  if ball.right >= 600:
    pygame.event.post(pygame.event.Event(leftscore))
  if ball.left <= 0:
    pygame.event.post(pygame.event.Event(rightscore))
    
  
    

  
def O10(keys_pressed, left, right):
  if keys_pressed[pygame.K_UP] and right.top > 0:
    right.y -= 10
  if keys_pressed[pygame.K_DOWN] and right.bottom < 380:
    right.y += 10
  if keys_pressed[pygame.K_w] and left.top > 0:
    left.y -= 10
  if keys_pressed[pygame.K_s] and left.bottom < 380:
    left.y += 10
  
   
 

def main():
  leftscores = 0
  rightscores = 0
  clock = pygame.time.Clock()
  ball = pygame.Rect(300,200,10,10)
  xs = 0
  boarder = pygame.Rect(310,0,10,490)
  
  ball_v =  [random.randint(3,5),random.randint(3,5)]
  right = pygame.Rect(550,150,10,80)
  left = pygame.Rect(50,150,10,80)
  while True:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              pygame.quit()
              sys.exit()
          if event.type == leftscore:
            leftscores += 1
            wall_sound.play()
            reset_ball(ball,ball_v)
          if event.type == rightscore:
            rightscores += 1
            wall_sound.play()
            reset_ball(ball,ball_v)
            

      keys_pressed = pygame.key.get_pressed()
      O10(keys_pressed, left, right)
      move_ball(ball,ball_v,left,right)
    
      xs += 0.1
      WIN.fill(BLACK)
      pygame.draw.rect(WIN,WHITE, ball)
      pygame.draw.rect(WIN,WHITE, right)
      pygame.draw.rect(WIN,WHITE, left)
      pygame.draw.rect(WIN,WHITE, boarder)

  
      left_scoret = score_font.render("{:02d}".format(leftscores), 1 , WHITE)
      right_scoret = score_font.render("{:02d}".format(rightscores), 1, WHITE)
      WIN.blit(right_scoret,(325,0))
      WIN.blit(left_scoret,(225,0))
      pygame.display.update()
      clock.tick(FPS)

main()
pygame 2.5.1 (SDL 2.28.2, Python 3.11.5)
Hello from the pygame community. https://www.pygame.org/contribute.html



An exception has occurred, use %tb to see the full traceback.


SystemExit



/Users/Ishan/Library/Python/3.11/lib/python/site-packages/IPython/core/interactiveshell.py:3516: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)



The Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.
import pygame, sys, random
pygame.init()

sf = pygame.font.SysFont

WIN = pygame.display.set_mode((600, 380))
FPS = 30
pygame.display.set_caption('Pong')

leftscore = pygame.USEREVENT + 1
rightscore = pygame.USEREVENT + 2

hit_sound = pygame.mixer.Sound("../images/hit.ogg")
wall_sound = pygame.mixer.Sound("../images/wall.ogg")

BLACK = (0, 0, 0)
WHITE = (255,255,255)
score_font = sf('Sans-serif',100)
angel_range = [-5,-4,-3,-2,2,3,4,5]
def reset_ball(ball,ball_v):
  ball_v[0] =  random.choice(angel_range)
  ball_v[1] =  random.choice(angel_range)
  ball.x = 300
  ball.y = 200  
  
def move_ball(ball,ball_v,left,right):
  ball.x += ball_v[0]
  ball.y += ball_v[1]
  if ball.top <= 0 or ball.bottom >= 380:
    ball_v[1] = -ball_v[1]
    hit_sound.play()
  if left.colliderect(ball) or right.colliderect(ball):
    ball_v[0] = -ball_v[0]
    hit_sound.play()
  if ball.right >= 600:
    pygame.event.post(pygame.event.Event(leftscore))
  if ball.left <= 0:
    pygame.event.post(pygame.event.Event(rightscore))
    
  
    

  
def O10(keys_pressed, left, right):
  if keys_pressed[pygame.K_UP] and right.top > 0:
    right.y -= 10
  if keys_pressed[pygame.K_DOWN] and right.bottom < 380:
    right.y += 10
  if keys_pressed[pygame.K_w] and left.top > 0:
    left.y -= 10
  if keys_pressed[pygame.K_s] and left.bottom < 380:
    left.y += 10
  
   
 

def main():
  leftscores = 0
  rightscores = 0
  clock = pygame.time.Clock()
  ball = pygame.Rect(300,200,10,10)
  xs = 0
  boarder = pygame.Rect(310,0,10,490)
  
  ball_v =  [random.randint(3,5),random.randint(3,5)]
  right = pygame.Rect(550,150,10,80)
  left = pygame.Rect(50,150,10,80)
  while True:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              pygame.quit()
              sys.exit()
          if event.type == leftscore:
            leftscores += 1
            wall_sound.play()
            reset_ball(ball,ball_v)
          if event.type == rightscore:
            rightscores += 1
            wall_sound.play()
            reset_ball(ball,ball_v)
            

      keys_pressed = pygame.key.get_pressed()
      O10(keys_pressed, left, right)
      move_ball(ball,ball_v,left,right)
    
      xs += 0.1
      WIN.fill(BLACK)
      pygame.draw.rect(WIN,WHITE, ball)
      pygame.draw.rect(WIN,WHITE, right)
      pygame.draw.rect(WIN,WHITE, left)
      pygame.draw.rect(WIN,WHITE, boarder)

  
      left_scoret = score_font.render("{:02d}".format(leftscores), 1 , WHITE)
      right_scoret = score_font.render("{:02d}".format(rightscores), 1, WHITE)
      WIN.blit(right_scoret,(325,0))
      WIN.blit(left_scoret,(225,0))
      pygame.display.update()
      clock.tick(FPS)

main()
pygame 2.5.1 (SDL 2.28.2, Python 3.11.5)
Hello from the pygame community. https://www.pygame.org/contribute.html



An exception has occurred, use %tb to see the full traceback.


SystemExit



/Users/Ishan/Library/Python/3.11/lib/python/site-packages/IPython/core/interactiveshell.py:3516: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)



The Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.