Discuss Scratch

pmp-p
New Scratcher
1 post

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

LoIdesMio wrote:

So I was trying to do it on Replit (yes i will only stay on replit nowhere else) but when I ran the code:
https://replit.com/@Ellirio/Pygame-Test

Hi, you probably can use pygbag on replit someone did it https://github.com/pygame-web/pygbag/issues/110 . Nowadays you only need a WebAssembly enabled browser to run pygame-ce on any operating system including mobile see https://pygame-web.github.io for a little documentation.

Last edited by pmp-p (Nov. 2, 2023 18:42:19)

MonkeyBean2
Scratcher
100+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

rishi272011 wrote:

Hello, I was making a game in pygame, which uses tkinter for selecting the seed. I was making a prototype when i encountered a problem.
def askseed(self, initvalue):
        def askseed(self, initvalue):
        root = tkinter.Tk()
        root.title("Seed")
        root.geometry("400x300")
        root.resizable(False, False)
        seedvar = tkinter.IntVar()
        _seed = initvalue
        def submit():
            global _seed
            _seed = str(seedvar.get())
            print("test1", _seed)
            seedvar.set(0)
            # root.destroy()
            
        intro1 = tkinter.Label(root, text="New World", font=("Times", 16), justify='center').pack(anchor='center', side="top", pady=20)
        intro2 = tkinter.Label(root, text="Enter seed:", font=("Times", 14)).pack(pady=5)
        seed_entry = tkinter.Entry(root, textvariable = seedvar, font=('calibre',10,'normal')).pack(side="top", after=intro2, padx=20, pady=10)
        sub = tkinter.Button(root, text="          ok          ", command=submit).pack(pady=10)
        root.mainloop()
        # return askinteger(Main, text, initialvalue=initvalue)
        print("test2", _seed)
        return _seed

How to i send the value from inside the submit() function inside the askseed function to the askseed function. The submit function is triggered by a button. [The init value is randomly chosen]

The result is:


