0% found this document useful (0 votes)
9 views

Game 1 Py

Uploaded by

pasha.vsharov
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)
9 views

Game 1 Py

Uploaded by

pasha.vsharov
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
You are on page 1/ 3

import pygame

clock = pygame.time.Clock()

pygame.init()
wnd = pygame.display.set_mode((635, 360))
pygame.display.set_caption('Game №1')
icon = pygame.image.load('images/icon.png.png')
pygame.display.set_icon(icon)

bg = pygame.image.load('images/back_ground.jpg')
enemy1_walk = [
pygame.image.load('images/Enemy1/enemy1_14.xcf'),
pygame.image.load('images/Enemy1/enemy1_2.xcf'),
pygame.image.load('images/Enemy1/enemy1_3.xcf'),
pygame.image.load('images/Enemy1/enemy1_14.xcf')
]
walk_left = [
pygame.image.load('images/left/left13.xcf'),
pygame.image.load('images/left/left2.xcf'),
pygame.image.load('images/left/left13.xcf'),
pygame.image.load('images/left/left4.xcf')
]
walk_right = [
pygame.image.load('images/right/right13.xcf'),
pygame.image.load('images/right/right2.xcf'),
pygame.image.load('images/right/right13.xcf'),
pygame.image.load('images/right/right4.xcf')
]
enemy_list_in_game = []

gameplay = True

enemy1_anim_count = 0
enemy1_speed = 5

player_anim_count = 0
bg_x = 0

bg_sound = pygame.mixer.Sound('sounds/player_sound_walk.mp3')
bg_sound.play()
finish_sound = pygame.mixer.Sound('sounds/ops.mp3')

label = pygame.font.Font('fonts/OpenSans-VariableFont_wdth,wght.ttf', 80)


label1 = pygame.font.Font('fonts/OpenSans-VariableFont_wdth,wght.ttf', 40)
Game_over = label.render('Game over', True, 'red')
Tap_to_restart = label1.render('Tap to restart', True, (145, 145, 145))
Ttr_rect = Tap_to_restart.get_rect(topleft=(190, 200))
bullet = pygame.image.load('images/bullet.xcf')
bullets = []
bullets_left = 5

player_speed = 5
player_x = 150
player_y = 230
is_jump = False
jump_count = 7
enemy1_timer = pygame.USEREVENT + 1
pygame.time.set_timer(enemy1_timer, 7000)

running = True
while running:
wnd.blit(bg, (bg_x, 0))
wnd.blit(bg, (bg_x + 635, 0))

if gameplay:
player_rect = walk_left[0].get_rect(topleft=(player_x, player_y))
if enemy_list_in_game:
for (i, el) in enumerate(enemy_list_in_game):
wnd.blit(enemy1_walk[enemy1_anim_count], el)
el.x -= 5
if el.x < -50:
enemy_list_in_game.pop(i)
if enemy1_anim_count >= 3:
enemy1_anim_count = 0
else:
enemy1_anim_count += 1

if player_rect.colliderect(el) and 230 <= player_y <= 250:


gameplay = False
finish_sound.play()
bg_sound.stop()

keys = pygame.key.get_pressed()
if keys[pygame.K_d] and player_x <= 200:
wnd.blit(walk_right[player_anim_count], (player_x, player_y))
player_x += player_speed
elif keys[pygame.K_a] and player_x >= 50:
wnd.blit(walk_left[player_anim_count], (player_x, player_y))
player_x -= player_speed
else:
wnd.blit(walk_right[player_anim_count], (player_x, player_y))

if not is_jump:
if keys[pygame.K_SPACE]:
is_jump = True
else:
if jump_count >= -7:
if jump_count > 0:
player_y -= (jump_count ** 2) / 2
else:
player_y += (jump_count ** 2) / 2
jump_count -= 1
else:
is_jump = False
jump_count = 7

if player_anim_count == 3:
player_anim_count = 0
else:
player_anim_count += 1

if bg_x == -634:
bg_x = 0
else:
bg_x -= 2

if bullets:
for (i, el) in enumerate(bullets):
wnd.blit(bullet, (el.x, el.y))
el.x += 10
if el.x > 640:
bullets.pop(i)
if enemy_list_in_game:
for (index, enemy) in enumerate(enemy_list_in_game):
if el.colliderect(enemy):
enemy_list_in_game.pop(index)
bullets.pop(i)
else:
wnd.blit(bg, (bg_x, 0))
wnd.blit(Game_over, (120, 50))
wnd.blit(Tap_to_restart, Ttr_rect)

mouse = pygame.mouse.get_pos()
if Ttr_rect.collidepoint(mouse) and pygame.mouse.get_pressed()[0]:
gameplay = True
bg_sound.play()
player_x = 150
enemy_list_in_game.clear()
bullets.clear()
bullets_left = 5

pygame.display.update()

for event in pygame.event.get():


if event.type == pygame.QUIT:
running = False
pygame.quit()
if event.type == enemy1_timer:

enemy_list_in_game.append(enemy1_walk[enemy1_anim_count].get_rect(topleft=(635,
255)))
if gameplay and keys[pygame.K_LSHIFT] and bullets_left > 0:
bullets.append(bullet.get_rect(topleft=(player_x + 40, player_y + 40)))
bullets_left -= 1
clock.tick(12)

You might also like