Sunday, 15 April 2012

python - Character only maintains movement while mouse is moving on screen? -


While my hands only keep movement for the phantom, while the cursor is moving inside the screen. I have tried to reconstruct some screens. Blits and display.update () and display.flip () I can not know why the character stops in one pixel instead of a change, as I intend

  background_image = 'Terrain_Grass_First.png' import pygame, sys import * pygame.init (from pygame.locals) pygame.display.set_caption ( 'Swan') screen_width = 600 screen_height = 400 screen = pygame.display.set_mode ((screen_width, screen_height), 0,32) pygame.mouse.set_visible (false) Sprite = pygame.image.load ('Hans_front_still.png'). convert_alpha () x, y = (0,0) Movex, movey = (0,0), while it is true: pygame.event.get () in the event that event.type == QUIT: pygame.quit () sys.exit () if event.type == keydown: If event.key == K_ESCAPE: pygame.quit () sys.exit () if event.key == K_w: y = -1 elif event.key = = K_a: x = -1 elif event.key == K_s: Y = +1 elif event.key == K_d: x = +1 elif event.type == KEYUP: if event.key == K_w: y = 0 Elif event.key == K_a: x = 0 alif event.key == K_s: y = 0 elif event.key == K_d: x = 0 Move x + = x + shifty + = y screen. Fill ((0,0,0)) screen. Blit (Phantom (conduction, conduction)) Pygame.display.flip ()    

Your LOOP Block On

You do not schedule any of your own programs.

Therefore, unless you have an event (mouse move, redra, etc.) in the OS, you do not do anything, give it to you.


And, if you have decided, you are calling movex + = x once per code. Therefore, when the OS is throwing a lot of events on you, then your phantom will tilt the madness on the screen, but when the events are coming slowly, it will crawl together. It's almost never that you want.


An easy fix for both problems is just to set your own events, for example, you can make sure that you get each event in 250 milliseconds, and you only can move the phantom on those events, for example:

  timer_event = pygame.USEREVENT + 1 pygame.time.set_timer (timer_event, 250), while it is true: pygame.event For event () in .get: # ... elif event .type == timer_event :. Movex + x = movey + y =   

Another option is to design your game with a fixed framerate

A trivial example looks like this is:

  FRAME_TIME = 50 # 50ms = 20fps next_frame_time = pygame.time.get_ticks () + FRAMES while true: while right: event = pygame.event.poll () If the event type == Pygame.NOEVENT: break elif # ... pygame.display.flip () now = pygame.time.get_ticks () if now & lt; Next_frame_time: pygame.time.wait (next_frame_time - now) next_frame_time + = FRAMES   

Now you can move every 5th frame.

A realistic example is to deal with missed frames, select between multiple events in the queue, wait and delay appropriate, etc. < / P>

But in most cases of event-driven cases, the version of the timer rather than the fixed-framerate version, especially if you are already going down that line.

No comments:

Post a Comment