pygame 2.5.0 (SDL 2.28.0, Python 3.10.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
test1 83
test2 163002 ⬅ original randomly chosen

your inner askseed function does nothing


9NAwRfSR-%dWFS$a$g&zBMTj2?;jvS?L5%^X%qpCX|A<*paJ=lU07c)wmt$v{TxL5



scratchGui.timeTravel.year = '2020'
rishi272011
Scratcher
91 posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

MonkeyBean2 wrote:

rishi272011 wrote:

Hello, I was making a game in pygame, which uses tkinter for selecting the seed. I was making a prototype when i encountered a problem.
def askseed(self, initvalue):
        def askseed(self, initvalue):
        root = tkinter.Tk()
        root.title("Seed")
        root.geometry("400x300")
        root.resizable(False, False)
        seedvar = tkinter.IntVar()
        _seed = initvalue
        def submit():
            global _seed
            _seed = str(seedvar.get())
            print("test1", _seed)
            seedvar.set(0)
            # root.destroy()
            
        intro1 = tkinter.Label(root, text="New World", font=("Times", 16), justify='center').pack(anchor='center', side="top", pady=20)
        intro2 = tkinter.Label(root, text="Enter seed:", font=("Times", 14)).pack(pady=5)
        seed_entry = tkinter.Entry(root, textvariable = seedvar, font=('calibre',10,'normal')).pack(side="top", after=intro2, padx=20, pady=10)
        sub = tkinter.Button(root, text="          ok          ", command=submit).pack(pady=10)
        root.mainloop()
        # return askinteger(Main, text, initialvalue=initvalue)
        print("test2", _seed)
        return _seed

How to i send the value from inside the submit() function inside the askseed function to the askseed function. The submit function is triggered by a button. [The init value is randomly chosen]

The result is:


pygame 2.5.0 (SDL 2.28.0, Python 3.10.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
test1 83
test2 163002 ⬅ original randomly chosen

your inner askseed function does nothing
Sorry, I pasted the code twice. Heres the real code:
def askseed(self, initvalue):
        root = tkinter.Tk()
        root.title("Seed")
        root.geometry("400x300")
        root.resizable(False, False)
        seedvar = tkinter.IntVar()
        _seed = initvalue
        def submit():
            global _seed
            _seed = str(seedvar.get())
            print("test1", _seed)
            seedvar.set(0)
            root.destroy()
            
        intro1 = tkinter.Label(root, text="New World", font=("Times", 16), justify='center').pack(anchor='center', side="top", pady=20)
        intro2 = tkinter.Label(root, text="Enter seed:", font=("Times", 14)).pack(pady=5)
        seed_entry = tkinter.Entry(root, textvariable = seedvar, font=('calibre',10,'normal')).pack(side="top", after=intro2, padx=20, pady=10)
        sub = tkinter.Button(root, text="          ok          ", command=submit).pack(pady=10)
        root.mainloop()
        # return askinteger(Main, text, initialvalue=initvalue)
        print("test2", _seed)
        return _seed
NotK3ndricAlt
Scratcher
1000+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

File "main.py", line 1
pip install scratchattach
^
SyntaxError: invalid syntax
** Process exited - Return Code: 1 **
Press Enter to exit terminal
How to fix???


If you see this on my signature, you’ve been infected by the NotK3ndricAlt virus! Put this on the top of your signature and add your name to the list of people who got this virus to help NotK3ndricAlt get more followers.
List of people who got this virus: NotK3ndricAlt
Forum clout (as of 27 Jan 2024): 11943
But then I had a very good idea. I used F5. See, using F5 gave me a whole new perspective and I was able to see a chest I couldn't have seen before.
Everytime I see the number 2024, I think its 2014 for a millisecond but then realize its 2024.
My browser / operating system: IPad iOS 16.1.1, Safari 16.1, No Flash version detected
Did you know that you can select the text then click on the BBCode tag you want to use and it'll pop up with the BBCode on the text?
Hola, soy NotK3ndricAlt! I speak Spanish and English.
My rentry!
Kids rule! B)
Participated in Scratch Awards 2023
pls plaey https://scratch.mit.edu/projects/928410544/ NOAW!!!M!!M!!M!M!!M!!M!!M!M!M!M!
#Add ForumPost Hearting (click to see suggestion)
Omni Studios Admin
(^v^ :: motion) // Kumquat protector
Hacked block:
when this sprite touches [mouse-pointer v] :: events hat
undeterminstic
Scratcher
1000+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

NotK3ndricAlt wrote:

File "main.py", line 1
pip install scratchattach
^
SyntaxError: invalid syntax
** Process exited - Return Code: 1 **
Press Enter to exit terminal
How to fix???
you are doing this in a python console

yes
NotK3ndricAlt
Scratcher
1000+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

minecraftmanpp_tutor wrote:

NotK3ndricAlt wrote:

File "main.py", line 1
pip install scratchattach
^
SyntaxError: invalid syntax
** Process exited - Return Code: 1 **
Press Enter to exit terminal
How to fix???
you are doing this in a python console
oh


If you see this on my signature, you’ve been infected by the NotK3ndricAlt virus! Put this on the top of your signature and add your name to the list of people who got this virus to help NotK3ndricAlt get more followers.
List of people who got this virus: NotK3ndricAlt
Forum clout (as of 27 Jan 2024): 11943
But then I had a very good idea. I used F5. See, using F5 gave me a whole new perspective and I was able to see a chest I couldn't have seen before.
Everytime I see the number 2024, I think its 2014 for a millisecond but then realize its 2024.
My browser / operating system: IPad iOS 16.1.1, Safari 16.1, No Flash version detected
Did you know that you can select the text then click on the BBCode tag you want to use and it'll pop up with the BBCode on the text?
Hola, soy NotK3ndricAlt! I speak Spanish and English.
My rentry!
Kids rule! B)
Participated in Scratch Awards 2023
pls plaey https://scratch.mit.edu/projects/928410544/ NOAW!!!M!!M!!M!M!!M!!M!!M!M!M!M!
#Add ForumPost Hearting (click to see suggestion)
Omni Studios Admin
(^v^ :: motion) // Kumquat protector
Hacked block:
when this sprite touches [mouse-pointer v] :: events hat
god286
Scratcher
1000+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

NotK3ndricAlt wrote:

minecraftmanpp_tutor wrote:

NotK3ndricAlt wrote:

File "main.py", line 1
pip install scratchattach
^
SyntaxError: invalid syntax
** Process exited - Return Code: 1 **
Press Enter to exit terminal
How to fix???
you are doing this in a python console
oh
You need to use a Terminal or Command Prompt

Here are some of my followers!

I joined: 5 years, 9 months, 24 days ago (31/03/2018)
I have: 479 followers
In total, I have attained: 1,403 loves, 1,145 favourites, and 33,731 views.
Fun Fact: If my account continued to gain followers at a similar rate to right now, in 14,210 years I would reach the number of followers griffpatch has today! Try to imagine how many followers he would have then!
Thank you everyone!
Script created by god286.
rishi272011
Scratcher
91 posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

Code:
def askseed(self, initvalue):
        root = tkinter.Tk()
        root.title("Seed")
        root.geometry("400x300")
        root.resizable(False, False)
        seedvar = tkinter.IntVar()
        _seed = initvalue
        def submit():
            global _seed
            _seed = str(seedvar.get())
            print("test1", _seed)
            seedvar.set(0)
            root.destroy()
            
        intro1 = tkinter.Label(root, text="New World", font=("Times", 16), justify='center').pack(anchor='center', side="top", pady=20)
        intro2 = tkinter.Label(root, text="Enter seed:", font=("Times", 14)).pack(pady=5)
        seed_entry = tkinter.Entry(root, textvariable = seedvar, font=('calibre',10,'normal')).pack(side="top", after=intro2, padx=20, pady=10)
        sub = tkinter.Button(root, text="          ok          ", command=submit).pack(pady=10)
        root.mainloop()
        # return askinteger(Main, text, initialvalue=initvalue)
        print("test2", _seed)
        return _seed

Does this depend on scope, would making the seed a class variable work?
like
self.seed
ajskateboarder
Scratcher
1000+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

rishi272011 wrote:

Code:
def askseed(self, initvalue):
        root = tkinter.Tk()
        root.title("Seed")
        root.geometry("400x300")
        root.resizable(False, False)
        seedvar = tkinter.IntVar()
        _seed = initvalue
        def submit():
            global _seed
            _seed = str(seedvar.get())
            print("test1", _seed)
            seedvar.set(0)
            root.destroy()
            
        intro1 = tkinter.Label(root, text="New World", font=("Times", 16), justify='center').pack(anchor='center', side="top", pady=20)
        intro2 = tkinter.Label(root, text="Enter seed:", font=("Times", 14)).pack(pady=5)
        seed_entry = tkinter.Entry(root, textvariable = seedvar, font=('calibre',10,'normal')).pack(side="top", after=intro2, padx=20, pady=10)
        sub = tkinter.Button(root, text="          ok          ", command=submit).pack(pady=10)
        root.mainloop()
        # return askinteger(Main, text, initialvalue=initvalue)
        print("test2", _seed)
        return _seed

Does this depend on scope, would making the seed a class variable work?
like
self.seed
If this function is the member of a class, it should work. Do you have the entire code, though? This looks confusing :P
rishi272011
Scratcher
91 posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

ajskateboarder wrote:

rishi272011 wrote:

Code:
def askseed(self, initvalue):
        root = tkinter.Tk()
        root.title("Seed")
        root.geometry("400x300")
        root.resizable(False, False)
        seedvar = tkinter.IntVar()
        _seed = initvalue
        def submit():
            global _seed
            _seed = str(seedvar.get())
            print("test1", _seed)
            seedvar.set(0)
            root.destroy()
            
        intro1 = tkinter.Label(root, text="New World", font=("Times", 16), justify='center').pack(anchor='center', side="top", pady=20)
        intro2 = tkinter.Label(root, text="Enter seed:", font=("Times", 14)).pack(pady=5)
        seed_entry = tkinter.Entry(root, textvariable = seedvar, font=('calibre',10,'normal')).pack(side="top", after=intro2, padx=20, pady=10)
        sub = tkinter.Button(root, text="          ok          ", command=submit).pack(pady=10)
        root.mainloop()
        # return askinteger(Main, text, initialvalue=initvalue)
        print("test2", _seed)
        return _seed

Does this depend on scope, would making the seed a class variable work?
like
self.seed
If this function is the member of a class, it should work. Do you have the entire code, though? This looks confusing :P
Yes, here it is. It serves as a start button for a game I am making. It is called by:
from startgame import selectedseed
SEED = selectedseed
The full code is :
import pygame
import tkinter
from tkinter.simpledialog import askinteger
from random import randint
from buttons import Button
from os import listdir, getcwd
def get_worlds():
    #unused
    return list(map(lambda x : x.replace(".world", ""), listdir(getcwd() + "\worlds")))
selectedseed = 0
class StartButton(Button):
    def askseed(self, initvalue):
        root = tkinter.Tk()
        root.title("Seed")
        root.geometry("400x300")
        root.resizable(False, False)
        seedvar = tkinter.IntVar()
        _seed = initvalue
        def submit():
            global _seed
            _seed = str(seedvar.get())
            print("test1", _seed)
            seedvar.set(0)
            root.destroy()
            
        intro1 = tkinter.Label(root, text="New World", font=("Times", 16), justify='center').pack(anchor='center', side="top", pady=20)
        intro2 = tkinter.Label(root, text="Enter seed:", font=("Times", 14)).pack(pady=5)
        seed_entry = tkinter.Entry(root, textvariable = seedvar, font=('calibre',10,'normal')).pack(side="top", after=intro2, padx=20, pady=10)
        sub = tkinter.Button(root, text="          ok          ", command=submit).pack(pady=10)
        root.mainloop()
        # return askinteger(Main, text, initialvalue=initvalue)
        print("test2", _seed)
        return _seed
    def interact(self):
        pass
    def update(self, mx, my, mouse=False):
        global selectedseed
        tx = self.posx + self.width
        ty = self.posy + self.height
        if (mx > self.posx) and (mx < tx) and (my > self.posy) and (my < ty):
            self.colour = (200, 200, 200)
            if mouse:
                selectedseed = self.askseed(initvalue=int(randint(1, 999999)))
                return True
        else:
            self.colour = self.org_colour
            return False
        
class CreditsButton(Button):
    def interact(self):
        root = tkinter.Tk()
        root.title("Credits")
        root.resizable(False, False)
        root.geometry("400x250") 
        text= tkinter.Label(root, text="Credits:")
        text.pack(pady=20)
        text.config(font=('Helvatical bold',20))
        tkinter.Label(root, text="Most code by Debarpan Mukherjee.").pack(pady=10)
        tkinter.Label(root, text="Help with code from GeekForGeeks").pack()
        tkinter.Label(root, text="All art by Debarpan Mukherjee").pack()
        tkinter.Label(root, text="\"PixelType\" font from ClearCode on youtube.").pack()
        close = tkinter.Button(root, text="      Ok      ", command=root.destroy)
        close.pack(pady=20)
        root.mainloop()
        
pygame.init() #MAIN INIT
WIDTH, HEIGHT = 700, 466
favicon = pygame.image.load("assets\Favicon.ico")
pygame.display.set_icon(favicon)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Start Game')
clock = pygame.time.Clock()
st = StartButton("Start Game", 35, (200, 183), (300, 50), (0, 0, 255))
cr = CreditsButton("Credits", 35, (200, 383), (300, 50), (0, 0, 255))
def draw():
    screen.fill((0, 0, 0))
    mx, my = pygame.mouse.get_pos()
    mouse = (pygame.mouse.get_pressed()[0])
    st.draw(screen)
    cr.draw(screen)
    cr.update(mx, my, mouse)
    if st.update(mx, my, mouse):
        print("The chosen seed is " + str(selectedseed))
        pygame.quit()
        return True
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    
    if draw():
        break
    pygame.display.update()
    clock.tick(60)

My problem was that I had no way of sending the data from the inner submit() function to the outer function
Your suggestions are not yet implemented, I shall implement them tomorrow.

Last edited by rishi272011 (Nov. 20, 2023 06:15:43)

rishi272011
Scratcher
91 posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

rishi272011 wrote:

ajskateboarder wrote:

rishi272011 wrote:

Code:
def askseed(self, initvalue):
        root = tkinter.Tk()
        root.title("Seed")
        root.geometry("400x300")
        root.resizable(False, False)
        seedvar = tkinter.IntVar()
        _seed = initvalue
        def submit():
            global _seed
            _seed = str(seedvar.get())
            print("test1", _seed)
            seedvar.set(0)
            root.destroy()
            
        intro1 = tkinter.Label(root, text="New World", font=("Times", 16), justify='center').pack(anchor='center', side="top", pady=20)
        intro2 = tkinter.Label(root, text="Enter seed:", font=("Times", 14)).pack(pady=5)
        seed_entry = tkinter.Entry(root, textvariable = seedvar, font=('calibre',10,'normal')).pack(side="top", after=intro2, padx=20, pady=10)
        sub = tkinter.Button(root, text="          ok          ", command=submit).pack(pady=10)
        root.mainloop()
        # return askinteger(Main, text, initialvalue=initvalue)
        print("test2", _seed)
        return _seed

Does this depend on scope, would making the seed a class variable work?
like
self.seed
If this function is the member of a class, it should work. Do you have the entire code, though? This looks confusing :P
Yes, here it is. It serves as a start button for a game I am making. It is called by:
from startgame import selectedseed
SEED = selectedseed
The full code is :
import pygame
import tkinter
from tkinter.simpledialog import askinteger
from random import randint
from buttons import Button
from os import listdir, getcwd
def get_worlds():
    #unused
    return list(map(lambda x : x.replace(".world", ""), listdir(getcwd() + "\worlds")))
selectedseed = 0
class StartButton(Button):
    def askseed(self, initvalue):
        root = tkinter.Tk()
        root.title("Seed")
        root.geometry("400x300")
        root.resizable(False, False)
        seedvar = tkinter.IntVar()
        _seed = initvalue
        def submit():
            global _seed
            _seed = str(seedvar.get())
            print("test1", _seed)
            seedvar.set(0)
            root.destroy()
            
        intro1 = tkinter.Label(root, text="New World", font=("Times", 16), justify='center').pack(anchor='center', side="top", pady=20)
        intro2 = tkinter.Label(root, text="Enter seed:", font=("Times", 14)).pack(pady=5)
        seed_entry = tkinter.Entry(root, textvariable = seedvar, font=('calibre',10,'normal')).pack(side="top", after=intro2, padx=20, pady=10)
        sub = tkinter.Button(root, text="          ok          ", command=submit).pack(pady=10)
        root.mainloop()
        # return askinteger(Main, text, initialvalue=initvalue)
        print("test2", _seed)
        return _seed
    def interact(self):
        pass
    def update(self, mx, my, mouse=False):
        global selectedseed
        tx = self.posx + self.width
        ty = self.posy + self.height
        if (mx > self.posx) and (mx < tx) and (my > self.posy) and (my < ty):
            self.colour = (200, 200, 200)
            if mouse:
                selectedseed = self.askseed(initvalue=int(randint(1, 999999)))
                return True
        else:
            self.colour = self.org_colour
            return False
        
class CreditsButton(Button):
    def interact(self):
        root = tkinter.Tk()
        root.title("Credits")
        root.resizable(False, False)
        root.geometry("400x250") 
        text= tkinter.Label(root, text="Credits:")
        text.pack(pady=20)
        text.config(font=('Helvatical bold',20))
        tkinter.Label(root, text="Most code by Debarpan Mukherjee.").pack(pady=10)
        tkinter.Label(root, text="Help with code from GeekForGeeks").pack()
        tkinter.Label(root, text="All art by Debarpan Mukherjee").pack()
        tkinter.Label(root, text="\"PixelType\" font from ClearCode on youtube.").pack()
        close = tkinter.Button(root, text="      Ok      ", command=root.destroy)
        close.pack(pady=20)
        root.mainloop()
        
pygame.init() #MAIN INIT
WIDTH, HEIGHT = 700, 466
favicon = pygame.image.load("assets\Favicon.ico")
pygame.display.set_icon(favicon)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Start Game')
clock = pygame.time.Clock()
st = StartButton("Start Game", 35, (200, 183), (300, 50), (0, 0, 255))
cr = CreditsButton("Credits", 35, (200, 383), (300, 50), (0, 0, 255))
def draw():
    screen.fill((0, 0, 0))
    mx, my = pygame.mouse.get_pos()
    mouse = (pygame.mouse.get_pressed()[0])
    st.draw(screen)
    cr.draw(screen)
    cr.update(mx, my, mouse)
    if st.update(mx, my, mouse):
        print("The chosen seed is " + str(selectedseed))
        pygame.quit()
        return True
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    
    if draw():
        break
    pygame.display.update()
    clock.tick(60)

My problem was that I had no way of sending the data from the inner submit() function to the outer function
Your suggestions are not yet implemented, I shall implement them tomorrow.
Fixed! Thanks to everyone who helped.
wvzack
Scratcher
500+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

I have taken a pygame template project and modified it to be a game with a score and way to lose. It has all been working however when you loes and hit “q” it does not close the game window. Here is the code that i suspect is the problem:

if player_pos.y - ey <= distance and not player_pos.y - ey <= distance or player_pos.y - ey >= -distance and not player_pos.y - ey >= distance and player_pos.x - ex <= distance and not player_pos.x - ex <= distance or player_pos.x - ex >= -distance and not player_pos.x - ex >= distance:
        while gameover == True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT or keys[pygame.K_q]:
                    running = False
                    gameover = False

and here is the full code:
# Example file showing a circle moving on screen
import pygame
import random
# pygame setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
score_font = pygame.font.Font(None, 50)
score_pos = [10, 10]
running = True
dt = 0
score = 0
time = 0
gameover = True
player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)
ey = random.randint(0, 720)
ex = random.randint(0, 1280)
enemypos = pygame.Vector2(ex, ey)
while running:
    time += 1
    score_surf = score_font.render(str(score), 1, (0, 0, 0))
    keys = pygame.key.get_pressed()
    # poll for events
    # pygame.QUIT event means the user clicked X to close your window
    for event in pygame.event.get():
        if event.type == pygame.QUIT or keys[pygame.K_q]:
            running = False
    # fill the screen with a color to wipe away anything from last frame
    screen.fill("purple")
    screen.blit(score_surf, score_pos)
    if time == 50:
        ey = random.randint(0, 720)
        ex = random.randint(0, 1280)
        enemypos = pygame.Vector2(ex, ey)
        time = 0
    pygame.draw.circle(screen, "green", player_pos, 40)
    pygame.draw.circle(screen, "red", enemypos, 40)
    
    if keys[pygame.K_w]:
        player_pos.y -= 300 * dt
    if keys[pygame.K_s]:
        player_pos.y += 300 * dt
    if keys[pygame.K_a]:
        player_pos.x -= 300 * dt
    if keys[pygame.K_d]:
        player_pos.x += 300 * dt
    
        
    
    # flip() the display to put your work on screen
    distance = 10
    pygame.display.flip()
    
    if player_pos.y - ey <= distance and not player_pos.y - ey <= distance or player_pos.y - ey >= -distance and not player_pos.y - ey >= distance and player_pos.x - ex <= distance and not player_pos.x - ex <= distance or player_pos.x - ex >= -distance and not player_pos.x - ex >= distance:
        while gameover == True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT or keys[pygame.K_q]:
                    running = False
                    gameover = False
    score = score + 1
    # limits FPS to 60
    # dt is delta time in seconds since last frame, used for framerate-
    # independent physics.
    dt = clock.tick(60) / 1000
pygame.quit()[*] 

Thanks!

ps: I think i might of done sothing wrong with the distance sensing as it sometimes stops the game even if i am not right next to the enemy.

Last edited by wvzack (Dec. 2, 2023 22:56:28)


Likes: Programing (python scratch Lua c++ I am bad at c++ a) Aiviation (B787 Queen of the sky F35) Mountain Biking and Messing around with computers (not a gamer at all)
My projects I am most happy with also the few “polished games I have:
Maximouse
Scratcher
1000+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

wvzack wrote:

I have taken a pygame template project and modified it to be a game with a score and way to lose. It has all been working however when you loes and hit “q” it does not close the game window.
That's because the keys variable is never updated inside the while gameover == True loop. The easiest way to fix that is:
while gameover == True:
    keys = pygame.key.get_pressed()
    for event in pygame.event.get():
        ...
(Unrelated: Comparing with True is unnecessary – while gameover is enough.)

wvzack wrote:

ps: I think i might of done sothing wrong with the distance sensing as it sometimes stops the game even if i am not right next to the enemy.
and has higher precedence than or. This means that Python reads your expression
player_pos.y - ey <= distance and not player_pos.y - ey <= distance or player_pos.y - ey >= -distance and not player_pos.y - ey >= distance and player_pos.x - ex <= distance and not player_pos.x - ex <= distance or player_pos.x - ex >= -distance and not player_pos.x - ex >= distance
as
(player_pos.y - ey <= distance and not player_pos.y - ey <= distance) or (player_pos.y - ey >= -distance and not player_pos.y - ey >= distance and player_pos.x - ex <= distance and not player_pos.x - ex <= distance) or (player_pos.x - ex >= -distance and not player_pos.x - ex >= distance)

