Showing posts with label pygame. Show all posts
Showing posts with label pygame. Show all posts

Pygame - car racing - python

 # Car Racing based on keypress


import sys,pygame


pygame.init()

screen = pygame.display.set_mode((500, 500))

pygame.display.set_caption("Car Racing")

  

# Car starting co-ordinates and size

x = 200

y = 200


width = 20

height = 20

  

# Moving speed

step = 5


# Loading image

car = pygame.image.load("car.png")

car = pygame.transform.scale(car, (35, 65))

carrect = car.get_rect()


# Changing direction 

car_rotated_Left = pygame.transform.rotate(car, 90)

car_rotated_Right = pygame.transform.rotate(car, 270)

car_rotated_Up = pygame.transform.rotate(car, 0)

car_rotated_Down = pygame.transform.rotate(car, 180)


# infinite loop 

while 1:

pygame.time.delay(10)

angle = 0

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()


# Get the keys pressed 

keys = pygame.key.get_pressed()

      

    # Left arrow key - decrement in x axis

if keys[pygame.K_LEFT]and x>0:

x-=step

car=car_rotated_Left

    # Right arrow key - increment in x axis

if keys[pygame.K_RIGHT] and x<500-width:

x += step

car=car_rotated_Right

         

    # Up arrow key - decrement in y axis

if keys[pygame.K_UP] and y>0:

y -= step

car=car_rotated_Up

          

    # Down arrow key - increment in y axis

if keys[pygame.K_DOWN] and y<500-height:

y += step

car=car_rotated_Down


    # Set screen background and Place moving object on screen 

screen.fill((255, 255, 255))

screen.blit(car, (x, y))

pygame.display.flip()

      

    # Display movement

pygame.display.update() 

  

pygame.quit()


Pygame - Moving an object based on key pressed

# Moving an object based on keypress


import sys,pygame


pygame.init()

screen = pygame.display.set_mode((500, 500))

pygame.display.set_caption("Moving object")

  

# Moving object starting co-ordinates and size

x = 200

y = 200


width = 20

height = 20

  

# Moving speed

step = 5

  

# infinite loop 

while 1:

pygame.time.delay(10)

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()


# Get the keys pressed 

keys = pygame.key.get_pressed()

      

    # Left arrow key - decrement in x axis

if keys[pygame.K_LEFT]and x>0:

x-=step

    # Right arrow key - increment in x axis

if keys[pygame.K_RIGHT] and x<500-width:

x += step

         

    # Up arrow key - decrement in y axis

if keys[pygame.K_UP] and y>0:

y -= step

          

    # Down arrow key - increment in y axis

if keys[pygame.K_DOWN] and y<500-height:

y += step

        

    # Set screen background and Place moving object on screen 

screen.fill((255, 255, 255))

pygame.draw.rect(screen, (255, 0, 0), (x, y, width, height))

      

    # Display movement

pygame.display.update() 

  

pygame.quit()

Algorithm - Bouncing ball - pygame

Algorithm - Bouncing ball 
  1.     Start the program
  2.     Set screen size and background color.
  3.    Set speed of moving ball.
  4.    Create a graphical window using set_mode()
  5.    Set caption
  6.     Load the ball image and create a rectangle area covering the image
  7.    Use blit() method to copy the pixel color of the ball to the screen
  8.     Set background color of screen and use flip() method to make all images visible.
  9.     Move the ball in specified speed.
  10.    If ball hits the edges of the screen reverse the direction. 
  11.    Create an infinite loop and Repeat steps 9 and 10 until user quits the program
  12.     Stop the program


Algorithm - Simulation of Elliptical Orbit

Algorithm - Simulation of Elliptical orbit
  1.     Start the program
  2.     Set screen size and caption
  3.     Create clock variable
  4.     Set x and y radius of ellipse.
  5.     Starting from degree 0 ending with 360 degrees in increments of 10 degrees  calculate the (x1, y1) coordinates to find a point in the elliptical orbit.
  6.     Convert degree to radians (degree * 2 * math.pi / 360)
  7.     Set background color, draw center circle, ellipse and another smaller circle on the ellipse
  8.     Refresh the screen every 5 clock ticks
  9.     Repeat steps 4 to 8 until user quits the program
  10.     Stop the program


Simulate Elliptical Orbit in Pygame

#Elliptical orbits

import pygame
import math
import sys

pygame.init()

screen = pygame.display.set_mode((600, 300))
pygame.display.set_caption("Elliptical orbit")

clock = pygame.time.Clock()

while(True):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
   
    xRadius = 250
    yRadius = 100
   
    for degree in range(0,360,10):
        x1 = int(math.cos(degree * 2 * math.pi / 360) * xRadius) + 300
        y1 = int(math.sin(degree * 2 * math.pi / 360) * yRadius) + 150
        screen.fill((0, 0, 0))
        pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35)
        pygame.draw.ellipse(screen, (255, 255, 255), [50, 50, 500, 200], 1)
        pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15)
       
        pygame.display.flip()
        clock.tick(5)


Algorithmhttps://drranurekha.blogspot.com/2019/10/algorithm-simulation-of-elliptical-orbit_58.html



Output:








Bouncing ball, python

# Bouncing ball


# Program
# Bouncing ball

import sys, pygame
pygame.init()


size = width, height = 700, 250
speed = [1, 1]
background = 255, 255, 255
screen = pygame.display.set_mode(size)


pygame.display.set_caption("Bouncing ball")


ball = pygame.image.load("ball.jpg")
ballrect = ball.get_rect()


while 1:
pygame.time.delay(2)

for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()

ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]

screen.fill(background)
screen.blit(ball, ballrect)
pygame.display.flip()


   


ball.jpg
   

Output: