0

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() 

2 Answers 2

2

Every time you call Player() you're creating a new player. So when you do

Player().score = Player().score - 10 print(Player().score) 

it's roughly equivalent to:

temp_player1 = Player() temp_player2 = Player() temp_player1.score = temp_player2.score - 10 temp_player3 = Player() print(temp_player3.score) 

As you can see from that, there are 3 different players, so you're never decrementing any player's score, and then you're printing a different player's score.

You need to add a player attribute to the Circle, then it can use self.player instead of creating a new player each time.

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 this.player = Player() 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: self.player.score = self.player.score - 10 print(self.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() 
Sign up to request clarification or add additional context in comments.

Comments

0

Barmar stole my answer. Use what he said, however don't forget to change this.player to self.player as he made a tiny mistake, probably because of other programming languages.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.