Coding
import pygame
import random
# Initialize pygame
pygame.init()
# Game Constants
WIDTH, HEIGHT = 800, 600
TRUCK_WIDTH, TRUCK_HEIGHT = 60, 40
MUSIC_NOTE_SIZE = 30
OBSTACLE_WIDTH, OBSTACLE_HEIGHT = 60, 40
SPEED = 5
GAME_SPEED = 5
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("DJ Truck Game")
# Load Images
truck_img = pygame.Surface((TRUCK_WIDTH, TRUCK_HEIGHT))
truck_img.fill((0, 255, 0)) # Green Truck
note_img = pygame.Surface((MUSIC_NOTE_SIZE, MUSIC_NOTE_SIZE))
note_img.fill((255, 255, 0)) # Yellow Music Note
obstacle_img = pygame.Surface((OBSTACLE_WIDTH, OBSTACLE_HEIGHT))
obstacle_img.fill((255, 0, 0)) # Red Obstacle
# Game Clock
clock = pygame.time.Clock()
# Fonts
font = pygame.font.SysFont("Arial", 36)
# Player Class
class DJTruck:
def __init__(self):
self.x = WIDTH // 2
self.y = HEIGHT - TRUCK_HEIGHT - 10
self.speed = 7
def move(self, direction):
if direction == "left" and self.x > 0:
self.x -= self.speed
if direction == "right" and self.x < WIDTH - TRUCK_WIDTH:
self.x += self.speed
def draw(self, surface):
surface.blit(truck_img, (self.x, self.y))
# Music Note Class
class MusicNote:
def __init__(self):
self.x = random.randint(0, WIDTH - MUSIC_NOTE_SIZE)
self.y = -MUSIC_NOTE_SIZE
self.speed = 5
def move(self):
self.y += self.speed
def draw(self, surface):
surface.blit(note_img, (self.x, self.y))
# Obstacle Class
class Obstacle:
def __init__(self):
self.x = random.randint(0, WIDTH - OBSTACLE_WIDTH)
self.y = -OBSTACLE_HEIGHT
self.speed = GAME_SPEED
def move(self):
self.y += self.speed
def draw(self, surface):
surface.blit(obstacle_img, (self.x, self.y))
# Main Game Function
def main():
running = True
truck = DJTruck()
music_notes = [MusicNote() for _ in range(5)]
obstacles = [Obstacle() for _ in range(3)]
score = 0
font_score = pygame.font.SysFont("Arial", 28)
while running:
screen.fill((0, 0, 0)) # Black Background
# Check events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Handle movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
truck.move("left")
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
truck.move("right")
# Move and draw Music Notes
for note in music_notes:
note.move()
if note.y > HEIGHT:
note.y = -MUSIC_NOTE_SIZE
note.x = random.randint(0, WIDTH - MUSIC_NOTE_SIZE)
if note.y + MUSIC_NOTE_SIZE > truck.y and note.x < truck.x + TRUCK_WIDTH and note.x + MUSIC_NOTE_SIZE > truck.x:
score += 10
note.y = -MUSIC_NOTE_SIZE
note.x = random.randint(0, WIDTH - MUSIC_NOTE_SIZE)
note.draw(screen)
# Move and draw Obstacles
for obstacle in obstacles:
obstacle.move()
if obstacle.y > HEIGHT:
obstacle.y = -OBSTACLE_HEIGHT
obstacle.x = random.randint(0, WIDTH - OBSTACLE_WIDTH)
if obstacle.y + OBSTACLE_HEIGHT > truck.y and obstacle.x < truck.x + TRUCK_WIDTH and obstacle.x + OBSTACLE_WIDTH > truck.x:
running = False # Game Over
obstacle.draw(screen)
# Draw the Truck
truck.draw(screen)
# Display score
score_text = font_score.render(f"Score: {score}", True, (255, 255, 255))
screen.blit(score_text, (10, 10))
pygame.display.update()
# Frame rate
clock.tick(60)
# Game Over screen
game_over_text = font.render("Game Over", True, (255, 0, 0))
score_text = font.render(f"Final Score: {score}", True, (255, 255, 255))
screen.fill((0, 0, 0))
screen.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 - 50))
screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, HEIGHT // 2 + 10))
pygame.display.update()
pygame.time.wait(2000) # Wait for 2 seconds before closing
pygame.quit()
if __name__ == "__main__":
main()
Computer science
- Computer programming, the process of creating and maintaining the source code of computer programs
- Line coding, in data storage
- Source coding, compression used in data transmission
- Coding theory
- Channel coding
Other uses
- Coding (social sciences), an analytical process in which data are categorized for analysis
- Coding strand of DNA in molecular biology
- Legal coding, the process of creating summary or keyword data from a document in the legal profession
- Medical coding, representation of medical diagnoses and procedures in standard code numbers
- Number coding in Metro Manila, a road space rationing policy implemented in Metro Manila, Philippines, commonly referred to as "coding"
- Coding (therapy), alternative therapeutic methods used to treat addictions in the post-Soviet countries
- Queer coding