Python Forum
I really need help, I am new to python, I am using a book that helps me to learn
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I really need help, I am new to python, I am using a book that helps me to learn
#1
from tkinter import *
from time import sleep, time
from random import randint
from math import sqrt
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title("Stoppa bubblorna!")
c = Canvas(window, width=WIDTH, height=HEIGHT, bg="darkblue")
c.pack()

ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill="red")
ship_id2 = c.create_oval(0, 0, 30, 30, outline="red")
SHIP_R = 15
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
c.move(ship_id, MID_X, MID_Y)
c.move(ship_id2, MID_X, MID_Y)

SHIP_SPD = 10
def move_ship(event):
    if event.keysym == "Up":
        c.move(ship_id, 0, -SHIP_SPD)
        c.move(ship_id2, 0, -SHIP_SPD)
    if event.keysym == "Down":
        c.move(ship_id, 0, SHIP_SPD)
        c.move(ship_id2, 0, SHIP_SPD)
    if event.keysym == "Right":
        c.move(ship_id, SHIP_SPD, 0)
        c.move(ship_id2, SHIP_SPD, 0)
    if event.keysym == "Left":
        c.move(ship_id, -SHIP_SPD, 0)
        c.move(ship_id2, -SHIP_SPD, 0)
c.bind_all("<Key>", move_ship)


bub_id = list()
bub_r = list()
bub_speed = list()
MIN_BUB_R = 10
MAX_BUB_R = 30
MAX_BUB_SPD = 5
GAP = 100
def create_bubble():
    x = WIDTH + GAP
    y = randint(0, HEIGHT)
    r = randint(MIN_BUB_R, MAX_BUB_R)
    id1 = c.create_oval(x - r, y - r, x + r, y + r, outline="white")
    bub_id.append(id1)
    bub_r.append(r)
    bub_speed.append(randint(1, MAX_BUB_SPD))
def move_bubbles():
    for i in range(len(bub_id)):
        c.move(bub_id[i], -bub_speed[i], 0)
def get_coords(id_num):
    pos = c.coords(id_num)
    x = (pos[0] + pos[2])/2
    y = (pos[1] + pos[3])/2
    return x, y
def del_bubble(i):
    del bub_r[i]
    del bub_speed[i]
    c.delete(bub_id[i])
    del bub_id[i]
def clean_up_bubs():
    for i in range(len(bub_id)-1, -1, -1):
        x, y = get_coords(bub_id[i])
        if x < -GAP:
            del_bubble(i)
def distance(id1, id2):
    x1, y1 = get_coords(id1)
    x2, y2 = get_coords(id2)
    return sqrt((x2 - x1)**2 + (y2 - y1)**2)
def collision():
    points = 0
    for bub in range(len(bub_id)-1, -1, -1):
        if distance(ship_id2, bub_id[bub]) < (SHIP_R + bub_r[bub]):
            points += (bub_r[bub] + bub_speed[bub])
            del_bubble(bub)
        return points
score = 0    
BUB_CHANCE = 10
#GLOBAL LOOP
while True:
    if randint(1, BUB_CHANCE) == 1:
        create_bubble()
    move_bubbles()
    clean_up_bubs()
    score += collision()
    print(score)
    window.update()
    sleep(0.01)
Larz60+ write Nov-26-2020, 10:37 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

I fixed for you this time. Please use bbcode tags on all future posts.
Reply
#2
I am using the book and it says this should work, the score is in collision as you can see, and when i want to add score += collision() it doesnt work and all the bubbles dissappear and when I just put collision(), the bubbles dont break when they collide. If anyone wants to ask, no this code doesn’t have any errors, it just says cannot use += on a none type and that’s all
Reply
#3
(Nov-27-2020, 01:23 PM)JaprO Wrote: no this code doesn’t have any errors, it just says cannot use += on a none type and that’s all

Those two things are contradictory - the second sounds like an exception is being raised. It would be easier to help you if you posted the traceback, but in any case, work backwards from that line and workout why the relevant variable is None. You can print stuff out (i.e. the values of your variables, or when execution has entered a certain branch) to help you narrow down where the problem is.
Reply
#4
(Nov-27-2020, 01:23 PM)JaprO Wrote: I am using the book and it says this should work, the score is in collision as you can see, and when i want to add score += collision() it doesnt work and all the bubbles dissappear and when I just put collision(), the bubbles dont break when they collide. If anyone wants to ask, no this code doesn’t have any errors, it just says cannot use += on a none type and that’s all
only error is Traceback (most recent call last):
File "C:\Users\kubus\AppData\Local\Programs\Python\Python39\Files\Bubble.py", line 90, in <module>
score += collision()
TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType' and either way when it says that it doesnt summon any bubbles and when i remove the error (i can just remove score += collision() and the error goes away but that makes other difficulties), by removing whats wrong it doesnt break the bubbles. nvm fixed the error, the return points if u look in the code had to many spaces, fixed it by removing one tab.
Reply
#5
Collision does not return a value for all cases. Is this a logic or indentation error?
Reply
#6
(Nov-27-2020, 04:17 PM)deanhystad Wrote: Collision does not return a value for all cases. Is this a logic or indentation error?
I found the error but thank you for your try to help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner stuck in Python book for kids mic81k 11 1,195 Nov-27-2023, 04:28 AM
Last Post: deanhystad
  Deitel book "Python for Programmers" ricardian 7 22,754 May-12-2023, 01:33 PM
Last Post: snippsat
Question Pandas : How to create an algorithm that helps me improve results and creating new co Smordy 8 2,241 Apr-10-2022, 10:28 PM
Last Post: Larz60+
  best " Learning Python " book for a beginner alok 4 3,045 Jul-30-2021, 11:37 AM
Last Post: metulburr
  Nested Python functions (Dan Bader's book) Drone4four 4 2,558 Jun-26-2021, 07:54 AM
Last Post: ndc85430
  Helps with reading csv file - 3 methods hhchenfx 4 3,271 May-13-2021, 04:15 AM
Last Post: buran
  Want to learn Python compilation and virtual machine IJB 3 2,616 Feb-14-2020, 02:59 PM
Last Post: IJB
  What is the bestway to learn Python? PythonRIMD 2 2,400 Jan-11-2020, 04:12 PM
Last Post: joe_momma
  creating an 'adress book' in python using dictionaries? apollo 6 14,796 May-06-2019, 12:03 PM
Last Post: snippsat
  Beginner want to learn python Keenygp 5 3,268 Apr-10-2019, 09:36 AM
Last Post: KevinBrown

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020