Advertisement
Guest User

Untitled

a guest
Dec 21st, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. import sys, pygame, math,os,random
  2. from pygame.locals import *
  3. pygame.init()
  4. size = width, height = 1000, 700
  5. screen = pygame.display.set_mode(size)
  6. # set up a bunch of constants
  7. WHITE = 255,255,255
  8. BLACK = ( 0, 0, 0)
  9. BROWN = (139, 69, 19)
  10. DARKGRAY = (128, 128, 128)
  11. bullets =[]
  12. reload = 0
  13. speed = 0
  14. walls=[]
  15. BGCOLOR = WHITE
  16. BULLETSPEED = 10
  17. ballrect = pygame.Rect(100,50, 10, 10)
  18. keys=pygame.key.get_pressed()
  19. class Wall:
  20. def __init__(self, pos):
  21. walls.append(self)
  22. self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
  23. class Bullet:
  24. def __init__(self,pos,speed,size):
  25. self.pos = pos
  26. self.speed = speed
  27. self.size = size
  28. def move(self):
  29. self.pos[0] = int(self.pos[0] + self.speed[0])
  30. self.pos[1] = int(self.pos[1] + self.speed[1])
  31. # standard pygame setup code
  32. pygame.init()
  33. pygame.display.set_caption('Bouncer')
  34.  
  35. # create image
  36. cannonSurf = pygame.Surface((100, 100))
  37. cannonSurf.fill(BGCOLOR)
  38. pygame.draw.circle(cannonSurf, BLACK, (50, 50), 15) # hole
  39. ############################Common knowledge, use of atan2##########################################
  40. '''Thanks to a stupid tiny rounding error since pygame doesn't have infinite precision
  41. I had to use vectors the small 0.1 pixel error would acumulate because of the accumulation by adding the speed'''
  42. def getUnitVector(x1, y1, x2, y2):
  43. delx = x2 - x1
  44. dely = y2 - y1
  45. m = math.sqrt(delx * delx + dely * dely)
  46. unit = (delx / m, dely / m)
  47. return unit
  48. level = [
  49. "WWWWWWWWWWWWWWWWWWWW",
  50. "W W",
  51. "W WWWWWW W",
  52. "W WWWW W W",
  53. "W W WWWW W",
  54. "W WWW WWWW W",
  55. "W W W W W",
  56. "W W W WWW WW",
  57. "W WWW WWW W W W",
  58. "W W W W W W",
  59. "WWW W WWWWW W W",
  60. "W W WW W",
  61. "W W WWWW WWW W",
  62. "W W E W W",
  63. "WWWWWWWWWWWWWWWWWWWW",
  64. ]
  65.  
  66. # Parse the level string above. W = wall, E = exit
  67. x = y = 0
  68. for row in level:
  69. for col in row:
  70. if col == "W":
  71. Wall((x, y))
  72. if col == "E":
  73. end_rect = pygame.Rect(x, y, 16, 16)
  74. x += 16
  75. y += 16
  76. x = 0
  77.  
  78. while True:
  79. for event in pygame.event.get():
  80. if event.type == QUIT or keys[pygame.K_ESCAPE]:
  81. pygame.quit()
  82. sys.exit()
  83.  
  84. # fill the screen to draw from a blank state
  85. screen.fill(BGCOLOR)
  86.  
  87. # draw the cannons pointed at the mouse cursor
  88. mousex, mousey = pygame.mouse.get_pos()
  89. startx = 500
  90. starty = 200
  91.  
  92. aim = getUnitVector(startx, starty, mousex, mousey)
  93. # rotate a copy of the cannon image and draw it
  94. rotatedSurf = pygame.transform.rotate(cannonSurf,aim)
  95. rotatedRect = rotatedSurf.get_rect()
  96. rotatedRect.center = (startnx, start)
  97. screen.blit(rotatedSurf, rotatedRect)
  98. if pygame.mouse.get_pressed()[0]:
  99. if reload>10:
  100. bx = BULLETSPEED * aim[0]
  101. by = BULLETSPEED * aim[1]
  102. bullet = Bullet([startx,starty], [bx,-by],10)
  103. bullets.append(bullet)
  104. reload=0
  105. reload = reload + 1
  106. # draw the cross hairs over the mouse
  107. pygame.draw.line(screen, BLACK, (mousex -10, mousey), (mousex + 10, mousey))
  108. pygame.draw.line(screen, BLACK, (mousex, mousey - 10), (mousex, mousey + 10))
  109. for wall in walls:
  110. pygame.draw.rect(screen, (100, 100, 200), wall.rect)
  111. pygame.draw.rect(screen, (255, 0, 0), end_rect)
  112.  
  113. #Bullet rect operations
  114. for b in bullets:
  115. b.move()
  116. ballrect = pygame.Rect(b.pos[0],b.pos[1],b.size,b.size)
  117. '''if ballrect.top < 0 or ballrect.bottom > height:
  118. b.speed[1] = -b.speed[1]
  119. if ballrect.right > width or ballrect.left < 0:
  120. b.speed[0] = -b.speed[0]'''
  121. # pygame.draw.circle(screen, BLACK, ballrect.center,b.size, 0)
  122. pygame.draw.circle(screen, BLACK, b.pos, 1, 0)
  123. pygame.display.flip()
  124. pygame.time.wait(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement