0% found this document useful (0 votes)
4 views2 pages

Bouncing Ball Game in Pygame

The document outlines a bouncing ball game and a car racing game simulation using Pygame. It includes code snippets for defining colors, ball settings, game loops, and event handling. The document also contains some syntax errors and typos that need correction for proper functionality.

Uploaded by

suryax945
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Bouncing Ball Game in Pygame

The document outlines a bouncing ball game and a car racing game simulation using Pygame. It includes code snippets for defining colors, ball settings, game loops, and event handling. The document also contains some syntax errors and typos that need correction for proper functionality.

Uploaded by

suryax945
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#Simulation of bouncing ball game using pygame

# Define colors
purple (128, 0, 128)
white = (255, 255, 255)
# Define ball settings
ball_radius 40
ball_ x, ball_y = 100, 100
ball_obj = pygame, Surface((baliradius * 2, ball_radius * 2), pygane. SRCALPHA)
[Link](
#Ballspeed (x
balobj, purple, (ball_radlus, ball_radius), [Link])
and y directions)
speed = [1, 1]
# Game loop
running = Thue
while running:
# Event loop
for event in [Link]):
if [Link] == pygame, QUTT:
oF EN
running= False
#Qear the screen with white color
[Link]( white)
# Move the ball
ballxt= speed[OT
bali_y += speed[1|
# Check for collision with walls
if ball_x -bali_radius < 0 or ball_x + ball_radius >= width:
speed [o]= -speed[O] # Reverse Xdirection
if bal_y - ball_radiuS <= 0 orbali_y+ ball radius >= height:
speed[1]= speed[1] # Reverse Ydirection
# Draw the ball at its new position
[Link](ball_obj,(ball_x- bal_radius, bal_y - ball_radius)
# Update the display
[Link]. flip()
# Quit pygarme
[Link]()
Car qame USin pygame
import pyaame
# Initialize Pyaame
pyaame. init()
# Set up the display
display width 800
display height. = 600
aameDisplay = [Link]. set mode( (display width, display_height) )
[Link]. set caption("A bit Racey")
# Colors
black
white (255, 255, 255)
# Set up the clock
clock pygame. time. Clock()
# Load Gar image (replace wit.h your actual imaae file)
Carlmg = pygame. image. load( car. png') # Make sure you have a car image 1n
the directory
# Function to draw the car G O L
def car(x, y):
[Link](carlma, (x, y) )
OF ENG
# Initial car position
display width * 0.45
y = display heiaht * 0. 8
# Game loop flag
Grashed = False
# Main game loop
while not CGrashed:
for aVent in [Link]. qet( ):
if eent..type [Link]:
Crashed # True
# Fill the screen with whit.e coior
[Link](white)
# Draw the Car at the current position
Garx, y)
# Update the display
[Link] , update()
# Set. the frame rat.e
[Link]í60)
# Quit. the aame TiRUCH RAPE
PALLI
[Link] t( )
quit( )

You might also like