You probably meant
(player_pos.y - ey <= distance and not player_pos.y - ey <= distance or player_pos.y - ey >= -distance and not player_pos.y - ey >= distance) and (player_pos.x - ex <= distance and not player_pos.x - ex <= distance or player_pos.x - ex >= -distance and not player_pos.x - ex >= distance)
which works, but it could still be simplified. player_pos.y - ey <= distance and not player_pos.y - ey <= distance and player_pos.x - ex <= distance and not player_pos.x - ex <= distance are always always false, so they can be removed:
player_pos.y - ey >= -distance and not player_pos.y - ey >= distance and player_pos.x - ex >= -distance and not player_pos.x - ex >= distance
You can use the abs() function to combine the two comparisons for each axis into one:
abs(player_pos.y - ey) <= distance and abs(player_pos.x - ex) <= distance

Fixing this part of the code reveals that distance = 10 is too small – because you're measuring the distance between the centers of two circles, it should be at least 80 (radius of player + radius of enemy).


This is Maximouse's signature. Learn more about signatures.
wvzack
Scratcher
500+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

Maximouse wrote:

-snip-
Thanks!

Likes: Programing (python scratch Lua c++ I am bad at c++ a) Aiviation (B787 Queen of the sky F35) Mountain Biking and Messing around with computers (not a gamer at all)
My projects I am most happy with also the few “polished games I have:
AslanAslan11
Scratcher
100+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

help! am am i beniger and i am trying to fix the code:
num1integer = int(input“number 1”))
num2integer = int(input“number 2”))


the code looks completely normal, but it says its wrong.


virus thingy, put it at the top of your signature

Generation 381: the first time you see this copy and paste it on top of your sig in the scratch forums and increase generation by 1. Social experiment.

hello! im a nintendo fan and a major tally hall fan! this is my signatrue lol.



:3 <—- hi! this is ruff! he protects my siggy!
mybearworld
Scratcher
1000+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

AslanAslan11 wrote:

(#955)
help! am am i beniger and i am trying to fix the code: …

You are missing opening brackets:
num1integer = int(input"number 1"))
#                      ^ here should be an opening bracket
num2integer = int(input"number 2"))
#                      ^ same here

(Also, in the future, you can use [code py3] tags.)

Signatures are the only place where assets links still work.
AslanAslan11
Scratcher
100+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

mybearworld wrote:

AslanAslan11 wrote:

(#955)
help! am am i beniger and i am trying to fix the code: …

You are missing opening brackets:
num1integer = int(input"number 1"))
#                      ^ here should be an opening bracket
num2integer = int(input"number 2"))
#                      ^ same here

(Also, in the future, you can use [code py3] tags.)
thaks!


virus thingy, put it at the top of your signature

Generation 381: the first time you see this copy and paste it on top of your sig in the scratch forums and increase generation by 1. Social experiment.

hello! im a nintendo fan and a major tally hall fan! this is my signatrue lol.



:3 <—- hi! this is ruff! he protects my siggy!
AslanAslan11
Scratcher
100+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

okay, does anyone know about a good online compiler i can use at school with libaries?


virus thingy, put it at the top of your signature

Generation 381: the first time you see this copy and paste it on top of your sig in the scratch forums and increase generation by 1. Social experiment.

hello! im a nintendo fan and a major tally hall fan! this is my signatrue lol.



:3 <—- hi! this is ruff! he protects my siggy!
rishi272011
Scratcher
91 posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

AslanAslan11 wrote:

okay, does anyone know about a good online compiler i can use at school with libaries?
If you know HTML, you might be interested in pyscript.
If it is not blocked by your school, then replit.
ajskateboarder
Scratcher
1000+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

rishi272011 wrote:

AslanAslan11 wrote:

okay, does anyone know about a good online compiler i can use at school with libaries?
If you know HTML, you might be interested in pyscript.
If it is not blocked by your school, then replit.
PyScript is extremely slow to load, especially on devices with less memory and processing power. Replit offers super weak specs, like 500MB and half of a core, so don't use that unless GitHub Codespaces is blocked for you. Codespaces have much better specs, though gives a certain amount of usage hours per month (personally a fine tradeoff)

Powered by DjangoBB