Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, pygame, math,os,random
- from pygame.locals import *
- pygame.init()
- size = width, height = 1000, 700
- screen = pygame.display.set_mode(size)
- # set up a bunch of constants
- WHITE = 255,255,255
- BLACK = ( 0, 0, 0)
- BROWN = (139, 69, 19)
- DARKGRAY = (128, 128, 128)
- bullets =[]
- reload = 0
- speed = 0
- walls=[]
- BGCOLOR = WHITE
- BULLETSPEED = 10
- ballrect = pygame.Rect(100,50, 10, 10)
- keys=pygame.key.get_pressed()
- class Wall:
- def __init__(self, pos):
- walls.append(self)
- self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
- class Bullet:
- def __init__(self,pos,speed,size):
- self.pos = pos
- self.speed = speed
- self.size = size
- def move(self):
- self.pos[0] = int(self.pos[0] + self.speed[0])
- self.pos[1] = int(self.pos[1] + self.speed[1])
- # standard pygame setup code
- pygame.init()
- pygame.display.set_caption('Bouncer')
- # create image
- cannonSurf = pygame.Surface((100, 100))
- cannonSurf.fill(BGCOLOR)
- pygame.draw.circle(cannonSurf, BLACK, (50, 50), 15) # hole
- ############################Common knowledge, use of atan2##########################################
- '''Thanks to a stupid tiny rounding error since pygame doesn't have infinite precision
- I had to use vectors the small 0.1 pixel error would acumulate because of the accumulation by adding the speed'''
- def getUnitVector(x1, y1, x2, y2):
- delx = x2 - x1
- dely = y2 - y1
- m = math.sqrt(delx * delx + dely * dely)
- unit = (delx / m, dely / m)
- return unit
- level = [
- "WWWWWWWWWWWWWWWWWWWW",
- "W W",
- "W WWWWWW W",
- "W WWWW W W",
- "W W WWWW W",
- "W WWW WWWW W",
- "W W W W W",
- "W W W WWW WW",
- "W WWW WWW W W W",
- "W W W W W W",
- "WWW W WWWWW W W",
- "W W WW W",
- "W W WWWW WWW W",
- "W W E W W",
- "WWWWWWWWWWWWWWWWWWWW",
- ]
- # Parse the level string above. W = wall, E = exit
- x = y = 0
- for row in level:
- for col in row:
- if col == "W":
- Wall((x, y))
- if col == "E":
- end_rect = pygame.Rect(x, y, 16, 16)
- x += 16
- y += 16
- x = 0
- while True:
- for event in pygame.event.get():
- if event.type == QUIT or keys[pygame.K_ESCAPE]:
- pygame.quit()
- sys.exit()
- # fill the screen to draw from a blank state
- screen.fill(BGCOLOR)
- # draw the cannons pointed at the mouse cursor
- mousex, mousey = pygame.mouse.get_pos()
- startx = 500
- starty = 200
- aim = getUnitVector(startx, starty, mousex, mousey)
- # rotate a copy of the cannon image and draw it
- rotatedSurf = pygame.transform.rotate(cannonSurf,aim)
- rotatedRect = rotatedSurf.get_rect()
- rotatedRect.center = (startnx, start)
- screen.blit(rotatedSurf, rotatedRect)
- if pygame.mouse.get_pressed()[0]:
- if reload>10:
- bx = BULLETSPEED * aim[0]
- by = BULLETSPEED * aim[1]
- bullet = Bullet([startx,starty], [bx,-by],10)
- bullets.append(bullet)
- reload=0
- reload = reload + 1
- # draw the cross hairs over the mouse
- pygame.draw.line(screen, BLACK, (mousex -10, mousey), (mousex + 10, mousey))
- pygame.draw.line(screen, BLACK, (mousex, mousey - 10), (mousex, mousey + 10))
- for wall in walls:
- pygame.draw.rect(screen, (100, 100, 200), wall.rect)
- pygame.draw.rect(screen, (255, 0, 0), end_rect)
- #Bullet rect operations
- for b in bullets:
- b.move()
- ballrect = pygame.Rect(b.pos[0],b.pos[1],b.size,b.size)
- '''if ballrect.top < 0 or ballrect.bottom > height:
- b.speed[1] = -b.speed[1]
- if ballrect.right > width or ballrect.left < 0:
- b.speed[0] = -b.speed[0]'''
- # pygame.draw.circle(screen, BLACK, ballrect.center,b.size, 0)
- pygame.draw.circle(screen, BLACK, b.pos, 1, 0)
- pygame.display.flip()
- pygame.time.wait(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement