CompSci Blogs

August 2023 to June 2024

View the Project on GitHub IshanCornick/new_student

2 October 2023

Frogger

# Import pygame and sys so that I can make the game in pygame and close it using sys.
import pygame, sys
# Opens pygame libary for my use
pygame.init()
# Set up the screen, name, size, fps and colors
frog = str(input("What frog do you want out of these? Frog 1 - Posion Dart Frog (less hp more damage)  Frog 2 - Tree Frog (less hp higher speed) Frog 3 - Bull Frog (More Hp,lower, bigger hitbox) Frog 4 - Glass Frog (Smaller hibox) Frog 5 - Flea frog (Basic frog)"))
SCREENWIDTH,SCREENHEIGHT = 500,400
FPS = 60
WIN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
pygame.display.set_caption("pygame_temp")
YELLOW = (255, 255, 0)
BLACK = (0, 0, 0)
RED = (255,0,0)
WHITE = (255,255,255)

# Set up background
BG = pygame.image.load("../images/Backgroundwater.jpg")
BG = pygame.transform.scale(BG, (SCREENWIDTH, SCREENHEIGHT))
GAMEOVER = pygame.image.load("../images/download.png")
GAMEOVER = pygame.transform.scale(GAMEOVER, (SCREENWIDTH, SCREENHEIGHT))

# Set up images and sounds
HITSOUND = pygame.mixer.Sound("../images/hit-4.ogg")
SHOOTSOUND = pygame.mixer.Sound("../images/pew.ogg")
if frog == "frog1":
  REDIMAGE = pygame.image.load("../images/spaceship_red.png")
elif frog == "frog2":
  REDIMAGE = pygame.image.load("../images/spaceship_red.png")
elif frog == "frog3":
  REDIMAGE = pygame.image.load("../images/spaceship_red.png")
elif frog == "frog4":
  REDIMAGE = pygame.image.load("../images/spaceship_red.png")
elif frog == "frog5":
  REDIMAGE = pygame.image.load("../images/spaceship_red.png")
REDIMAGE = pygame.transform.scale(REDIMAGE, (55, 40))
REDIMAGE = pygame.transform.rotate(REDIMAGE, -90)
YELLOWIMAGE = pygame.image.load("../images/spaceship_yellow.png")
YELLOWIMAGE = pygame.transform.scale(YELLOWIMAGE, (55, 40))
YELLOWIMAGE = pygame.transform.rotate(YELLOWIMAGE, 90)
HEALTH_FONT = pygame.font.SysFont("Sans-serif", 40)
YELLOW_HIT = pygame.USEREVENT + 1
RED_HIT = pygame.USEREVENT + 2
boarder = pygame.Rect(SCREENWIDTH/2-5,0,10,SCREENHEIGHT)

# Pressing keys make you move, make boaders 
def yellowmovement(keys_pressed, yellow):
  if keys_pressed[pygame.K_a] and yellow.x > 0:
    yellow.x -= 5
  if keys_pressed[pygame.K_d] and yellow.right <= boarder.left +16:
    yellow.x += 5
  if keys_pressed[pygame.K_w] and yellow.top >= 0:
    yellow.y -= 5
  if keys_pressed[pygame.K_s] and yellow.bottom <= SCREENHEIGHT:
    yellow.y += 5
def redmovement(keys_pressed, red):
  if keys_pressed[pygame.K_LEFT] and red.x >= boarder.right:
    red.x -= 5
  if keys_pressed[pygame.K_RIGHT] and red.right <= SCREENWIDTH:
    red.x += 5
  if keys_pressed[pygame.K_UP] and red.top >= 0:
    red.y -= 5
  if keys_pressed[pygame.K_DOWN] and red.bottom <= SCREENHEIGHT:
    red.y += 5
# Make red bullet move and get removed when off screen  
def handle_bullets(yellow_bullet, red_bullet, r, y):
  for i in red_bullet:
    i.x -= 10
    if y.colliderect(i):
      pygame.event.post(pygame.event.Event(YELLOW_HIT))
      red_bullet.remove(i)
    elif i.x < 0:
      red_bullet.remove(i)
# Make yellow bullet move and get removed when off screen 
  for i in yellow_bullet:
    i.x += 10
    if r.colliderect(i):
      pygame.event.post(pygame.event.Event(RED_HIT))
      yellow_bullet.remove(i)
    elif i.x > SCREENWIDTH:
      yellow_bullet.remove(i)


