Making items appear from obstacles in game? - python

I'm making a game using Pygame where there are random obstacles on the screen (like boxes). The boxes can be removed by the player when they place a bomb next to it. I have some items that I would like to have randomly appear in place of the box if it is removed. I'm not sure how to go about this logically. Does anyone have any tips? No code is necessary (but helpful), I just want some steps logic-wise to get me started.

In order to have the box removed you need to call a ~destructor function on it, which would remove the image of the box and so on correct?
Take advantage of that and create a function that chooses which item to spawn (could be random, up to you) in the position where the box used to be.
Then call this function at the end of the destructor. That's how imagine it working.

Related

What's the easiest way to shrink and move an entire turtle drawing?

I'm messing around with turtle and I drew something but I want to be able to scale down it's size and put it in a different part of the screen.
For example let's say you have a stickman made up of five rectangles and one circle as the head, when drawing the stickman I had to give the x and y positions for each rectangle and also the length/width of each one but now it's too big and I want the entire stickman smaller.
Is there a way to easily scale down the stickman I already drew? and also be able to assign the entire object different x/y cords as a whole so I can place him somewhere else?
Let me know if anyone can help, I'm trying to just make a simple stick man game and this is really crucial to know so sorry if it's a very basic question.

How can I make a rectangle in pygame be able to check for other rectangles around it?

As part of my current pygame project (I am very new to pygame), I have created multiple rectangles that move at random around the screen. As one of the features of this game, I want to make it so if one rectangle is close enough to another one, it moves towards it. Another feature I want to add is that rectangles cannot collide with each other, but I don't want to just do the regular
if rectangle1.colliderect(rectangle2):
rectange.x -= 10 # That is, it's previous position
because it will make the animations look odd.
The main way I can see to solve these problems is to use some sort of function that could check if a rectangle.x - 30 is another rectangle (or something similar), but I am unaware of one that exists.
I have attempted to look through google, but I haven't found anything as all the posts are different problems that aren't quite the same.
Thank you for any responses!
Use inflate to create a rectangle that is larger than the original one and surrounds it. Use this rectangle to find other rectangles in range:
test_rect = rect1.inflate(dist, dist)
if test_rect.colliderect(rect2):
# [...]

Graphics.py: How to clear the window?

I have a window and I'm rendering an image onto it. Every frame the position changes, so the problem is that (obviously) it doesn't disappear from the last frame. I want to clear the window every frame. I can't find any clear methods for the window object (GraphWin) anywhere. And I can't use undraw() because then I'd have to do it for everything on the screen.
First thing to consider is if you're simply moving graphic objects between frames, then call their move() methods rather than erase and redraw them. If you must clear the screen, then I suggest:
Before dropping down to the Tkinter level, I'd consider using Zelle Graphics' own underpinnings. The reason is that Zelle Graphics keeps it's own parallel records of objects and if you delete them from the Tkinter level, you could get the two out of sync. Here's my suggestion:
def clear(win):
for item in win.items[:]:
item.undraw()
win.update()
However, undrawing items is slow, likely slower than your desired frame rate. So, you'll want to turn off auto flush:
win = GraphWin(..., autoflush=False)
and then call:
update()
whenever you have something to present to your user -- this will speed up the graphics since it won't show all the intermediate steps.
The above advice does not apply to things drawn with the win.plot() method, however. Plotting is implemented at a lower level than other Zelle graphics so you do need to drop down to Tkinter to clear the plot. See How to undraw plot with Zelle graphics?
for an example.

I am having trouble exiting my infinite loop in pygame

