worm.py sample rewritten;

Minor fix in sf.SoundStream;
Updated sf.Image.


git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1019 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
remi-k 2009-02-22 14:57:18 +00:00
parent e580c8cd63
commit 49c7769273
4 changed files with 258 additions and 216 deletions

View File

@ -1,240 +1,275 @@
#!/usr/bin/python #!/usr/bin/python
from PySFML import * from PySFML import sf
import math import math
import random import random
import sys
class Menu:
def __init__(self, screen_width, screen_height):
self.selection = 0
text_color = sf.Color(220, 220, 20, 255)
self.spacing = screen_height/7
self.title = sf.String("PyWorm!")
self.title.SetColor(text_color)
self.title.SetPosition(screen_width/2-80., self.spacing)
levels = ["Very Easy", "Easy", "Medium", "Hard"]
x_align = [-80., -50., -70., -50.]
self.strings = []
for i in range(0, 4):
string = sf.String(levels[i])
string.SetColor(text_color)
string.SetPosition(screen_width/2+x_align[i], (2+i)*self.spacing+20)
self.strings.append(string)
self.rectangle = sf.Shape.Rectangle(0, 0, screen_width, 40, sf.Color(50, 50, 10))
def next_frame(self, win):
self.rectangle.SetY(self.spacing*(2 + self.selection)+20)
win.Draw(self.rectangle)
win.Draw(self.title)
win.Draw(self.strings)
def key_up(self, pressed):
if pressed:
self.selection = (self.selection - 1) % 4
def key_down(self, pressed):
if pressed:
self.selection = (self.selection + 1) % 4
def Game(Difficulty):
PartsPerFrame = 1 + Difficulty # Number of drawn base parts each frame class Apple(sf.Sprite):
PartsSpacing = 3 # Each worm's base part is separated by PartsSpacing pixels def __init__(self):
TurnStep = 0.15 # Turn the worm's head of 0.15 rad apple_img = sf.Image() # Apple's image
if not apple_img.LoadFromFile("./data/apple.png"):
print "Could not load data/apple.png"
sf.Sprite.__init__(self, apple_img)
self.SetCenter(apple_img.GetWidth()/2, apple_img.GetHeight()/2)
self.size = apple_img.GetWidth()
PartSize = 6.0 # worm's base part size for collision def random_move(self, arena):
PartRealSize = 18.0 # worm's real base part size for drawing self.SetPosition( \
random.randrange(arena.arena_left+arena.border, arena.arena_right-arena.border), \
# Load images random.randrange(arena.arena_top+arena.border, arena.arena_bottom-arena.border) \
Rond = sf.Image() # Image containing the base part of the worm )
if not Rond.LoadFromFile("./data/rond2.png"):
print "Could not load data/rond2.png"
return
WormPart = sf.Sprite(Rond)
WormPart.SetCenter(Rond.GetWidth()/2, Rond.GetHeight()/2)
AppleImg = sf.Image() # Apple's image
if not AppleImg.LoadFromFile("./data/apple.png"):
print "Could not load data/apple.png"
return
Apple = sf.Sprite(AppleImg, 0, 0, 1, 1, 0) # Corresponding sprite
Black = sf.Color(0,0,0,255)
UglyYellow = sf.Color(220, 220, 20, 255)
Stop = False
Event = sf.Event() # Our events manager
Level = 0
ShrinkValue = 20
Border = 30
ArenaTop = 20
ArenaBottom = 520
RequiredLength = 300
ExitLeft = 350
ExitRight = 450
ExitImg = sf.Image(ExitRight-ExitLeft, ArenaTop, Black)
Exit = sf.Sprite(ExitImg, ExitLeft, 0, 1, 1, 0)
Score = 0
HeadX, HeadY = 0, 0
while not Stop:
#Initialize a new game
Level += 1
ArenaLeft = ShrinkValue*Level
ArenaRight = 800-ShrinkValue*Level
ArenaImg = sf.Image(ArenaRight-ArenaLeft, ArenaBottom-ArenaTop, Black)
Arena = sf.Sprite(ArenaImg, ArenaLeft, ArenaTop, 1, 1, 0)
AppleX, AppleY = random.randrange(ArenaLeft+Border, ArenaRight-Border), random.randrange(ArenaTop+Border, ArenaBottom-Border)
Apple.SetX(AppleX - AppleImg.GetWidth()/2) # We move the apple to somewhere else, randomly
Apple.SetY(AppleY - AppleImg.GetHeight()/2)
Crash = False
Running = True
LevelStr = sf.String("Level: " + str(Level))
LevelStr.SetPosition(60., 540.)
LevelStr.SetColor(UglyYellow)
ScoreStr = sf.String("Score: 0")
ScoreStr.SetPosition(260., 540.)
ScoreStr.SetColor(UglyYellow)
Length = 1
TargetedLength = 30
Worm = [[ArenaLeft+50., ArenaTop+50.]]
Angle = 0
i = 0
Dir = 0
while Running: # Game main loop
while App.GetEvent(Event): # Event Handler
if Event.Type == sf.Event.Closed:
App.Close()
return
if Event.Type == sf.Event.KeyPressed:
if Event.Key.Code == sf.Key.Escape:
Running = False
Stop = True
if Event.Key.Code == sf.Key.Left:
Dir = -1
if Event.Key.Code == sf.Key.Right:
Dir = 1
if Crash and Length<=1:
Running = False
if Event.Type == sf.Event.KeyReleased:
if Event.Key.Code == sf.Key.Left and Dir == -1:
Dir = 0
if Event.Key.Code == sf.Key.Right and Dir == 1:
Dir = 0
App.Draw(Arena)
if not Crash: # Create new parts and check collisions if the worm hasn't crashed yet class Arena(dict):
for i in range(0, PartsPerFrame): # We create PartsPerFrame Worm's parts shrink_value, border, arena_top = 20, 30, 20
Angle += Dir*TurnStep def __init__(self, window_width, window_height):
HeadX, HeadY = Worm[Length-1][0]+PartsSpacing*math.cos(Angle), Worm[Length-1][1]+PartsSpacing*math.sin(Angle) self.window_width = window_width
if TargetedLength <= RequiredLength: self.arena_bottom, self.exit_left, self.exit_right = window_height-80, window_width/2 - 50, window_width/2 + 50
if math.sqrt ( (AppleX - HeadX)**2 + (AppleY - HeadY)**2 ) < 14 + PartSize/2: # The Worm ate the apple self['level_str'] = sf.String()
Score += 1 self['level_str'].SetColor(sf.Color.White)
TargetedLength += 20 # The worm gets longer self['level_str'].SetPosition(60., window_height-60)
if TargetedLength <= RequiredLength: self['score_str'] = sf.String()
AppleX, AppleY = random.randrange(ArenaLeft+Border, ArenaRight-Border), random.randrange(ArenaTop+Border, ArenaBottom-Border) self['score_str'].SetColor(sf.Color.White)
Apple.SetX(AppleX - AppleImg.GetWidth()/2) # We move the apple to somewhere else, randomly self['score_str'].SetPosition(260., window_height-60)
Apple.SetY(AppleY - AppleImg.GetHeight()/2) self.exit_rect = sf.Shape.Rectangle(self.exit_left, 0, self.exit_right, self.arena_top, sf.Color.Black)
App.Draw(Apple) self.reset()
self.update_arena_rect()
if HeadX<ArenaLeft+PartSize/2 or HeadX>ArenaRight-PartSize/2 or HeadY<ArenaTop+PartSize/2 or HeadY>ArenaBottom-PartSize/2: # Crash into a wall def update_arena_rect(self):
if Length > RequiredLength: self['arena_rect'] = sf.Shape.Rectangle(self.arena_left, self.arena_top, self.arena_right, self.arena_bottom, sf.Color.Black)
if HeadY<ArenaTop+PartSize/2:
if HeadX<ExitLeft+PartSize/2 or HeadX>ExitRight-PartSize/2:
Crash = True
elif HeadY < 0:
Length = 0
Running = False # Level completed!
else:
Crash = True
elif Running:
Crash = True
if not Crash:
Worm.append([HeadX, HeadY])
Length += 1
def reset(self):
self.level, self.score, self.arena_left, self.arena_right = 1, 0, self.shrink_value, self.window_width-self.shrink_value
self.update_arena_rect()
self['level_str'].SetText("Level: 1")
self['score_str'].SetText("Score: 0")
if TargetedLength > RequiredLength: def update_score(self):
App.Draw(Exit) self.score += 1
self['score_str'].SetText("Score: " + str(self.score))
if Length >= TargetedLength: def next_level(self):
Worm[0:TargetedLength] = Worm[Length-TargetedLength:Length] self.level += 1
for i in range(Length, TargetedLength): self['level_str'].SetText("Level: " + str(self.level))
del Worm[i] self.arena_left += self.shrink_value
Worm[TargetedLength:Length] = [] self.arena_right -= self.shrink_value
Length = TargetedLength self.update_arena_rect()
self.score += 4
self.update_score()
for i in range(0, Length): class Part(sf.Sprite):
WormPart.SetPosition(Worm[i][0], Worm[i][1]) def __init__(self, rond, x, y):
App.Draw(WormPart) # Draw the part on screen sf.Sprite.__init__(self, rond)
if i < Length - PartSize/PartsSpacing - 1: self.SetCenter(rond.GetWidth()/2, rond.GetHeight()/2)
if math.sqrt( (HeadX-Worm[i][0])**2 + (HeadY-Worm[i][1])**2 ) < PartSize and Running: # Check for collision self.SetPosition(x, y)
Crash = True
if Crash and Length>0: class Worm(list):
TargetedLength -= PartsPerFrame parts_spacing, turn_step, part_size, start_x, start_y, required_length, grow_length = 3, 0.15, 6.0, 50., 50., 300, 20
def __init__(self, difficulty):
self.parts_per_frame = 1 + difficulty
self.angle = 0
self.direction = 0 # 0, 1 or -1 according to the key pressed
self.rond = sf.Image()
self.level_completed = False
if not self.rond.LoadFromFile("./data/rond2.png"):
print "Could not load data/rond2.png"
ScoreStr.SetText("Score: " + str(Score)) def reset(self, arena):
self.targeted_length, self.angle, self.direction = 30, 0, 0
self[:] = [Part(self.rond, arena.arena_left+self.start_x, arena.arena_top+self.start_y)]
App.Draw(ScoreStr) def left(self, pressed):
App.Draw(LevelStr) if pressed:
App.Display() # Refresh Screen self.direction = -1
App.Clear(BGColor) elif self.direction == -1:
self.direction = 0
def right(self, pressed):
# End of the game if pressed:
if Crash: self.direction = 1
Level = 0 elif self.direction == 1:
Score = 0 self.direction = 0
def restart(self, arena):
if self.targeted_length == 0 and not self.level_completed:
arena.reset()
self.reset(arena)
return True
else: else:
Score += 5 # End level bonus return False
del Worm def crash(self):
del Arena self.targeted_length = 0
del ArenaImg
def Menu(): def move(self, arena, apple):
head_x, head_y = -1, -1
if self.is_running(): # Create new parts and check collisions if the worm hasn't crashed yet
for i in range(self.parts_per_frame): # We create PartsPerFrame Worm's parts
self.angle += self.direction*self.turn_step
head_x, head_y = self[-1].GetPosition()
head_x += self.parts_spacing*math.cos(self.angle)
head_y += self.parts_spacing*math.sin(self.angle)
if self.is_running() and self.targeted_length <= self.required_length: # let's check if the worm ate the apple
if math.hypot(apple.GetPosition()[0] - head_x, apple.GetPosition()[1] - head_y) < apple.size/2 + self.part_size/2: # Yes it did
arena.update_score()
self.targeted_length += self.grow_length # The worm gets longer
apple.random_move(arena)
if head_x<arena.arena_left+self.part_size/2 or head_x>arena.arena_right-self.part_size/2 or head_y<arena.arena_top+self.part_size/2 or head_y>arena.arena_bottom-self.part_size/2: # Crash into a wall
if len(self) > self.required_length:
if head_y<arena.arena_top+self.part_size/2:
if head_x<arena.exit_left+self.part_size/2 or head_x>arena.exit_right-self.part_size/2: # Crash into the exit walls
self.crash()
elif head_y < 0:
self.level_completed = True
self.targeted_length = 0
else:
self.crash()
elif self.is_running():
self.crash()
if self.is_running():
self.append(Part(self.rond, head_x, head_y))
if len(self) > self.targeted_length:
if len(self) - self.targeted_length >= self.parts_per_frame:
del self[0:self.parts_per_frame]
else:
del self[0:len(self) - self.targeted_length]
Selection = 0 if (head_x, head_y) == (-1, -1) and len(self) > 0:
head_x, head_y = self[-1].GetPosition()
TextColor = sf.Color(220, 220, 20, 255) if len(self) > self.part_size/self.parts_spacing + 1:
for i in range(len(self)):
if i < len(self) - self.part_size/self.parts_spacing - 1:
test_x, test_y = self[i].GetPosition()
if math.hypot(head_x-test_x, head_y-test_y) < self.part_size and self.is_running(): # Check for collision
self.crash()
Running = True if len(self) == 0:
Event = sf.Event() if self.level_completed:
self.level_completed = False
arena.next_level()
self.reset(arena)
Title = sf.String("PyWorm!") def is_running(self):
Title.SetX(320.) return (self.targeted_length > 0) and not self.level_completed
Title.SetY(50.) def draw_exit(self):
Title.SetColor(TextColor) return self.targeted_length > self.required_length or self.level_completed
Levels = ["Very Easy", "Easy", "Medium", "Hard"] class Game:
Xs = [320., 350., 330., 350.] def __init__(self, difficulty, window_width, window_height):
Strings = [0,0,0,0] self.arena = Arena(window_width, window_height)
for i in range(0, 4): self.worm = Worm(difficulty)
Strings[i] = sf.String(Levels[i]) self.worm.reset(self.arena)
Strings[i].SetColor(TextColor) self.apple = Apple()
Strings[i].SetPosition(Xs[i], 200. + 80*i) self.apple.random_move(self.arena)
self.pause = False
RectangleImg = sf.Image(ScreenWidth, 40, sf.Color(50,50,10,255)) def enter(self, pressed):
Rectangle = sf.Sprite(RectangleImg, 0, 350, 1, 1, 0) if pressed:
if not self.worm.restart(self.arena):
self.pause = not self.pause
while App.IsOpened(): # Game main loop def next_frame(self, win):
while App.GetEvent(Event): # Event Handler win.Draw(self.arena.values())
if Event.Type == sf.Event.Closed: if not self.pause:
App.Close() self.worm.move(self.arena, self.apple)
if Event.Type == sf.Event.KeyPressed: if self.worm.draw_exit():
if Event.Key.Code == sf.Key.Escape: win.Draw(self.arena.exit_rect)
App.Close() elif self.worm.is_running():
elif Event.Key.Code == sf.Key.Up: win.Draw(self.apple)
Selection = (Selection - 1) % 4 win.Draw(self.worm)
elif Event.Key.Code == sf.Key.Down:
Selection = (Selection + 1) % 4
elif Event.Key.Code == sf.Key.Return:
Game(Selection)
Rectangle.SetY(200 + Selection*80)
App.Draw(Rectangle)
App.Draw(Title)
for i in range(0,4):
App.Draw(Strings[i])
App.Display()
App.Clear(BGColor)
class Main:
# Entry Point
def __init__(self):
# Initialize the window
self.win = sf.RenderWindow(sf.VideoMode(800, 600,32), "PyWorm", sf.Style.Close) # Creates the window
self.win.EnableKeyRepeat(False)
background_color = sf.Color(100, 100, 0, 255)
self.win.SetFramerateLimit(30)
event = sf.Event()
self.keys = {} # keys to watch
self.menu_begin()
# Initialize the window # Boucle principale
ScreenWidth, ScreenHeight = 800, 600 while self.win.IsOpened():
App = sf.RenderWindow(sf.VideoMode(ScreenWidth,ScreenHeight,32), "PyWorm", sf.Style.Close) # Creates the window while self.win.GetEvent(event): # Event Handler
BGColor = sf.Color(100,100,0,255) if event.Type == sf.Event.Closed:
App.SetFramerateLimit(30) self.win.Close()
Menu() elif event.Type == sf.Event.KeyPressed:
for key in self.keys:
if event.Key.Code == key:
self.keys[key](True)
elif event.Type == sf.Event.KeyReleased:
for key in self.keys:
if event.Key.Code == key:
self.keys[key](False)
self.win.Display()
self.win.Clear(background_color)
self.next_frame(self.win)
# Menu
def menu_begin(self):
self.menu = Menu(self.win.GetWidth(), self.win.GetHeight())
self.keys = {sf.Key.Escape:self.close_window, sf.Key.Up:self.menu.key_up, sf.Key.Down:self.menu.key_down, sf.Key.Return:self.menu_end}
self.next_frame = self.menu.next_frame
def close_window(self, pressed):
if pressed:
self.win.Close()
def menu_end(self, pressed):
if pressed:
selection = self.menu.selection
del self.menu
self.game_begin(selection)
# Game
def game_begin(self, selection):
self.game = Game(selection, self.win.GetWidth(), self.win.GetHeight())
self.keys = {sf.Key.Left:self.game.worm.left, sf.Key.Right:self.game.worm.right, sf.Key.Return:self.game.enter, sf.Key.Escape:self.game_end}
self.next_frame = self.game.next_frame
def game_end(self, pressed):
if pressed:
del self.game
self.menu_begin()
Main()

