anakinmorgan
Posts: 13
Joined: Wed Jul 18, 2012 6:17 pm

Magpi 4 Fish Game

Mon Aug 06, 2012 2:48 pm

If anyone can offer just a little advise i would be forever greatful.
After the insane problem with the last issue and Bat and Ball i decided to give up the ghost and try magpi issue 4 "Fish Game"

I am however tearing more hair out as just like the "bat and ball" this game is giving me issues
I have written just like it says on the magpi and even geany let me know what mistakes i had made, this then ran the program but just with a black screen.
I thought it may have something to do with the files you need to download ie. png and wav files
But this is not the case.

Please Help

Raspban Wheezy Geanny user.

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Magpi 4 Fish Game

Mon Aug 06, 2012 3:20 pm

It is difficult to help with this little information. Have you already tried to put in some print statements to see which lines get executed and where the program might hang?

Code: Select all

while not self.asleep():
    sheep += 1

anakinmorgan
Posts: 13
Joined: Wed Jul 18, 2012 6:17 pm

Re: Magpi 4 Fish Game

Mon Aug 06, 2012 3:25 pm

Um ok and so i should have said this is my very first time programming aswell... so i am very thankful of your help.

I have not tried this as i thought geany would let me know which it did when i ran it and it told me like... line 30 "something something" to which i corrected and it ran with no errors.
So thats what made me post this as no errors yet only a black screen(this is a screen that comes up when i run the program for geany, it comes up with a LXTerminal screen then another screen with the heading of "Fish Game" to which this window is Black.

Thanks

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

Re: Magpi 4 Fish Game

Mon Aug 06, 2012 4:10 pm

Sorry to hear you are having problems with this.
You could have a look at this blog post in which I go through an earlier (simpler) version of the Bat and Ball thing.
100Mudcats

If you are having problems with the fish game also, I will post up a link to an earlier version that may be easier to follow a bit later this evening.

If you can post up exactly what is going wrong, I will try to help!
Just out of interest, have you tried opening a console in that folder and typing:

Code: Select all

python fish.py
(or whatever you named your file). Sometimes Geany itself causes problems.
In the mean time, I have uploaded some videos, which go though some programs that definitely will work!
Video One

Umbrella
Posts: 1
Joined: Mon Aug 06, 2012 5:48 pm

Re: Magpi 4 Fish Game

Mon Aug 06, 2012 5:57 pm

you have to make sure to use the correct indention in the code. wrong indention after a for loop will run without complain, but is logically wrong.

anakinmorgan
Posts: 13
Joined: Wed Jul 18, 2012 6:17 pm

Re: Magpi 4 Fish Game

Mon Aug 06, 2012 6:01 pm

If you have a link to the fish game I would be very greatful.
Thanks

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Magpi 4 Fish Game

Tue Aug 07, 2012 6:28 am

Why don't you show us YOUR program so that someone else can take a look and see if they get a blank screen?

T.
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

Re: Magpi 4 Fish Game

Tue Aug 07, 2012 8:26 am

I am going to share the code of an earlier "draft" of the fish game.
This one doesn't have the "searchlight" - Jaseman wrote that part.
However, it is very similar (just dodge the black bars!)
Here it is as a .zip (with images and sounds).
And here's a tar.gz archive.
Gameplay should look like this:
Image

And here's my bat_ball.py. It's a little different from the published version (it was too long as-is). But it does use "pygame.draw" to make the bat and ball, unlike the version on my blog in which I used .png images.

anakinmorgan
Posts: 13
Joined: Wed Jul 18, 2012 6:17 pm

Re: Magpi 4 Fish Game

Tue Aug 07, 2012 8:52 am

if anyone can see something i have missed or i should put in. thanks

Code: Select all

# Fish Game



# By Steve Morgan 02.08.12


import pygame, random
from pygame.locals import*

pygame.init()

clock = pygame.time.Clock()



screen = pygame.display.set_mode([600,400])

pygame.display.set_caption("Fish Game")



music = pygame.mixer.Sound("tune.wav")

music.play(-1)

toy = pygame.mixer.Sound("toy.wav")

burp = pygame.mixer.Sound("burp.wav")



tux = pygame.image.load("arm_linux.png").convert_alpha()

fish = pygame.image.load("fish.png").convert_alpha()



tx=280; ty=180; txd=0; tyd=0

bx=600; by=-15

fx=600; fy=random.randint(0,370)

bkcol=[127,212,255]

lives=3; score=0; run = True;



# Draw the surface for the tux, fish and ball

tuxsurf = pygame.Surface((60,70))

tuxsurf.fill(bkcol)

tuxsurf.set_colorkey(bkcol)

tuxsurf.blit(tux,[0,0])

fishsurf = pygame.Surface((35,30))

fishsurf.fill(bkcol)

fishsurf.set_colorkey(bkcol)

fishsurf.blit(fish,[0,0])

ballsurf = pygame.Surface((60,60))

ballsurf.fill(bkcol)

ballsurf.set_colorkey(bkcol)

ballsurf.set_alpha(128)

pygame.draw.circle(ballsurf,[255,255,255],[30,30],30)



while run:
	
	for event in pygame.event.get():
			
		if event.type == pygame.QUIT: run = False
		
		if event.type == pygame.KEYDOWN:
			
			if event.key == pygame.K_LEFT: txd=-4
			
			if event.key == pygame.K_RIGHT: txd=4

			if event.key == pygame.K_UP: tyd=-4

			if event.key == pygame.K_DOWN: tyd=4
		
		if event.type == pygame.KEYUP:

			txd=0; tyd=0

	tx+=txd; ty+=tyd
	


# This part stops Tux from leaving the edges of the screen

if tx<= 0: tx=0

if tx>=540: tx=540

if ty<0: ty=0

if ty>=330: ty=330



# Make the ball chase Tux

if bx>=tx: bx=bx=1

else: bx=bx+1

if by>=ty: by=by=1

else: by=by+1

fx=fx-4

if fx<=-10: fx=600; fy=random.randint(0,370)



# Collision Detection (Tux & Fish, Tux & Ball)

if fx<=tx+50 and fx>=tx and fy>=ty-30 and fy<=ty+70:

	toy.play(); fx=600;fy=random.randint(0,370); score +=1

if bx<tx+40 and bx>=tx-40 and by>=ty-50 and by<=ty+60:

	burp.play(); bx=600; by=-15; lives -=1; tx=280; ty=180



screen.fill(bkcol)

screen.blit(tuxsurf,[tx,ty])

screen.blit(fishsurf,[fx,fy])

screen.blit(ballsurf,[bx,by])

font = pygame.font.Font(None, 20)

text = font.render("Score: "+str(score), 1, (0,0,0))

screen.blit(text, [5,5])

text = font.render("Lives: "+str(lives), 1, (0,0,0))

screen.blit(text, [80,5])



if lives <= 0:

	font = pygame.font.Font(None, 120)

	text = font.render("GAME OVER!", 1, (255,0,0))

	screen.blit(text, [36,150])

	pygame.display.update();pygame.time.wait(4000)

	lives=3; score=0; tx=280; ty=180

	
		pygame.display.update(); clock.tick(100)

pygame.quit()


anakinmorgan
Posts: 13
Joined: Wed Jul 18, 2012 6:17 pm

Re: Magpi 4 Fish Game

Tue Aug 07, 2012 2:45 pm

Just found this post from Python 3(.2.3) and Pygame on Rasbian Wheezy
the user put:

As the title says I'm using the latest Rasbian Wheezy image, and it only comes with Pygame installed for Python 2 (annoyingly.) I have a game I've written in Python 3 (.1.3 to be precise), and I want to see if/how well it runs on the Pi.

So this prob is my issue. I would however like to see this running so i first need to check i have pygame installed... and then download this i think:
sudo apt-get install python3-dev python3-numpy

Thanks

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

Re: Magpi 4 Fish Game

Tue Aug 07, 2012 4:30 pm

Hey anakin,

From the code you posted, I think it's an indentation problem. Everything from where you check if Tux has gone off screen outside the while loop!

The indentation of the last 2 lines looks okay though.

mark

anakinmorgan
Posts: 13
Joined: Wed Jul 18, 2012 6:17 pm

Re: Magpi 4 Fish Game

Tue Aug 07, 2012 5:33 pm

Thanks mark.... Um what are indentations. Sorry if I'm an idiot asking

timhoffman
Posts: 85
Joined: Sat Nov 05, 2011 11:31 pm

Re: Magpi 4 Fish Game

Wed Aug 08, 2012 12:21 am

In python white space (blank bits at the beginning of each line) are significant.

The indentation denotes scope and nesting.
The following two code examples are different
.e.g

Code: Select all

if x ==1:
    print "hello"
print "good bye"

Code: Select all

if x ==1:
    print "hello"
    print "good bye"
Can you guess how the indents affect the behaviour and how the statements are grouped around the condition.

I personally would NOT recommend using geany if you are starting out. Out of the box it's indentation settings are not correct for python. Even if you set the indentation settings correct "de-dent" function shift=>tab inserts tabs rather than spaces. What might not be obvious is when you cut and paste python code, or open other files from different sources you can easily then get mixed up tabs and spaces, which can end up meaning the code looks ok, but the indentation is incorrect.

A very light weight alternative is scite, or leafpad (sudo apt-get install scite)

With any editor you should set it to use spaces, not tabs, and the indent 2 or 4 space characters.

anakinmorgan
Posts: 13
Joined: Wed Jul 18, 2012 6:17 pm

Re: Magpi 4 Fish Game

Wed Aug 08, 2012 3:01 pm

Indentation... i cant begin to tell you how annoyed i was with myself.
Such a big thank you and special thanks to Mark.

Cheers

I have a working game YAY

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

Re: Magpi 4 Fish Game

Wed Aug 08, 2012 5:37 pm

:D
Cool!
Have you had a look at any of my YouTube videos? There are nine now.
Here'sthe latest.

Return to “Python”