Code
This is my little game of pig. I only got it coded for 2 players. I have ran multiple scenarios and it seems to work out good.
I just can't seem to exit the while loop
I am having trouble at line #109.
I set it to break, but i believe it may be break from the small if statement, which I don't want.
I am wanting to break from the large while loop ( game loop ) line #100
I also want to use pygame to add some graphics of dice i have created.
I was wondering if I can keep my code as is and just modify it to work with pygame easily or do I have change the entire code to work with pygame?
I just want to do a simple interface
It will be similar to this : http://cs.gettysburg.edu/projects/pig/pi…
but I will add in dice, simulation of dice rolling 2d, sounds, a win state graphics and sounds, and input, mainly mouse toggle.
Any tips on how to do the pygame portion is much appreciated.
I have read many tutorials on pygame, but I just don't feel confident to get it to work with my own game.
I have made a ball bounce, but I really don't understand what some of pygame's code mean.
I am a little confused on display screen vs background
screen would be pygame.display.set_mode((some size))
then background will capture the exact size of the screen, but not really sure.
I have figured out how to put dice on the screen, and change face every half a second, but that's it.
nice little program. it seems to work fine for me (break exits for and while loops, not if).
when you play the game it's a little confusing because the check to see if someone has won comes only after the other player plays and then you enter 'hold'. perhaps that is what made you think it wasn't working? it might be better (i don't know the exact rules of pig) to put the check for winning right after the code that runs on 'hold'?
sorry can't help with pygame; just wanted to post to say it seems to work.
also, if you wanted, you could put the two players in an array:
players = [player1, player2]
and then the current player would be:
players[player_turn-1]
and you could use the same code for either player, duplicating the logic in the main loop. so the code would be more like:
if players[player_turn-1].winner():
...
#Make a Variable set it to "False".
#= means to assign something to something.
#== means equal to.
#Example in a if statement if something == to something do something.
test = False
#This is the loop.
while test == False:
#Put Code Here.
#Put a If Statement `enter code here`to stop loop.
if #Parameters:
test = True

Pygame doesn't draw

I need help with a program I'm making. It's a version of Conway's Game of Life.
This game is right now made out of 3 files: main.py, cellBoard.py, cell.py
main.py takes care to instance cellboard and make it update its data, give it mouse input, and tell it to draw itself (an instance of the pygame surface is given to it, which handles it to the cells which are the actual ones that draw themselves)
cellboard.py creates a list of cells based off their size and the screen's size, to fill it properly. It's a 2D list. When it creates the cells it sets their state (alive currently) and handles them an instance of its instance of the original surface instance.
cell.py contains all the things a cell can do: die, live, be toggled, be drawn.
In fact, when I need to draw the whole board I just call cellBoard's own draw() and it should take care of calling each cell's draw. And it does.
The execution gets to the point where the cell should be drawn (checked with prints) and the pixel filling function is executed (using a for loop to cover an area). But nothing is actually drawn to the screen, or at least nothing is visible.
I have no idea what is causing this. I checked the code multiple times, I've even rewritten the whole program from scratch to make it more tidy (and I had the same problem as now)
What is causing this? My idea would be that somehow the instance of surface Cell gets is not good anymore to work because something happened to it (it goes through cellboard before getting to each cell, could that be the problem?)
Here's the source code (all 3 files, they are very short and barebones so they should be easy to read) http://dl.dropbox.com/u/2951174/src.zip
Thanks in advance to anyone who feels like helping. I need to complete this project very fast so your help would be greatly appreciated.
First of a quick suggestion:
People are much more likely to help you if they don't have to download a zip file, next time just post the code parts you suspect not to work.
Anyways, problem seems to be in your main loop:
#Keyboard events
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = 0
#Mouse events
#todo
#Grid update <------- here you update the grid and the cells are being drawn
cb.draw()
#Graphical output <------------ here you're filling the WHOLE screen with white
screen.fill(THECOLORS["white"])
pygame.display.flip()
You need to move your screen.fill call above cb.draw so you don't paint over the cells.
Also in cell.py your drawing code is A) Broken and B) bad.
Instead of setting every pixel on its own, which is slow and in it's current state doesn't draw the cells correctly, you can just as well draw rectangle:
pygame.draw.rect(self.surface, (100, 10, 10), (self.pos[0], self.pos[1], self.size, self.size))

Categories