def main():
  global BG
  # make HP of both
  red_hp = 10
  yellow_hp = 10
  # CLock for fps
  clock = pygame.time.Clock()
  # hitbox
  red = pygame.Rect(375,200, 55, 40 )
  yellow = pygame.Rect(125,200, 55, 40)
  # List of bullets, I can append to add to list
  red_bullet = []
  yellow_bullet = []
  # Repeating list for everything
  while True:
    for event in pygame.event.get():
    # Quit the game when over
      if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
    # shooting sound effects and where the bullets spawns from
      if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LSHIFT:
          Bullet = pygame.Rect(yellow.right - 35, yellow.centery +3, 25, 5)
          yellow_bullet.append(Bullet)
          SHOOTSOUND.play()
        if event.key == pygame.K_RSHIFT:
          Bullet2 = pygame.Rect(red.left, red.centery + 3, 25, 5)
          red_bullet.append(Bullet2)
          SHOOTSOUND.play()
    # if hit my the enemy or hit the enemy play a sound
      if event.type == RED_HIT:
        red_hp -= 1
        HITSOUND.play()
      if event.type == YELLOW_HIT:
        yellow_hp -= 1
        HITSOUND.play()
    # run everything with assigned variables
    handle_bullets(yellow_bullet, red_bullet, red, yellow)

    keys_pressed = pygame.key.get_pressed()
    yellowmovement(keys_pressed, yellow)
    redmovement(keys_pressed, red)
  # Who won?
    if red_hp <= 0:
      print ("Yellow wins")
      break
     
    elif yellow_hp <= 0:
      print ("Red wins")
      break
      
    

# Run images to display
    WIN.fill(BLACK)
    WIN.blit(BG, (0,0))
    pygame.draw.rect(WIN, BLACK, boarder)
    WIN.blit(REDIMAGE, (red.x,red.y))
    WIN.blit(YELLOWIMAGE, (yellow.x,yellow.y))
    for i in yellow_bullet:
      pygame.draw.rect(WIN, YELLOW, i)
    for i in red_bullet:
      pygame.draw.rect(WIN, RED, i)
# Print hp at top corner
    red_text = HEALTH_FONT.render("Hp: " + str(red_hp), 1, WHITE)
    yellow_text = HEALTH_FONT.render("Hp: " + str(yellow_hp), 1, WHITE)
    WIN.blit(red_text, (375,0))
    WIN.blit(yellow_text, (0,0))
    pygame.display.update()
    clock.tick(FPS)
# Check again if game over
  while True:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
        exit()
        
      
# If game over show game over screen
    WIN.fill(BLACK)
    WIN.blit(GAMEOVER, (0,0))
    pygame.display.update()
    clock.tick(FPS)

# Run main
main()
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 and sys so that I can make the game in pygame and close it using sys.
import pygame, sys
# Opens pygame libary for my use
pygame.init()
# Set up the screen, name, size, fps and colors
frog = str(input("What frog do you want out of these? Frog 1 - Posion Dart Frog (less hp more damage)  Frog 2 - Tree Frog (less hp higher speed) Frog 3 - Bull Frog (More Hp,lower, bigger hitbox) Frog 4 - Glass Frog (Smaller hibox) Frog 5 - Flea frog (Basic frog)"))
SCREENWIDTH,SCREENHEIGHT = 500,400
FPS = 60
WIN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
pygame.display.set_caption("pygame_temp")
YELLOW = (255, 255, 0)
BLACK = (0, 0, 0)
RED = (255,0,0)
WHITE = (255,255,255)

# Set up background
BG = pygame.image.load("../images/Backgroundwater.jpg")
BG = pygame.transform.scale(BG, (SCREENWIDTH, SCREENHEIGHT))
GAMEOVER = pygame.image.load("../images/download.png")
GAMEOVER = pygame.transform.scale(GAMEOVER, (SCREENWIDTH, SCREENHEIGHT))

# Set up images and sounds
HITSOUND = pygame.mixer.Sound("../images/hit-4.ogg")
SHOOTSOUND = pygame.mixer.Sound("../images/pew.ogg")
if frog == "frog1":
  REDIMAGE = pygame.image.load("../images/spaceship_red.png")
elif frog == "frog2":
  REDIMAGE = pygame.image.load("../images/spaceship_red.png")
elif frog == "frog3":
  REDIMAGE = pygame.image.load("../images/spaceship_red.png")
elif frog == "frog4":
  REDIMAGE = pygame.image.load("../images/spaceship_red.png")
elif frog == "frog5":
  REDIMAGE = pygame.image.load("../images/spaceship_red.png")
REDIMAGE = pygame.transform.scale(REDIMAGE, (55, 40))
REDIMAGE = pygame.transform.rotate(REDIMAGE, -90)
YELLOWIMAGE = pygame.image.load("../images/spaceship_yellow.png")
YELLOWIMAGE = pygame.transform.scale(YELLOWIMAGE, (55, 40))
YELLOWIMAGE = pygame.transform.rotate(YELLOWIMAGE, 90)
HEALTH_FONT = pygame.font.SysFont("Sans-serif", 40)
YELLOW_HIT = pygame.USEREVENT + 1
RED_HIT = pygame.USEREVENT + 2
boarder = pygame.Rect(SCREENWIDTH/2-5,0,10,SCREENHEIGHT)