View File

@ -293,7 +293,8 @@ Copy pixels from another image onto this one. This function does a slow pixel co
Source : Source image to copy\n\ Source : Source image to copy\n\
DestX : X coordinate of the destination position\n\ DestX : X coordinate of the destination position\n\
DestY : Y coordinate of the destination position\n\ DestY : Y coordinate of the destination position\n\
SourceRect : Sub-rectangle of the source image to copy (empty by default - entire image)"}, SourceRect : Sub-rectangle of the source image to copy (empty by default - entire image)\n\
ApplyAlpha : Should the copy take in account the source transparency? (false by default)"},
{"Create", (PyCFunction)PySfImage_Create, METH_VARARGS, "Create(Width=0, Height=0, Color=sf.Color.Black)\n\ {"Create", (PyCFunction)PySfImage_Create, METH_VARARGS, "Create(Width=0, Height=0, Color=sf.Color.Black)\n\
Create an empty image.\n\ Create an empty image.\n\
Width : Image width\n\ Width : Image width\n\
@ -397,16 +398,21 @@ PySfImage_Copy(PySfImage* self, PyObject *args)
PySfIntRect *SourceRect = NULL; PySfIntRect *SourceRect = NULL;
PySfImage *Source = NULL; PySfImage *Source = NULL;
unsigned int DestX, DestY; unsigned int DestX, DestY;
if (! PyArg_ParseTuple(args, "O!II|O!", &PySfImageType, &Source, &DestX, &DestY, &PySfIntRectType, &SourceRect)) PyObject *PyApplyAlpha;
bool ApplyAlpha = false;
if (! PyArg_ParseTuple(args, "O!II|O!O", &PySfImageType, &Source, &DestX, &DestY, &PySfIntRectType, &SourceRect, &PyApplyAlpha))
return NULL; return NULL;
if (PyObject_IsTrue(PyApplyAlpha))
ApplyAlpha = true;
if (SourceRect) if (SourceRect)
{ {
PySfIntRectUpdateObj(SourceRect); PySfIntRectUpdateObj(SourceRect);
self->obj->Copy(*(Source->obj), DestX, DestY, *(SourceRect->obj)); self->obj->Copy(*(Source->obj), DestX, DestY, *(SourceRect->obj), ApplyAlpha);
} }
else else
self->obj->Copy(*(Source->obj), DestX, DestY); self->obj->Copy(*(Source->obj), DestX, DestY, sf::IntRect(0, 0, 0, 0), ApplyAlpha);
Py_RETURN_NONE; Py_RETURN_NONE;
} }

View File

@ -38,6 +38,7 @@ bool CustomSoundStream::OnGetData(Chunk& Data)
if (PyObject_HasAttrString(SoundStream, "OnGetData")) if (PyObject_HasAttrString(SoundStream, "OnGetData"))
{ {
PyObject *PyData=NULL; PyObject *PyData=NULL;
Data.NbSamples = 0;
if ((PyData = PyObject_CallFunction(PyObject_GetAttrString(SoundStream, "OnGetData"), NULL))) if ((PyData = PyObject_CallFunction(PyObject_GetAttrString(SoundStream, "OnGetData"), NULL)))
{ {
if (PyArg_Parse(PyData, "s#", &(Data.Samples), &(Data.NbSamples))) if (PyArg_Parse(PyData, "s#", &(Data.Samples), &(Data.NbSamples)))

View File

@ -25,12 +25,12 @@
#ifndef __PYSOUNDSTREAM_HPP #ifndef __PYSOUNDSTREAM_HPP
#define __PYSOUNDSTREAM_HPP #define __PYSOUNDSTREAM_HPP
#include <SFML/Audio/SoundStream.hpp>
#include <iostream>
#include <Python.h> #include <Python.h>
#include <structmember.h> #include <structmember.h>
#include <SFML/Audio/SoundStream.hpp>
#include <iostream>
class CustomSoundStream : public sf::SoundStream class CustomSoundStream : public sf::SoundStream
{ {
public : public :