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

Dodger Game Code with Pygame

This document contains a Python script that implements a simple dodger game using the Pygame library. The player controls a character that must avoid falling obstacles while accumulating points, with the game ending upon collision. The script includes collision detection, score tracking, and dynamic obstacle speed increase.

Uploaded by

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

Dodger Game Code with Pygame

This document contains a Python script that implements a simple dodger game using the Pygame library. The player controls a character that must avoid falling obstacles while accumulating points, with the game ending upon collision. The script includes collision detection, score tracking, and dynamic obstacle speed increase.

Uploaded by

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

import pygame

import random
[Link]()
display_width = 880
display_height = 600
color1 = (51, 204, 204)
color2 = (0, 0, 0)
color3 = (255, 0, 0)
display = [Link].set_mode((display_width, display_height))
[Link].set_caption("Dodger Game Using Pygame")
frame_rate = [Link]()
gamer_detail = 45
gamer_movement = [display_width // 2, display_height - 2 * gamer_detail]
gamer_speed = 15
to_dodge_detail = 45
to_dodge_position1 = [[Link](0, display_width - to_dodge_detail), 0]
to_dodge_position2 = [[Link](0, display_width - to_dodge_detail), 0]
to_dodge_speed = 10
points = 0
font = [Link]("calibri", 35)

def check_for_colision(gamer_movement, enemy_pos):


p_x, p_y = gamer_movement
e_x, e_y = enemy_pos
if (e_x < p_x < e_x + to_dodge_detail or e_x < p_x + gamer_detail < e_x +
to_dodge_detail) and \
(e_y < p_y < e_y + to_dodge_detail or e_y < p_y + gamer_detail < e_y +
to_dodge_detail):
return True
return False

game_over = False
while not game_over:
for event in [Link]():
if [Link] == [Link]:
game_over = True
keys = [Link].get_pressed()
if keys[pygame.K_LEFT]:
gamer_movement[0] -= gamer_speed
if keys[pygame.K_RIGHT]:
gamer_movement[0] += gamer_speed
if gamer_movement[0] < 0:
gamer_movement[0] = 0
elif gamer_movement[0] > display_width - gamer_detail:
gamer_movement[0] = display_width - gamer_detail
[Link](color2)
to_dodge_position1[1] += to_dodge_speed
to_dodge_position2[1] += to_dodge_speed
if to_dodge_position1[1] > display_height:
to_dodge_position1 = [[Link](
0, display_width - to_dodge_detail), 0]
points += 1
to_dodge_speed += 0.5
if to_dodge_position2[1] > display_height:
to_dodge_position2 = [[Link](
0, display_width - to_dodge_detail), 0]
points += 1
to_dodge_speed += 0.5
if check_for_colision(gamer_movement, to_dodge_position1) or
check_for_colision(gamer_movement,

to_dodge_position2):
game_over = True
break
[Link](
display, color3, (to_dodge_position1[0], to_dodge_position1[1],
to_dodge_detail, to_dodge_detail))
[Link](
display, color3, (to_dodge_position2[0], to_dodge_position2[1],
to_dodge_detail, to_dodge_detail))
[Link](
display, color1, (gamer_movement[0], gamer_movement[1],
gamer_detail, gamer_detail))
score_text = [Link]("Score: {}".format(points), True, color1)
[Link](score_text, (10, 10))
[Link]()
frame_rate.tick(30)
[Link](color2)
game_over_text = [Link]("Final Score: {}".format(points), True, color1)
[Link](game_over_text, (display_width //
2 - 200, display_height // 2 - 20))
[Link]()
[Link](3000)
[Link]()

You might also like