Hi,
I am pretty new to Python and coding generally, and I have been trying to learn by writing something in Pythonista that might help my daughter with her reading. The idea is that a word appears at the top of the screen (e.g. 'cow') and your score increases if you touch the corresponding picture on a 4 x 4 grid of various images.
I have been 90% there for a while now, but I'm struggling to deal properly with touch values and also to retain the grid of images after the first touch. My most recent efforts seem to have only made it worse (!), so I am turning to the forum in desperation...
I still haven't completely got my head around classes and methods, and I suspect this might be where I am going wrong. If anyone is able to take a look I would be very grateful. My code is as follows:
from scene import *
import Image
import random
import sound
import time
class MyScene (Scene):
def setup(self):
# Set up layer1 (where score and word will appear)
self.root_layer = Layer(self.bounds)
self.layer1 = Layer(Rect(0, 200, self.size.w, self.size.h - 200))
self.root_layer.add_layer(self.layer1)
# Create black background
background(0, 0, 0)
# Setup screen
self.w, self.h = self.size.w, self.size.h - 200
self.col_width = self.w / 4
self.row_height = self.h / 4
# Create list of images and the word which will appear with each one.
self.images = [('Ant', 'a n t'), ('Bear_Face', 'b e a r'), ('Bug', 'c a t e r p i l l a r'), ('Chicken', 'c h i c k e n'), ('Cow_Face', 'c o w'), ('Dog_Face', 'd o g'), ('Elephant', 'e l e p h a n t'), ('Fish', 'f i s h'), ('Frog_Face', 'f r o g'), ('Honeybee', 'b e e'), ('Pig_Face', 'p i g'),('Snail', 's n a i l'), ('Snake', 's n a k e'), ('Whale', 'w h a l e'), ('Tiger_Face', 't i g e r'), ('Rabbit_Face', 'r a b b i t')] * 2
# Draw grid of images
self.co_ords = []
self.a = []
self.rects = []
for i in range(16):
x, y = (i % 4) * self.col_width, (i / 4) * self.row_height
self.a.append(x)
self.a.append(y)
self.co_ords.append(self.a)
self.a = []
self.rects.append( Rect( x, y, self.col_width, self.row_height ))
for i in range(len(self.co_ords)):
image(self.images[i][0], self.co_ords[i][0], self.co_ords[i][1], self.col_width, self.row_height)
# Pick random image
self.random_image()
# Set self.score to 0
self.score = 0
# Display word and score
self.update_score()
self.update_answer()
def update_score(self):
# display score
text("Score: " + str(self.score), font_name='ChalkboardSE-Bold', font_size=24.0, x = self.w / 20, y = self.size.h - 100, alignment = 6)
def update_answer(self):
# display legible version of answer
text(self.images[self.current_image][1], font_name='ChalkboardSE-Bold', font_size=72.0, x = self.w * 0.5, y = self.size.h - 100, alignment=5)
def random_image(self):
# choose a random image
self.current_image = random.randint(0, len(self.images) - 1)
def touch_ended(self, touch):
# check touch.location against list of self.rects
for n, r in enumerate( self.rects ):
if touch.location in r:
# if touch.location is within correct image:
if n == self.current_image:
sound.play_effect('Powerup_1')
self.score += 1
self.update_score()
self.random_image()
self.update_answer()
return
# if touch.location is outside correct image:
if n != self.current_image:
sound.play_effect('Error')
self.score -= 1
self.update_score()
return
run(MyScene())