forked from aosyborg/duckhunt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David
committed
Oct 2, 2011
0 parents
commit 6311276
Showing
18 changed files
with
317 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import os, sys, time | ||
import pygame | ||
import gun, duck | ||
from gun import Gun | ||
from duck import Duck | ||
|
||
class Driver(object): | ||
def __init__(self, surface): | ||
self.surface = surface | ||
self.gun = Gun(surface) | ||
self.ducks = [Duck(surface), Duck(surface)] | ||
self.round = 1 | ||
self.phase = 'shoot' | ||
self.score = 0 | ||
self.birdCount = 10 | ||
self.timer = int(time.time()) | ||
self.roundTime = 10 # Seconds in a round | ||
|
||
def handleEvent(self, event): | ||
# If we are in the shooting phase, pass event off to the gun | ||
if event.type == pygame.MOUSEMOTION: | ||
self.gun.moveCrossHairs(event.pos) | ||
elif event.type == pygame.MOUSEBUTTONDOWN: | ||
gunFired = self.gun.shoot() | ||
for duck in self.ducks: | ||
if gunFired: | ||
if (duck.isShot(event.pos)): | ||
self.score = self.score + 10 | ||
self.birdCount = self.birdCount - 1 | ||
else: | ||
duck.flyOff = True | ||
|
||
def update(self): | ||
allDone = False | ||
|
||
# Update all ducks | ||
for duck in self.ducks: | ||
duck.update() | ||
|
||
self.manageRound() | ||
|
||
|
||
def render(self): | ||
for duck in self.ducks: | ||
duck.render() | ||
self.gun.render() | ||
|
||
def manageRound(self): | ||
timer = int(time.time()) | ||
|
||
# Check round end | ||
timesUp = (timer - self.timer) > self.roundTime | ||
if not (timesUp or (self.ducks[0].isFinished and self.ducks[1].isFinished)): | ||
return | ||
|
||
# Let any remaining ducks fly off | ||
for duck in self.ducks: | ||
if not duck.isDead: | ||
duck.flyOff = True | ||
return | ||
|
||
# Populate screen with new ducks | ||
self.ducks = [Duck(self.surface), Duck(self.surface)] | ||
self.timer = int(time.time()) | ||
self.gun.reloadIt() |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import os, sys, random | ||
import pygame | ||
|
||
FRAME_SIZE = 77, 70 | ||
|
||
class Duck(object): | ||
def __init__(self, surface): | ||
self.surface = surface | ||
self.image = pygame.image.load(os.path.join('media', 'duck.png')) | ||
self.imageReversed = False | ||
self.isDead = False | ||
self.isFinished = False | ||
self.flyOff = False | ||
|
||
# Animation | ||
self.animationDelay = 10 | ||
self.frame = 0 | ||
self.animationFrame = 0 | ||
self.justShot = False | ||
|
||
# Find a starting position | ||
x = random.randint(0, 1) | ||
y = random.randint(0, surface.get_height() / 2) | ||
self.position = x, y | ||
|
||
# Find direction | ||
while True: | ||
self.dx = random.randint(-4, 4) | ||
self.dy = random.randint(-4, 4) | ||
if not self.dx == 0 and not self.dy == 0: | ||
break | ||
|
||
def update(self): | ||
self.frame = (self.frame + 1) % self.animationDelay | ||
x, y = self.position | ||
|
||
# Update position | ||
self.position = (x + self.dx), (y + self.dy) | ||
if not self.isDead: | ||
self.changeDirection() | ||
|
||
# If they have flown off they are good as dead to us | ||
frameWidth, frameHeight = FRAME_SIZE | ||
pastLeft = (x + frameWidth) < 0 | ||
pastTop = (y + frameHeight) < 0 | ||
pastRight = x > self.surface.get_width() | ||
if self.flyOff and (pastLeft or pastTop or pastRight): | ||
self.isDead = True | ||
self.isFinished = True | ||
|
||
def render(self): | ||
width, height = FRAME_SIZE | ||
x, y = self.position | ||
|
||
# Set offsets | ||
xOffset, yOffset = FRAME_SIZE | ||
if not self.imageReversed: | ||
xOffset = 0 | ||
|
||
# Only update animation on key frames | ||
if self.frame == 0: | ||
self.animationFrame = (self.animationFrame + 1) % 4 | ||
|
||
# Animate flying | ||
if not self.isDead: | ||
rect = ((width * self.animationFrame) + xOffset), 0, width, height | ||
self.surface.blit(self.image, self.position, rect) | ||
|
||
# Animate the duck drop | ||
else: | ||
if self.imageReversed: | ||
self.image = pygame.transform.flip(self.image, True, False) | ||
self.imageReversed = False | ||
|
||
# First frame is special | ||
if self.justShot: | ||
if self.frame == 0: | ||
self.justShot = False | ||
y -= self.dy | ||
self.position = (x, y) | ||
rect = (width * 4), height, width, height | ||
return self.surface.blit(self.image, self.position, rect) | ||
|
||
# Animate falling | ||
if y < (self.surface.get_height() / 2): | ||
rect = (width * 4), 0, width, height | ||
return self.surface.blit(self.image, self.position, rect) | ||
else: | ||
self.isFinished = True | ||
|
||
def isShot(self, pos): | ||
x1, y1 = self.position | ||
x2, y2 = pos | ||
frameX, frameY = FRAME_SIZE | ||
|
||
# If shot was outside the duck image | ||
if x2 < x1 or x2 > (x1 + frameX): | ||
return False | ||
if y2 < y1 or y2 > (y1 + frameY): | ||
return False | ||
|
||
# Prepare for the fall | ||
self.isDead = True | ||
self.justShot = True | ||
self.frame = 1 | ||
self.dy = 4 | ||
self.dx = 0 | ||
return True | ||
|
||
def changeDirection(self): | ||
x, y = self.position | ||
frameWidth, frameHeight = FRAME_SIZE | ||
|
||
# Only update on key frames | ||
if not self.frame == 0: | ||
return | ||
|
||
# At the left side of the screen | ||
if x <= 0 and not self.flyOff: | ||
while True: | ||
self.dx = random.randint(2, 4) | ||
self.dy = random.randint(-4, 4) | ||
if not self.dy == 0: | ||
break | ||
|
||
# At the right side of the screen | ||
elif (x + frameWidth) > self.surface.get_width() and not self.flyOff: | ||
while True: | ||
self.dx = random.randint(-4, -2) | ||
self.dy = random.randint(-4, 4) | ||
if not self.dy == 0: | ||
break | ||
|
||
# At the top of the screen | ||
elif y <= 0 and not self.flyOff: | ||
while True: | ||
self.dx = random.randint(-4, 4) | ||
self.dy = random.randint(2, 4) | ||
if not self.dx == 0: | ||
break | ||
|
||
# At the bottom of the screen | ||
elif y > (self.surface.get_height() / 2): | ||
while True: | ||
self.dx = random.randint(-4, 4) | ||
self.dy = random.randint(-4, -2) | ||
if not self.dx == 0: | ||
break | ||
|
||
# Reverse image if duck is flying opposite direction | ||
if self.dx < 0 and not self.imageReversed: | ||
self.imageReversed = True | ||
self.image = pygame.transform.flip(self.image, True, False) | ||
elif self.dx > 0 and self.imageReversed: | ||
self.imageReversed = False | ||
self.image = pygame.transform.flip(self.image, True, False) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os, sys | ||
import pygame | ||
import driver | ||
from driver import Driver | ||
|
||
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 500 | ||
TITLE = "Symons Media: Duck Hunt" | ||
CLOCK_TICK = 50 | ||
BG_COLOR = 255, 255, 255 | ||
|
||
class Game(object): | ||
def __init__(self): | ||
self.running = True | ||
self.surface = None | ||
self.clock = pygame.time.Clock() | ||
self.size = SCREEN_WIDTH, SCREEN_HEIGHT | ||
background = os.path.join('media', 'background.jpg') | ||
self.background = pygame.image.load(background) | ||
self.driver = None | ||
|
||
def init(self): | ||
pygame.init() | ||
pygame.display.set_caption(TITLE) | ||
pygame.mouse.set_visible(False) | ||
self.surface = pygame.display.set_mode(self.size) | ||
self.driver = Driver(self.surface) | ||
|
||
def handleEvent(self, event): | ||
if event.type == pygame.QUIT: | ||
self.running = False | ||
else: | ||
self.driver.handleEvent(event) | ||
|
||
def loop(self): | ||
self.clock.tick(CLOCK_TICK) | ||
self.driver.update() | ||
|
||
def render(self): | ||
self.surface.blit(self.background, (0,0)) | ||
self.driver.render() | ||
pygame.display.flip() | ||
|
||
def cleanup(self): | ||
pygame.quit() | ||
sys.exit(0) | ||
|
||
def execute(self): | ||
self.init() | ||
|
||
while (self.running): | ||
for event in pygame.event.get(): | ||
self.handleEvent(event) | ||
self.loop() | ||
self.render() | ||
|
||
self.cleanup() | ||
|
||
if __name__ == "__main__": | ||
theGame = Game() | ||
theGame.execute() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os, sys | ||
import pygame | ||
|
||
class Gun(object): | ||
def __init__(self, surface): | ||
self.surface = surface | ||
self.mouseImg = pygame.image.load(os.path.join('media', 'crosshairs.png')) | ||
self.mousePos = (0,0) | ||
self.sounds = { | ||
'blast': os.path.join('media', 'blast.mp3'), | ||
'drop': os.path.join('media', 'drop.mp3'), | ||
'hit': os.path.join('media', 'hit.mp3'), | ||
'point': os.path.join('media', 'point.mp3'), | ||
'quack': os.path.join('media', 'quack.mp3')} | ||
self.rounds = 3 | ||
|
||
def render(self): | ||
self.surface.blit(self.mouseImg, self.mousePos) | ||
|
||
def reloadIt(self): | ||
self.rounds = 3 | ||
|
||
def moveCrossHairs(self, pos): | ||
xOffset = self.mouseImg.get_width() / 2 | ||
yOffset = self.mouseImg.get_height() / 2 | ||
x, y = pos | ||
self.mousePos = (x - xOffset), (y - yOffset) | ||
|
||
def shoot(self): | ||
if self.rounds <= 0: | ||
return False | ||
|
||
pygame.mixer.music.load(self.sounds['blast']) | ||
pygame.mixer.music.play() | ||
self.rounds = self.rounds - 1 | ||
return True |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.