python variable not being reset - python

I'm currently making a game with pygame. I have one issue with my game at the moment.
The scrolling of the screen is fine, but once the image has been scrolled completely after its second time (it works fine first time) the screen blit goes all weird, all the sprites in the game leaves massive trails behind where it was previously (check screnshots). This is simply because for some stragnge reason the variable "x" is not being reset back to 0 once it has exceeded the screenwidth after the second time, it resets after it equal 1384 the first time but not after the second time..
any help is greatly appreciated.
http://pastebin.com/ub6gi8Zn (pastebin code gone)
Blit working fine before background has repeated itself twice SCREENSHOT
gyazo.com/aa5626d4927b0b9299ce2ec42c9ba501 -- after the background repeating itself twice-- sorry couldn't add more than 2 links

The problem is that you aren't clearing the screen buffer.
That is, the sprites and background just get redrawn over themselves because you haven't erased the pixels of the last time you drew those sprites on the screen!
I have never used pygame before but this is a general graphics programming problem, I tried looking up and it seems the function you are looking for is screen.fill(some_color). You fill the entire screen with a given color so the output of the last drawn frame disappear.
By the way this also happens because your "background" doesn't fill the entire area of the screen.

Related

How do you rotate an image in Pygame without constantly redrawing the background?

I am making a randomized spinner in pygame, but the only method I've found is to constantly redraw the whole screen. Is there a way that doesn't use this, it becomes extremely laggy.
You can not. why do you want that? It is common to redraw the entire scene in each frame. Of course you can try to redraw just a rectangular section of the background. However, this is usually not worth the effort.
The pygame way to do this is to pass a list of rectangular areas to pygame.display.update(). If rectangles are passed to pygame.display.update() then not the entire display will be redrawn, just the specified areas::
Update portions of the screen for software displays
[...]
You can pass the function a single rectangle, or a sequence of rectangles. It is more efficient to pass many rectangles at once than to call update multiple times with single or a partial list of rectangles.
e.g.:
while run:
# [...]
screen.blit(background, (0, 0))
screen.blit(image1, bounding_rect1)
screen.blit(image2, bounding_rect2)
pygame.display.update([bounding_rect1, bounding_rect2])
That is how computer animation works, you're redrawing the screen on every frame. Every game practically works the same way. I am not sure what graphics pipeline pygame uses but in OpenGL you usually initialize all your geometry outside of the render loop and then use pointers to batch-process everything in the render-loop so you're not duplicating a lot of boilerplate code or making duplicate objects every frame.
A related question has been answered here:
https://gamedev.stackexchange.com/questions/125546/what-is-the-best-way-to-handle-the-actual-render-loop

How to accurately track mouse movements in python, tkinter?

So i am trying to make a simple Drawpad using python and tkinter, and i have pretty much got it working, except there is massive gaps in between the individual dots.
Heres an example
I was wondering if there is a more accurate mouse tracking method? Heres my code for making the dots.
def draw(event):
global DotSize
arr = []
arr.append([event.x, event.y])
for i in arr:
yes=i[0]
no=i[1]
Canvas.create_oval(yes,no,yes,no, fill="black", width=DotSize)
//Theres a load more code in here but i dont think its relevant.
Canvas.bind("<B1 Motion>",draw)
Thanks!
It looks like you are redrawing every pixel every time. First you draw one oval. Then you draw two. then you draw three, and so on. You only need to draw the last item in the array.
As for the accuracy -- what you are doing is the most accurate way to track mouse movement in tkinter. There's nothing more efficient than binding to <B1-Motion>.

how to add interface in python, with pygame

Help! I am new to programming! I am trying to make a monopoly game. My program so far creates player and card objects, these objects have some values stored.
I am trying to use pygame to implement some kind of animations and create a menu bar that pops up on the pygame screen, but I don't know how to get started. So far all I have is a pygame background screen, I want to know how I can place a small rectangle on the pygame screen given position, depending on the players object's position value. I need to know how I can have all that code I have written just for the game logic, and have it run along side with this pygame stuff.
I really need to know how to get started. If anyone is willing to spend some time with me, to get me going in the right direction, I would really appreciate it, thanks!
Reading over some pygame tutorials will certainly help.
The flow of your program will eventually look something like this:
# This is intentionally simplified pseudocode, but at the bottom-most level most
# simple games have a similar structure to this.
#Construct your game objects
players = [Player(args) for n in range(num_players)]
cards = loadCards() #or however you load your cards (files,database,etc)
board = Board()
#initialize your display context
pygame_screen.init() #or whatever is the correct syntax for Pygame
# Main game loop
while 1:
check_inputs() #get key/mouse input inputs for this frame
handle_inputs() #handle each input that has occurred during this frame
update_gamestate() #update the state on the game (who's turn, etc)
update_display() #update pygame window graphics (draw stuff, flip display)
cleanup()
As for the details on how to draw rectangles and whatnot, you should read about the various draw functions here

How would you draw multiple screens in PyGame (in only one window)?

I'm trying to port over some code from Javascript to Python, and I'm having trouble even figuring out where to start. I've looked at a good dozen tutorials for PyGame, but none of them seem to click for me. So, I was hoping to get a quick example here, or at least a point in the right direction.
I'm wanting to make a number of screens I can switch back and forth between, depending on what the user is doing at the time, and even display two side by side. At the moment, all I've got is some Javascript that draws random circles onto the screen. The PyGame logic is the only thing I'm having trouble with.
Here's my javascript for reference.
You can create a Subsurface for each subscreen that you want to create.
Then you may treat each as if it were a full screen / single surface, yet they still reference the original screen.
Pygame is a wrapper for SDL. SDL uses a surface to represent a bitmap, or anything that can be drawn on screen. With the pygame.display.set_mode((w,h),0,d) you can get the surface, or the whole canvas. You can then draw or blit the other surfaces and then call flip(), to show changes. If you wish to have a few screens, you could have a current state number, and blit the screens accordingly.
For example:
if(current_state == MAIN_SCREEN):
drawAll(screen)
else
drawEnemiesOnly(screen)
you could change the screens with the number keys:
for event in pygame.event.get():
if event.type == KEY_DOWN:
if(event.key == K_1):
current_state = 1

Beginner problem with pygame

I'm just beginning a very simple pygame code that draws a green line on a white background. However, I have to use pygame.display.flip() in order for it to show up, but it seems that I would have to use this every time I made a change and this seems too impractical for me to be doing it right. Am I missing something?
There's a good reason for this. Double buffering is a technique used to prevent "flickering". Basically, you want to draw a frame to memory instead of to the monitor and then push the frame all at once when its drawn. Otherwise, you can see different elements of the same frame go up at different times.
What you want to do is call pygame.display.flip() only once per frame draw, not after every change. Usually, this is done by having a "draw" function that is called at the end of a frame once the physics manipulations and game rules are done, and flip()ing at the end of draw.
Yes, you will have to call it each time you want to update screen. By default pygame uses double buffering Wiki link.

Categories

Resources