diff --git a/HW1/test.py b/HW1/test.py deleted file mode 100644 index fd58400..0000000 --- a/HW1/test.py +++ /dev/null @@ -1,29 +0,0 @@ -# check if the worm has hit itself or the edge - if worm2Coords[HEAD2]['x'] == -1 or worm2Coords[HEAD2]['x'] == CELLWIDTH or worm2Coords[HEAD2]['y'] == -1 or worm2Coords[HEAD2]['y'] == CELLHEIGHT: - return # game over - for wormBody2 in worm2Coords[1:]: - if wormBody2['x'] == worm2Coords[HEAD2]['x'] and wormBody2['y'] == worm2Coords[HEAD2]['y']: - return # game over - - # check if worm has eaten an apply - if worm2Coords[HEAD2]['x'] == apple['x'] and worm2Coords[HEAD2]['y'] == apple['y']: - # don't remove worm's tail segment - apple = getRandomLocation() # set a new apple somewhere - elif worm2Coords[HEAD2]['x'] == apple2['x'] and worm2Coords[HEAD2]['y'] == apple2['y']: - # don't remove worm's tail segment - apple2 = getRandomLocation() # set a new apple somewhere - elif worm2Coords[HEAD2]['x'] == apple3['x'] and worm2Coords[HEAD2]['y'] == apple3['y']: - # don't remove worm's tail segment - apple3 = getRandomLocation() # set a new apple somewhere - else: - del worm2Coords[-1] # remove worm's tail segment - - # move the worm by adding a segment in the direction it is moving - if direction == UP: - newHead2 = {'x': worm2Coords[HEAD2]['x'], 'y': worm2Coords[HEAD2]['y'] - 1} - elif direction == DOWN: - newHead2 = {'x': worm2Coords[HEAD2]['x'], 'y': worm2Coords[HEAD2]['y'] + 1} - elif direction == LEFT: - newHead2 = {'x': worm2Coords[HEAD2]['x'] - 1, 'y': worm2Coords[HEAD2]['y']} - elif direction == RIGHT: - newHead2 = {'x': worm2Coords[HEAD2]['x'] + 1, 'y': worm2Coords[HEAD2]['y']} \ No newline at end of file diff --git a/HW1/wormy.py b/HW1/wormy.py index c68ce69..4ef2cb9 100644 --- a/HW1/wormy.py +++ b/HW1/wormy.py @@ -6,7 +6,13 @@ import random, pygame, sys,math from pygame.locals import * -FPS = 5 +class Bullet: + def __init__(self, direction, position): + self.direction = direction + self.position = position + + +FPS = 1 WINDOWWIDTH = 680 WINDOWHEIGHT = 520 CELLSIZE = 20 @@ -32,7 +38,6 @@ LEFT = 'left' RIGHT = 'right' HEAD = 0 # syntactic sugar: index of the worm's head -HEAD2 = 0 def main(): global FPSCLOCK, DISPLAYSURF, BASICFONT @@ -59,18 +64,27 @@ def runGame(): worm2Coords = [{'x': startx - 2, 'y': starty}, {'x': startx - 2, 'y': starty - 1}, - {'x': startx - 2, 'y': starty - 2}] + {'x': startx - 2, 'y': starty - 2}, + {'x': startx - 2, 'y': starty - 3}, + {'x': startx - 2, 'y': starty - 4}, + {'x': startx - 2, 'y': starty - 5}, + {'x': startx - 2, 'y': starty - 6}] + bullets = [] + stones = [] direction = DOWN direction2 = DOWN # Start the apple in a random place. - apple = getRandomLocation() - apple2 = getRandomLocation() - apple3 = getRandomLocation() + apples = [] + i = 0 + while i < 10: + apples.append(getRandomLocation()) + i+=1 while True: # main game loop + for event in pygame.event.get(): # event handling loop if event.type == QUIT: terminate() @@ -83,6 +97,16 @@ def runGame(): direction = UP elif (event.key == K_DOWN) and direction != UP: direction = DOWN + elif event.key == K_RSHIFT : + if direction == UP: + bullet = Bullet(direction, {'x': wormCoords[HEAD]['x'], 'y': wormCoords[HEAD]['y'] - 1}) + elif direction == DOWN: + bullet = Bullet(direction, {'x': wormCoords[HEAD]['x'], 'y': wormCoords[HEAD]['y'] + 1}) + elif direction == LEFT: + bullet = Bullet(direction, {'x': wormCoords[HEAD]['x'] - 1, 'y': wormCoords[HEAD]['y']}) + elif direction == RIGHT: + bullet = Bullet(direction, {'x': wormCoords[HEAD]['x'] + 1, 'y': wormCoords[HEAD]['y']}) + bullets.append(bullet) if (event.key == K_a) and direction2 != RIGHT: direction2 = LEFT @@ -92,31 +116,65 @@ def runGame(): direction2 = UP elif (event.key == K_s) and direction2 != UP: direction2 = DOWN + elif event.key == K_LSHIFT: + if direction == UP: + bullet = Bullet(direction, {'x': worm2Coords[HEAD]['x'], 'y': worm2Coords[HEAD]['y'] - 1}) + elif direction == DOWN: + bullet = Bullet(direction, {'x': worm2Coords[HEAD]['x'], 'y': worm2Coords[HEAD]['y'] + 1}) + elif direction == LEFT: + bullet = Bullet(direction, {'x': worm2Coords[HEAD]['x'] - 1, 'y': worm2Coords[HEAD]['y']}) + elif direction == RIGHT: + bullet = Bullet(direction, {'x': worm2Coords[HEAD]['x'] + 1, 'y': worm2Coords[HEAD]['y']}) + bullets.append(bullet) elif event.key == K_ESCAPE: terminate() + + + # check if bullets have hit a worm + for bullet in bullets: + if (bullet.position in wormCoords): + index = wormCoords.index(bullet.position) + if (index == HEAD): + return # game over + else: + stones.extend(wormCoords[index:]) + wormCoords = wormCoords[:index] + for bullet in bullets: + if (bullet.position in worm2Coords): + index = worm2Coords.index(bullet.position) + if (index == HEAD): + return # game over + else: + stones.extend(worm2Coords[index:]) + worm2Coords = worm2Coords[:index] + # check if the worm has hit itself or the edge if wormCoords[HEAD]['x'] == -1 or wormCoords[HEAD]['x'] == CELLWIDTH or wormCoords[HEAD]['y'] == -1 or wormCoords[HEAD]['y'] == CELLHEIGHT: return # game over - for wormBody in wormCoords[1:]: - if wormBody['x'] == wormCoords[HEAD]['x'] and wormBody['y'] == wormCoords[HEAD]['y']: - return # game over - for wormBody2 in worm2Coords[1:]: - if wormBody2['x'] == wormCoords[HEAD]['x'] and wormBody['y'] == wormCoords[HEAD]['y']: - return # game over + if wormCoords[HEAD] in wormCoords[1:]: + return + if wormCoords[HEAD] in worm2Coords[0:]: + return + if worm2Coords[HEAD] in worm2Coords[1:]: + return + if worm2Coords[HEAD] in wormCoords[0:]: + return + if worm2Coords[HEAD] in stones[0:]: + return + if wormCoords[HEAD] in stones[0:]: + return - # check if worm has eaten an apply - if wormCoords[HEAD]['x'] == apple['x'] and wormCoords[HEAD]['y'] == apple['y']: - # don't remove worm's tail segment - apple = getRandomLocation() # set a new apple somewhere - elif wormCoords[HEAD]['x'] == apple2['x'] and wormCoords[HEAD]['y'] == apple2['y']: - # don't remove worm's tail segment - apple2 = getRandomLocation() # set a new apple somewhere - elif wormCoords[HEAD]['x'] == apple3['x'] and wormCoords[HEAD]['y'] == apple3['y']: - # don't remove worm's tail segment - apple3 = getRandomLocation() # set a new apple somewhere + # check if worm has eaten an apply + if wormCoords[HEAD] in apples: + apples[apples.index(wormCoords[HEAD])] = getRandomLocation() else: del wormCoords[-1] # remove worm's tail segment + if worm2Coords[HEAD] in apples: + apples[apples.index(worm2Coords[HEAD])] = getRandomLocation() + else: + del worm2Coords[-1] + # move the worm by adding a segment in the direction it is moving if direction == UP: @@ -130,47 +188,41 @@ def runGame(): # check if the worm has hit itself or the edge - if worm2Coords[HEAD2]['x'] == -1 or worm2Coords[HEAD2]['x'] == CELLWIDTH or worm2Coords[HEAD2]['y'] == -1 or \ - worm2Coords[HEAD2]['y'] == CELLHEIGHT: + if worm2Coords[HEAD]['x'] == -1 or worm2Coords[HEAD]['x'] == CELLWIDTH or worm2Coords[HEAD]['y'] == -1 or \ + worm2Coords[HEAD]['y'] == CELLHEIGHT: return # game over for wormBody2 in worm2Coords[1:]: - if wormBody2['x'] == worm2Coords[HEAD2]['x'] and wormBody2['y'] == worm2Coords[HEAD2]['y']: + if wormBody2['x'] == worm2Coords[HEAD]['x'] and wormBody2['y'] == worm2Coords[HEAD]['y']: return # game over for wormBody in wormCoords[1:]: - if wormBody['x'] == worm2Coords[HEAD2]['x'] and wormBody['y'] == worm2Coords[HEAD2]['y']: + if wormBody['x'] == worm2Coords[HEAD]['x'] and wormBody['y'] == worm2Coords[HEAD]['y']: return # game over - # check if worm has eaten an apply - if worm2Coords[HEAD2]['x'] == apple['x'] and worm2Coords[HEAD2]['y'] == apple['y']: - # don't remove worm's tail segment - apple = getRandomLocation() # set a new apple somewhere - elif worm2Coords[HEAD2]['x'] == apple2['x'] and worm2Coords[HEAD2]['y'] == apple2['y']: - # don't remove worm's tail segment - apple2 = getRandomLocation() # set a new apple somewhere - elif worm2Coords[HEAD2]['x'] == apple3['x'] and worm2Coords[HEAD2]['y'] == apple3['y']: - # don't remove worm's tail segment - apple3 = getRandomLocation() # set a new apple somewhere - else: - del worm2Coords[-1] # remove worm's tail segment - # move the worm by adding a segment in the direction it is moving + + + # move the worm by adding a segment in the direction it is moving if direction2 == UP: - newHead2 = {'x': worm2Coords[HEAD2]['x'], 'y': worm2Coords[HEAD2]['y'] - 1} + newHead2 = {'x': worm2Coords[HEAD]['x'], 'y': worm2Coords[HEAD]['y'] - 1} elif direction2 == DOWN: - newHead2 = {'x': worm2Coords[HEAD2]['x'], 'y': worm2Coords[HEAD2]['y'] + 1} + newHead2 = {'x': worm2Coords[HEAD]['x'], 'y': worm2Coords[HEAD]['y'] + 1} elif direction2 == LEFT: - newHead2 = {'x': worm2Coords[HEAD2]['x'] - 1, 'y': worm2Coords[HEAD2]['y']} + newHead2 = {'x': worm2Coords[HEAD]['x'] - 1, 'y': worm2Coords[HEAD]['y']} elif direction2 == RIGHT: - newHead2 = {'x': worm2Coords[HEAD2]['x'] + 1, 'y': worm2Coords[HEAD2]['y']} + newHead2 = {'x': worm2Coords[HEAD]['x'] + 1, 'y': worm2Coords[HEAD]['y']} wormCoords.insert(0, newHead) #have already removed the last segment worm2Coords.insert(0, newHead2) #have already removed the last segment DISPLAYSURF.fill(BGCOLOR) + drawGrid() + for num, bullet in enumerate(bullets): + bullets[num] = drawBullet(bullet) + for stone in stones: + drawStone(stone) drawWorm(wormCoords) drawWorm(worm2Coords) - drawApple(apple) - drawApple(apple2) - drawApple(apple3) + for apple in apples: + drawApple(apple) drawScore(len(wormCoords) - 3, len(worm2Coords) - 3) pygame.display.update() FPSCLOCK.tick(FPS) @@ -181,7 +233,6 @@ def drawPressKeyMsg(): pressKeyRect.topleft = (WINDOWWIDTH - 200, WINDOWHEIGHT - 30) DISPLAYSURF.blit(pressKeySurf, pressKeyRect) - def checkForKeyPress(): if len(pygame.event.get(QUIT)) > 0: terminate() @@ -280,6 +331,31 @@ def drawApple(coord): #pygame.draw.rect(DISPLAYSURF, RED, appleRect) pygame.draw.circle(DISPLAYSURF, RED,(xcenter,ycenter),RADIUS) +def drawStone(stone): + x = stone['x'] * CELLSIZE + y = stone['y'] * CELLSIZE + xcenter = stone['x'] * CELLSIZE + math.floor(CELLSIZE/2) + ycenter = stone['y'] * CELLSIZE+ math.floor(CELLSIZE/2) + #appleRect = pygame.Rect(x, y, CELLSIZE, CELLSIZE) + #pygame.draw.rect(DISPLAYSURF, RED, appleRect) + pygame.draw.circle(DISPLAYSURF, DARKGRAY,(xcenter,ycenter),RADIUS) + +def drawBullet(bullet): + direction = bullet.direction + if direction == UP: + bullet.position['y'] -= 1 + elif direction == DOWN: + bullet.position['y'] += 1 + elif direction == LEFT: + bullet.position['x'] -= 1 + elif direction == RIGHT: + bullet.position['x'] += 1 + xcenter = bullet.position["x"] * CELLSIZE + math.floor(CELLSIZE / 2) + ycenter = bullet.position["y"] * CELLSIZE + math.floor(CELLSIZE / 2) + pygame.draw.circle(DISPLAYSURF, YELLOW,(int(xcenter),int(ycenter)),int(RADIUS)) + + return bullet + def drawGrid(): for x in range(0, WINDOWWIDTH, CELLSIZE): # draw vertical lines