# Pressing keys make you move, make boaders 
def yellowmovement(keys_pressed, yellow):
  if keys_pressed[pygame.K_a] and yellow.x > 0:
    yellow.x -= 5
  if keys_pressed[pygame.K_d] and yellow.right <= boarder.left +16:
    yellow.x += 5
  if keys_pressed[pygame.K_w] and yellow.top >= 0:
    yellow.y -= 5
  if keys_pressed[pygame.K_s] and yellow.bottom <= SCREENHEIGHT:
    yellow.y += 5
def redmovement(keys_pressed, red):
  if keys_pressed[pygame.K_LEFT] and red.x >= boarder.right:
    red.x -= 5
  if keys_pressed[pygame.K_RIGHT] and red.right <= SCREENWIDTH:
    red.x += 5
  if keys_pressed[pygame.K_UP] and red.top >= 0:
    red.y -= 5
  if keys_pressed[pygame.K_DOWN] and red.bottom <= SCREENHEIGHT:
    red.y += 5
# Make red bullet move and get removed when off screen  
def handle_bullets(yellow_bullet, red_bullet, r, y):
  for i in red_bullet:
    i.x -= 10
    if y.colliderect(i):
      pygame.event.post(pygame.event.Event(YELLOW_HIT))
      red_bullet.remove(i)
    elif i.x < 0:
      red_bullet.remove(i)
# Make yellow bullet move and get removed when off screen 
  for i in yellow_bullet:
    i.x += 10
    if r.colliderect(i):
      pygame.event.post(pygame.event.Event(RED_HIT))
      yellow_bullet.remove(i)
    elif i.x > SCREENWIDTH:
      yellow_bullet.remove(i)


def main():
  global BG
  # make HP of both
  red_hp = 10
  yellow_hp = 10
  # CLock for fps
  clock = pygame.time.Clock()
  # hitbox
  red = pygame.Rect(375,200, 55, 40 )
  yellow = pygame.Rect(125,200, 55, 40)
  # List of bullets, I can append to add to list
  red_bullet = []
  yellow_bullet = []
  # Repeating list for everything
  while True:
    for event in pygame.event.get():
    # Quit the game when over
      if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
    # shooting sound effects and where the bullets spawns from
      if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LSHIFT:
          Bullet = pygame.Rect(yellow.right - 35, yellow.centery +3, 25, 5)
          yellow_bullet.append(Bullet)
          SHOOTSOUND.play()
        if event.key == pygame.K_RSHIFT:
          Bullet2 = pygame.Rect(red.left, red.centery + 3, 25, 5)
          red_bullet.append(Bullet2)
          SHOOTSOUND.play()
    # if hit my the enemy or hit the enemy play a sound
      if event.type == RED_HIT:
        red_hp -= 1
        HITSOUND.play()
      if event.type == YELLOW_HIT:
        yellow_hp -= 1
        HITSOUND.play()
    # run everything with assigned variables
    handle_bullets(yellow_bullet, red_bullet, red, yellow)

    keys_pressed = pygame.key.get_pressed()
    yellowmovement(keys_pressed, yellow)
    redmovement(keys_pressed, red)
  # Who won?
    if red_hp <= 0:
      print ("Yellow wins")
      break
     
    elif yellow_hp <= 0:
      print ("Red wins")
      break
      
    

# Run images to display
    WIN.fill(BLACK)
    WIN.blit(BG, (0,0))
    pygame.draw.rect(WIN, BLACK, boarder)
    WIN.blit(REDIMAGE, (red.x,red.y))
    WIN.blit(YELLOWIMAGE, (yellow.x,yellow.y))
    for i in yellow_bullet:
      pygame.draw.rect(WIN, YELLOW, i)
    for i in red_bullet:
      pygame.draw.rect(WIN, RED, i)
# Print hp at top corner
    red_text = HEALTH_FONT.render("Hp: " + str(red_hp), 1, WHITE)
    yellow_text = HEALTH_FONT.render("Hp: " + str(yellow_hp), 1, WHITE)
    WIN.blit(red_text, (375,0))
    WIN.blit(yellow_text, (0,0))
    pygame.display.update()
    clock.tick(FPS)
# Check again if game over
  while True:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
        exit()
        
      
# If game over show game over screen
    WIN.fill(BLACK)
    WIN.blit(GAMEOVER, (0,0))
    pygame.display.update()
    clock.tick(FPS)

# Run main
main()
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.