python - How can you add movement to shapes in pygame? -


i using python 2.7 tried make simple game using pygame module. in program makes rectangle, , having trouble doing getting move upon keys being pressed. believe problem 'player.move' part in code, documentation poor on pygames website. appreciated.

import pygame import random import time  pygame.init()  white = (255,255,255) black = (0,0,0)  displaywidth = 800 displayheight = 800  fps = 30 clock = pygame.time.clock()  blockwidth = 50 blockheight = 50  pygame.display.set_caption('test game') screen = pygame.display.set_mode([displaywidth, displayheight]) background = pygame.surface(screen.get_size()) background.fill((white)) background = background.convert() screen.blit(background, (0,0)) global xstart, ystart xstart = 400 ystart = 400 global player player = pygame.draw.rect(screen, black, ([xstart,ystart,blockwidth,blockheight])) pygame.display.update() def mainloop():     global x, y     x = xstart     y = ystart     mainloop = true     pygame.display.update()     while mainloop == true:         event in pygame.event.get():             if event.type == pygame.quit:                  mainloop = false             elif event.type == pygame.keydown:                 if event.key == pygame.k_escape:                     mainloop = false                 if event.key == pygame.k_up:                     player.move(x, y + 10)                     pygame.display.update()                 if event.key == pygame.k_down:                     player.move(x, y - 10)                     pygame.display.update()                 if event.key == pygame.k_left:                     player.move(x - 10, y)                     pygame.display.update()                 if event.key == pygame.k_right:                     player.move(x + 10, y)                     pygame.display.update()             clock.tick(fps)             pygame.display.flip()  mainloop() pygame.quit() 

find tutorial (ie, program arcade games python , pygame) because have many things change.

pygame low-level library , have on own. in while loop have clear screen or draw background , draw player - again , again.

here code after modifications.

you have learn pygame.rect, pygame.surface, (pygame.sprite), events, etc.

import pygame import random import time  # --- constants --- (upper_case names)  white = (255, 255, 255) black = (0  ,   0,   0)  display_width = 800 display_height = 800  fps = 30  block_width = 50 block_height = 50  # --- functions --- (lower_case names)  def run():      mainloop = true      speed_x = 0     speed_y = 0      while mainloop:          # --- events ---          event in pygame.event.get():             if event.type == pygame.quit:                  mainloop = false             elif event.type == pygame.keydown:                 if event.key == pygame.k_escape:                     mainloop = false                 # - start moving -                 elif event.key == pygame.k_up:                     speed_y = -10                 elif event.key == pygame.k_down:                     speed_y = 10                 elif event.key == pygame.k_left:                     speed_x = -10                 elif event.key == pygame.k_right:                     speed_x = 10             #elif event.type == pygame.keyup:             #    # - stop moving -             #    if event.key == pygame.k_up:             #        speed_y = 0             #    elif event.key == pygame.k_down:             #        speed_y = 0             #    elif event.key == pygame.k_left:             #        speed_x = 0             #    elif event.key == pygame.k_right:             #        speed_x = 0                      # --- updates ---          player_rect.move_ip(speed_x, speed_y)          # --- draws ---          screen.blit(background, background_rect)         screen.blit(player, player_rect)                             pygame.display.flip()          clock.tick(fps)  # --- main ---  pygame.init() pygame.display.set_caption('test game')  screen = pygame.display.set_mode( (display_width, display_height) ) screen_rect = screen.get_rect()  background_rect = screen_rect background = pygame.surface(background_rect.size) background.fill(white) background = background.convert()  player_rect = pygame.rect(400, 400, block_width, block_height) player = pygame.surface(player_rect.size) player.fill(black)  clock = pygame.time.clock()  run()  pygame.quit() 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -