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 going to have multiple questions so i figure i would put it into one thread so i do not flood the website with my stupid questions.
i will list all questions as an edited question, and hope it will be answered. the first question is as follows (on my 2nd question):
this is my first "real" graphics based game i will create, but i know i need assistance.
def update(self):
if games.keyboard.is_pressed(games.K_w):
self.y -= 1
if games.keyboard.is_pressed(games.K_a):
self.x -= 1
if games.keyboard.is_pressed(games.K_s):
self.y += 1
if games.keyboard.is_pressed(games.K_d):
self.x += 1
if games.keyboard.is_pressed(games.K_i):
Inventory()
the above is in my update function of my player class, checking to see if i is pressed.
def Inventory():
global LEVEL, TOTAL_XP, MAX_HEALTH, MAX_MAGICKA, viewing_inv
viewing_inv = True
inventory_items = []
while viewing_inv == True:
print "yo"
score = games.Text(value = "Points", size = 25, color = color.green, top = 5,
left = games.screen.width/2 + 14)
games.screen.add(score)
if games.keyboard.is_pressed(games.K_i):
games.screen.remove(score)
viewing_inv = False
the above is a temp inventory function i am using to make sure things are working, which they are not.
i added the print statement so i can view what is going on behind the scenes. i see the word "yo" print roughly 2-4 times each time i hit i. how can i successfully get it so that if i press i i go into the inventory function w/o having the computer loop through 2+ times before i can remove my finger? this question is already alot even though i want to be able to press i again to exit. any advice would be appreciated!
If your room class is working correctly you shouldn't need a global, think about using instance attributes which are set in the constructor.
Also I don't think a separate class per room type is needed. It feels unnecessarily complex, as essentially each room behaves the same - it may look different - but you can handle to look of the room via instance attributes. What you need I think is to create separate instances of the single room class, and when you create the instance, pass in the walls, enemies, floors etc.
Related
This question already has an answer here:
Updating text in pygame
(1 answer)
Closed 12 months ago.
I am trying to create a small game. I made a score tracker with font.Font .
points = 0
score_text = pygame.font.Font('Pixeltype.ttf',100)
score_surf = score_text.render(f'Current points: {points}', None, 'Green')
This is before the while True loop that starts the game. This is pure variable declaration.
Later below, as i defeat 'enemies', I add +100 points to points.
Although my score does not update with screen.blit(score_surf,(850,40)), if i call print(points), my points have indeed increased.
I tried creating a separate text element for the points, still had this problem.
I am new to pygame module and i figured out I should practice the stuff i learned before moving on.
Thanks a lot in advance!
The problem is in this line
score_surf = score_text.render(f'Current points: {points}', None, 'Green')
Expression f'Current points: {points}' gets evaluated only once to variable and passed to the render. It isn't function returning string but only formatted string.
You have to render score_surf again after it is changed.
How to fix it
See this answer.
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
I am making space invaders in pygame, I am relatively new to python and this is my first project in pygame. I am trying to make my aliens move down when reaching the edge of the screen on either side. However, it is not quite working as expected. I am adding a link to a GitHub page so that anyone willing to help can view my code.
Basically what's happening is I have set the aliens to move down 1 pixel when touching the sides because when there are a lot of aliens, this moves them down quite a bit. Obviously, as the aliens start getting killed off, they move down less. However, that is not the strange part. The strange part is the fact that sometimes they will move down 1 px on the one side, but many on the other side. I'm not quite sure what I am doing wrong.
https://github.com/Kris-Stoltz/space_invaders
Add break statements so you don't call update multiple times (if an even number of aliens hit the wall, you end up traveling the same direction!)
You have to increase the y advance in update too:
for alien in aliens:
if alien.rect.right >= WIDTH:
aliens.update()
break
elif alien.rect.left <= 0:
aliens.update()
break
and:
def update(self):
self.direction *= -1
self.rect.y += 10
The code looks pretty cool though!
I changed that code to
for alien in aliens:
if alien.rect.right >= WIDTH-1:
aliens.update()
elif alien.rect.left <= 1:
aliens.update()
But I see if enemies too less they don't move down
def update(self):
print("x",self.rect.y)
self.rect.y += 1
print("y",self.rect.y)
self.direction *= -1
That peace of code looks like incorrect
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
I am trying to get my widget to light up when the mouse is hovering over it. Here is what I have tried:
self.buttons = []
self.buttonTips = ["Select and Add a clip to the project", "Order the clips in your project", "Trim the selected clip",
"Cut out audio breaks in the selected clip", "Preview clip (in new window)", "Render/Export your video"]
self.commands = [self.getClipPath, self.reorderClips, self.trimClip, self.cutOutAudio, self._preview, self.finishVideo]
self.images = [_addImage, _switchImage, _trimImage, _autoTrimImage, _previewImage, _exportImage]
for index, tip in enumerate(self.buttonTips):
self.buttons.append(Button(self.root, image=self.images[index], command=self.commands[index], bg=_bgcolor, relief=FLAT))
self.buttons[index].photo = self.images[index]
self.buttons[index].place(x=index * 30, y=490)
self.buttons[index].bind("<Enter>", func=partial(changeButtonBG, 1, self.buttons[index]))
self.buttons[index].bind("<Leave>", func=partial(changeButtonBG, 0, self.buttons[index]))
addToolTip(self.buttons[index], tip)
When I change the event type to <Motion> the function runs perfectly, but when I use or it doesn't work? Anyone know why?
Ok I have figured out my problem. In this line here:
addToolTip(self.buttons[index], tip)
the addToolTip() function also adds a bind to the widget. So I edited the binds so they had:
button.bind(func=function, add="+")
which adding the add="+" made it work
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 4 years ago.
Improve this question
So I'm using tkinter to generate a maze but the thing is that I used blocks for the walls, I'm still a beginner in python so I don't know how I'm supposed to do this, here is the problem :
To have a block I used block = PhotoImage(file ='images/block.ppm')
block0 = Label(root, image=block)
block1 = Label(root, image=block)
...
I used a script to write me like 425 objects and place them at different places with block0.place(x=20, y=20), I knew it was dumb but I had no idea what to do else, it printed me the maze but when I wanted to close with the command root.destroy
I couldn't make it. I guess it's because I did this dumb thing.
So how am I supposed to make this ?
I tried to learn more about classes but it didn't worked too.
Here is my code https://mega.nz/#F!Z7xB2IJK!NSSLM6rRFJDE5kpMPs6W_Q
Thanks in advance
It took me quite some time to figure out what you want to do. If you haven't found a solution to your problem yet, here is some code to try out.
The first thing you should do is declare a global variable to store all maze blocks.
blocks = [] # create a list for the maze blocks
Then we will need to functions for building and destroying the maze. You already have a similar structure in your code example (destroythemhehe and mazebuilder) but we can greatly improve readability and performance here.
def gamephase():
global menu, quit, block
menu = Button(root, text='Menu', relief=RIDGE, bg='#C90', command=menuevent)
menu.place(x=50, y=540)
quit = Button(root, text='Quitter', relief=RIDGE, bg='#C90', command=destroy_maze)
quit.place(x=670, y=540)
generate = Button(root, text='Generer', relief=RIDGE, bg='#C90', command=build_maze)
generate.place(x=360, y=540)
The function for building the maze is not really complicated:
def build_maze():
global blocks
for x in range(17):
for y in range(25):
if zone[x][y]:
b = Label(root, image=block)
b.place(x=20 + y * 30, y=20 + x * 30) # a formula which calculates the position of the block
blocks.append(b) # add the block to the list
It does basically the same thing as your function mazebuilder but it saves all the Labels to the blocks list which is much cleaner than your blocker function. Now we need a function to destroy the blocks:
def destroy_maze():
global blocks
for x in blocks: # destroy each block
x.destroy()
blocks = []
And we are done! This should work as expected but I haven't tested it thoroughly.
You can make your code even prettier if you use classes instead of global variables. This can also help you prevent some nasty bugs.
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 9 years ago.
Improve this question
I want to teach my kid, how to quickly read by syllables. For that I intend to write a program, that sequentially shows syllables on a screen waits for a microphone input to record, how the syllables ware pronounced. Later I would check if the recorded voice corresponds to the syllables.
I know how to detect and record a spoken word in Python. My problem is different and I believe very simple: how make a non-modal big window with custom text inside?
I know the question must sound very stupid for anyone having even a moderate experience in Python, but this will be my first try at UI for a looong time (I did some on VB6 a while ago) and I have exactly no experience how to do it, and I want to do it the easy and dirty way. I don't need a fancy UI with menus, widgets and stuff. Just a big blank window with a custom text and no controls and no event loop.
I am looking for something like that:
whdl=ShowWindow(text,fontsize)
#do my stuff
Close(whdl)
Here's a script that shows one way to do this, using pygame. The script shows several "words" in sequence, but you can hack away to make it do whatever you like. (If you do, I suspect that eventually you'll want to use the usual GUI event loop, so you can handle key presses, mouse clicks, etc.)
import os
import time
import pygame
# Center the Frame in the screen.
os.environ['SDL_VIDEO_CENTERED'] = '1'
BG_COLOR = (224, 255, 255)
TEXT_COLOR = (0, 0, 255)
SIZE = (800, 360)
def show_window(text, font, screen):
rendered_text = font.render(text, True, TEXT_COLOR)
screen.fill(BG_COLOR)
screen.blit(rendered_text, ((SIZE[0] - rendered_text.get_width()) // 2,
(SIZE[1] - rendered_text.get_height()) // 2))
pygame.display.flip()
pygame.init()
screen = pygame.display.set_mode(SIZE)
font = pygame.font.SysFont("comicsansms", 200)
words = ['foo', 'bar', 'baz']
for word in words:
show_window(word, font, screen)
# Replace the following `sleep` call with your stuff.
time.sleep(3)
pygame.quit()
print "done"