I've been starting to learn oop and im having difficulty making it do what i need it to. I'm building a simple game using the turtle module where a circle is dropped and everytime the circle drops out of the screen the score should decrease.Ill be adding shooting functions and speed configurations and all that. But at the moment I'm stuck on this circle loop.
The problem is the score is not changing correctly. I have two classes Circle and Player. The score is an instance variable of Player class, but drop() function in Circle class tallies the score.
I can make it work using a procedural style of programming but with oop im stuck.. Heres code. Any constructive criticism is welcome.
import turtle as t import random class Circle: def __init__(self,size): self.size = size #self.speed = speed self.circle = t.Turtle() self.ypos = 300 def size_color(self): self.circle.color('red') self.circle.shape('circle') self.circle.shapesize(self.size,self.size,1) #positions circle random x top y def posit(self): number = random.randint(-340,340) self.circle.ht() self.circle.penup() self.size_color() self.circle.goto(number,self.ypos) self.circle.st() #substracts from y position for falling effect #should substract from score as well def drop(self): self.posit() while True: self.ypos = self.ypos- 4 self.circle.sety(self.ypos) if self.ypos <= -300: Player().score = Player().score - 10 print(Player().score) self.ypos = 300 self.posit() class Player: def __init__(self,score=200): self.score = score def display_screen(): window = t.Screen() window.bgcolor('black') display_screen() c = Circle(2) c.drop()