Advertisement
j7sx

Untitled

Jun 19th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from pygame import *
  4.  
  5. window = pygame.display.set_mode((640, 480))
  6. pygame.display.set_caption("test")
  7. screen = pygame.Surface((640, 480))
  8.  
  9. class Sprite:
  10.   def __init__ (self, xpos, ypos, filename):
  11.     self.x = xpos
  12.     self.y = ypos
  13.     self.bitmap = pygame.image.load(filename)
  14.     self.bitmap.set_colorkey((255,255,255))
  15.   def render(self):
  16.     screen.blit(self.bitmap, (self.x, self.y))
  17.  
  18. """white    = ( 255, 255, 255)
  19. class Block (pygame.sprite.Sprite):
  20.     def __init__(self, width, height):
  21.         pygame.sprite.Sprite.__init__(self)
  22.         self.image = pygame.Surface([width, height])
  23.         self.image = pygame.image.load("wood.png").convert()
  24.         self.image.set_colorkey(white)
  25.         self.rect = self.image.get_rect()   """
  26.  
  27.  
  28. def Intersect (s1_x, s2_x, s1_y, s2_y):
  29.   if (s1_x > s2_x - 24) and (s1_x < s2_x + 24) and (s1_y > s2_y - 26) and (s1_y < s2_y +24):
  30.      return 1
  31.   else:
  32.     return 0
  33.  
  34. hero = Sprite(0, 454, "hero.png")
  35. hero.go_right = True
  36. hero.go_left = True
  37.  
  38. target = Sprite(500,0, 'target.jpg')
  39. #platform = Sprite(0,0, "wood.png")
  40. img = pygame.image.load("wood.png")
  41. #blockx = Block(31,22)
  42. move_speed = 0.7
  43. altitude = 2
  44.  
  45. block = [
  46. "----x------x---x--x-",
  47. "x---------x------x--",
  48. "---x--x--x---x--x---",
  49. "---x--xxx---------xx",
  50. "-----------xx---x---",
  51. "-------x-----x------",
  52. "xxx-------xx----xx--",
  53. "-----xxx-----xx----x",
  54. "---xx-----xx-----xx-",
  55. "------xx------xx----",
  56. "---xxx----xx-----xx-",
  57. "xx------xx-----xx---",
  58. "----xx-----xx-------",
  59. "---x----xx-----x-xx-",
  60. "xxxxxx------xx------",
  61. "----------xx---xx---",
  62. "xxx----xx----x----xx",
  63. "x--xxx----xx--xx--xx",
  64. "-------xx-----------",
  65. "----------xxx----xxx",
  66. "--xx----xx-----xx---",
  67. "-----xx-----xx------"]
  68.  
  69. PLATFORM_WIDTH = 31
  70. PLATFORM_HEIGHT = 20
  71.  
  72. done =True
  73. while done:
  74.   for e in pygame.event.get():
  75.       if e.type == pygame.QUIT:
  76.           done = False
  77.  
  78.   screen.fill((0,127,0))
  79.   hero.render()
  80.   target.render()
  81.   window.blit(screen, (0,0))
  82.   x=y=0
  83.   for row in block:
  84.     for col in row:
  85.       if col == "x":
  86.         pf = Surface ((PLATFORM_WIDTH, PLATFORM_HEIGHT))
  87.         pf.blit(img, (0,0))
  88.         window.blit(pf, (x, y))
  89.        
  90.       x += PLATFORM_WIDTH
  91.     y+= PLATFORM_HEIGHT
  92.     x=0
  93.  
  94. #Движение вправо
  95.   if e.type == KEYDOWN and e.key == K_RIGHT:
  96.     if hero.x < 640 - 24:
  97.       hero.go_right = True
  98.       hero.x += move_speed
  99.       #проверка на столкновение
  100.       #if Intersect (hero.x, pf.x, hero.y, pf.y):
  101.       hero.go_right = False
  102.   else:
  103.     hero.go_right = False
  104.  
  105. #Движение влево
  106.  
  107.   if e.type == KEYDOWN and e.key == K_LEFT:
  108.     if hero.x >= 0:
  109.       hero.go_left = True
  110.       hero.x -= move_speed
  111.   else:
  112.     hero.go_left = False    
  113.  
  114.   pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement