Advertisement
j7sx

pygame

Jul 20th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from pygame import *
  4.  
  5. pygame.init()
  6. size = width, height = 640, 480
  7.  
  8. window = pygame.display.set_mode(size)
  9. pygame.display.set_caption('hello, pygame!')
  10.  
  11. screen = pygame.Surface((640,480))
  12.  
  13. class Sprite:
  14.   def __init__(self, xpos, ypos, filename):
  15.     self.x = xpos
  16.     self.y = ypos
  17.     self.bitmap = pygame.image.load(filename)
  18.     self.bitmap.set_colorkey((255,255,255))
  19.   def render(self):
  20.     screen.blit(self.bitmap, (self.x, self.y))
  21.  
  22.  
  23. def Intersect (x1, x2, y1, y2):
  24.   if (x1>x2-32) and (x1<x2+32) and (y1>y2-32) and (y1<y2+32):
  25.     return 1
  26.   else:
  27.     return 0
  28.  
  29.  
  30. #PLATFORM_COLOR = (0, 45, 16)
  31.  
  32. level = [
  33. '------*---',
  34. '----------',
  35. '---------*',
  36. '*---*-----',
  37. '------*---',
  38. '-----*----',
  39. '-------*--',
  40. '---*--*---',
  41. '*----*--*-',
  42. '----------',
  43. '-*---*----',
  44. '---*------',
  45. '----*-----',
  46. '----------',
  47. '---*------',
  48. '-------*--',
  49. '-----*----',
  50. '--*-------',
  51. '------*-*-',
  52. '---*------']
  53.  
  54. hero = Sprite(0.0, 448.0, "rsz_hero.png")
  55.  
  56. hero.go_right = True
  57. hero.go_left = True
  58. hero.go_down = True
  59. hero.go_up = True
  60. hero.jump = True
  61. hero.move_speed = 0.7
  62. hero.jump_altitude = 0.5
  63. hero.gravity = -0.2
  64.  
  65. target = Sprite(300.0, 0.0, "target.png")
  66.  
  67. platform = Sprite(0,0, "ground.png")
  68. #platform = pygame.image.load("ground.png")
  69.  
  70. done = True
  71. while done:
  72.  for e in pygame.event.get():
  73.   if e.type == pygame.QUIT:
  74.    done = False
  75.    
  76.  screen.fill((0,40,58))
  77.  
  78.  x=y=0
  79.  width = 64
  80.  height = 20
  81.  for row in level:
  82.   for col in row:
  83.     if col == '*':
  84.       platform.render()
  85.       #pf = pygame.Surface((PLATFORM_WIDTH,PLATFORM_HEIGHT))
  86.       #pf.fill(PLATFORM_COLOR)
  87.       #pf.blit(platform, (0,0))
  88.       #screen.blit(pf, (x,y))
  89.     x += width
  90.   y += height
  91.   x = 0      
  92.  
  93. #движение вправо
  94.  if e.type == KEYDOWN and e.key == K_RIGHT:
  95.    if hero.x > 640 - 32:
  96.     hero.go_right = False
  97.    else:
  98.     hero.go_right = True
  99.     hero.x += hero.move_speed
  100.  
  101. #движение влево  
  102.  if e.type == KEYDOWN and e.key == K_LEFT:
  103.   if hero.x <= 0:
  104.     hero.go_left = False
  105.   else:
  106.     hero.go_left = True
  107.     hero.x -= hero.move_speed
  108.  
  109. #движение вверх
  110.  if e.type == KEYDOWN and e.key == K_SPACE:
  111.   if hero.y < 480 and hero.y > 0:
  112.     hero.jump = True
  113.     hero.y -= hero.jump_altitude
  114.     if hero.y !=0:
  115.       hero.y = hero.gravity
  116.   else:
  117.     hero.jump = False  
  118.  
  119.  
  120.  if Intersect (target.x, hero.x, target.y, hero.y) == True:
  121.   hero.go_right = True
  122.   hero.go_down = True
  123.   hero.x += 0.00000001
  124.   hero.y += 0.00000001
  125.  
  126.  hero.render()
  127.  target.render()
  128.  
  129.  window.blit(screen, (0,0))
  130.  pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement