Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.79 KB | None | 0 0
  1. import pygame
  2. from random import randrange, choice
  3.  
  4. WIDTH = 600
  5. HEIGHT = 400
  6.  
  7. pygame.init()
  8. win = pygame.display.set_mode((WIDTH, HEIGHT))
  9. pygame.display.set_caption('Game Developer(clown)')
  10.  
  11. walkLeft = [pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/left1.png'),
  12.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/left2.png'),
  13.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/left1.png'),
  14.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/left0.png'),
  15.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/left4.png'),
  16.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/left3.png'),
  17.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/left4.png')]
  18.  
  19.  
  20. walkRight = [pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/right1.png'),
  21.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/right2.png'),
  22.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/right1.png'),
  23.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/right0.png'),
  24.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/right4.png'),
  25.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/right3.png'),
  26.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/right4.png')]
  27.  
  28. walkUp = [pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/up_1.png'),
  29.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/up_2.png'),
  30.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/up_1.png'),
  31.     pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/up_3.png')]    # TODO
  32.  
  33. playerStand = pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/stay.png')
  34. bg = pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/bg.jpg')
  35. coin = pygame.image.load('C:/Users/User/Desktop/PYTHON/pygame/dudar/sprites/coin.png')
  36.  
  37. clock = pygame.time.Clock()
  38.  
  39. width = 60
  40. height = 71
  41. x = randrange(10, WIDTH-10-width)
  42. y = HEIGHT - 5 - height
  43. speed = 5
  44. colors = [(122, 28, 255), (124, 255, 207), (127, 0, 0), (255, 106, 0), (35, 255, 15)]
  45.  
  46. left, right = False, False
  47. up, down = False, False
  48. animCount = 0
  49. lastMove = 'left'
  50.  
  51. class Bullet:
  52.     """ create new bullet for attack """
  53.     def __init__(self, x, y, R, facing, color=choice(colors)):
  54.         self.x = x
  55.         self.y = y
  56.         self.R = R
  57.         self.facing = facing
  58.         self.color = color
  59.         self.speed = 8 * self.facing
  60.  
  61.     def draw(self, win):
  62.         pygame.draw.circle(win, self.color, (self.x, self.y), self.R)      
  63.  
  64. coinX = randrange(5, WIDTH-25)
  65. coinY = randrange(36, HEIGHT-100)
  66.  
  67. def draw_on_win():
  68.     global animCount
  69.     win.blit(bg, (0, 0))
  70.     win.blit(coin, (coinX, coinY))
  71.  
  72.     if animCount+1 >= 56:
  73.         animCount = 0
  74.     if left:
  75.         win.blit(walkLeft[animCount//8], (x, y))
  76.         animCount += 2
  77.     elif right:
  78.         win.blit(walkRight[animCount//8], (x, y))
  79.         animCount += 2
  80.     elif up:
  81.         win.blit(walkUp[animCount//14], (x, y))
  82.         animCount += 2
  83.     elif down:
  84.         win.blit(walkUp[animCount//14], (x, y))
  85.         animCount += 2
  86.     else:
  87.         win.blit(playerStand, (x, y))
  88.  
  89.     # draw bullet
  90.     for bullet in bullets:
  91.         bullet.draw(win)
  92.  
  93.     pygame.display.update()
  94.  
  95. bullets = []
  96. run = True
  97. while run:
  98.     clock.tick(30)
  99.  
  100.     for event in pygame.event.get():
  101.         if event.type == pygame.QUIT:
  102.             run = False
  103.  
  104.     for bullet in bullets:
  105.         if (bullet.x - bullet.R) < 600 and (bullet.x + bullet.R) > 0:
  106.             bullet.x += bullet.speed
  107.         else:
  108.             bullets.remove(bullet)
  109.  
  110.         ''' Тут создаю совпадение монетки с пулькой'''
  111.         if bullet.x+5 >= coinX and bullet.x <= coinX+25:
  112.             if bullet.y+5 >= coinY and bullet.y <= coinY+25:
  113.                 coinX = randrange(5, WIDTH-25)
  114.                 coinY = randrange(36, HEIGHT-36)
  115.  
  116.     if event.type == pygame.MOUSEBUTTONDOWN:
  117.         if event.button == 1:
  118.             if lastMove == 'right':
  119.                 facing = 1
  120.             elif lastMove == 'left':        # TODO: lastMove for Y
  121.                 facing = -1
  122.             if len(bullets) < 10:
  123.                 bullets.append(Bullet(round(x + width//2), round(y + height//2),
  124.                               5, facing))
  125.  
  126.     keys = pygame.key.get_pressed()
  127.     if (keys[pygame.K_LEFT] or keys[pygame.K_a]) and x > speed:
  128.         x -= speed
  129.         right, up = False, False
  130.         left = True
  131.         lastMove = 'left'
  132.     elif (keys[pygame.K_RIGHT] or keys[pygame.K_d]) and x+speed+width+13 < WIDTH:
  133.         x += speed
  134.         left, up = False, False
  135.         right = True
  136.         lastMove = 'right'
  137.     elif (keys[pygame.K_UP] or keys[pygame.K_w]) and y > speed:
  138.         y -= speed//1.5
  139.         left, right = False, False
  140.         up = True
  141.     elif (keys[pygame.K_DOWN] or keys[pygame.K_s]) and y+speed+height < HEIGHT:
  142.         y += speed//1.5
  143.         left, right = False, False
  144.         up = True
  145.     else:
  146.         left, right, up, down = False, False, False, False
  147.         animCount = 0
  148.  
  149.     win.fill((0, 0, 0))
  150.     draw_on_win()
  151.  
  152. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement