Python pygame writing text in sprite [closed] - python-3.x

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am making a game where you shall shoot down diffrent boxes with diffrent nummber and text on and was wondring if you can write text as a sprite

class Text(pygame.sprite.Sprite):
def __init__(self, text, size, color, width, height):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.SysFont("Arial", size)
self.textSurf = self.font.render(text, 1, color)
self.image = pygame.Surface((width, height))
W = self.textSurf.get_width()
H = self.textSurf.get_height()
self.image.blit(self.textSurf, [width/2 - W/2, height/2 - H/2])
I hope that helps, this will draw text centered on surface in a sprite

Related

Get A Response From A Result (CURL) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
I'm noobie/beginner at Python
I was creating this script
I tried to curl a URL and got a response(A)
now I want to get the response(B) from that response A
result = os.popen("curl https://raw.githubusercontent.com/SirdLay/test/main/test.txt").read()
index = os.popen("curl [what to be used here?]")
response1 = os.popen("curl https://raw.githubusercontent.com/SirdLay/test/main/test.txt").read()
url = response1[<key>]
# key is some key in the response1.
# You can print the response1 to see the object.
# For your particular case you dont need a key.
# so you can use
url = response1
response2 = os.popen("curl " + url).read()

AttributeError: 'str' object has no attribute 'describe' [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
class Computer:
def _inti_(self, storage, color , system):
no_of_Computer = 0
self.storage = storage
self.color = color
self.system = system
Computer.no_of_Computer +=1
def describe (self):
print(f'my storage is {self.storage} and my color is{self.color} and my system is {self.system}')
Computer_1 = ("1TB , silver , windows ")
Computer_2 = (" 4TB , black , linux")
Computer_3 = (" 9TB , white ,mac ")
Computer_1.describe()
Computer_1, Computer_2 and Computer_3 aren't Computer instances, they are just strings (surrounded by parentheses). You need to call the Compueter's constructor to create new instances of it. Also, note that each argument should be its own string, not a single string with commas in it.
Additionally, note that a constructor is defined by the method __init__ (note the double undescores), not _inti_:
class Computer:
def __init__(self, storage, color , system):
no_of_Computer = 0
self.storage = storage
self.color = color
self.system = system
Computer.no_of_Computer +=1
def describe (self):
print(f'my storage is {self.storage} and my color is{self.color} and my system is {self.system}')
Computer_1 = Computer("1TB", "silver", "windows")
Computer_2 = Computer("4TB", "black", "linux")
Computer_3 = Computer("9TB", "white", "mac")
Computer_1.describe()

In name == 'main', why is variables shared in class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Well, I just noticed that variables in if name == 'main': are shared to the classes in the same file - why is this? I don't recall having this issue in python2...
class A:
def __init__(self, b):
self.b = b
def func_a(self):
return d
if __name__ == '__main__':
classA = A(1)
d = 2
print(classA.func_a())
prints out 2.
What's the reasoning?
this definitely also happens in python2
and is very simple: declaring variables outside of functions/classes makes them global and when python searches for variables with the name d it doesn't find it in the local scope but it does find the global variable

How do I use the signal in one class to call a function in another in PyQt5? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I want to create a button in my PyQt5 app, which would change the window title name to a new value.
I tried to make a custom function in class PushButton(QPushButton) which would tell MainWindow to setWindowTitle to a new name. Then I connected the .clicked signal within PushButton class to that function, however, whenever I press the button the application crashes.
I am kind of new, so if I got any class/function relationship wrong, please correct me.
What am I missing here?
windowTitle : QString
This property holds the window title (caption)
This property only makes sense for top-level widgets, such as windows and dialogs. If no caption has been set, the title is based of the windowFilePath. If neither of these is set, then the title is an empty string.
setWindowTitle(const QString &)
import sys
from PyQt5.QtWidgets import (QPushButton, QWidget, QLineEdit, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Change the window title name')
self.resize(300, 150)
self.lineEdit = QLineEdit(self)
self.lineEdit.move(30, 65)
button = QPushButton("Button", self)
button.clicked.connect(self.onClicked)
button.move(190, 65)
def onClicked(self):
self.setWindowTitle(self.lineEdit.text()) # <---
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())

Tk does not behave as I expect. What is wrong with my code ? [duplicate]

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 5 years ago.
I want a rectangle to appear WHEN I click on the button. But the rectangle appears even before I click.
from tkinter import *
def dessin():
can.create_rectangle(50,50,70,70, fill = 'navy')
fen = Tk()
can = Canvas(fen, width= 100, height = 100, bg = 'ivory')
can.pack(side = TOP)
bou = Button(fen, text= 'envoyez le rectangle', command = dessin())
bou.pack(side = BOTTOM)
bou1 = Button(fen,text='Quitter', command = fen.quit)
bou1.pack(side=RIGHT)
fen.mainloop()
You need to pass the function itself, not the result of the function. So leave the () off.
bou = Button(fen, text= 'envoyez le rectangle', command = dessin)

Resources