Monday, 15 July 2013

cursor - game coding in python using pygame -



cursor - game coding in python using pygame -

how write code such image controlled mouse motion in python using pygame?

i appreciate if help writing of code because have absolutely no thought how it.

i have tried this:

#!/usr/bin/env python # -*- coding: utf-8 -*- import pygame import random pygame.init() size=[800,600] screen=pygame.display.set_mode(size) pygame.display.set_caption("sub dice") background_position=[0,0] background_image=pygame.image.load('c:\users\shivangi\desktop\shivangi project\program\star.png').convert() card=pygame.image.load('c:\users\shivangi\desktop\shivangi project\program\lappy.png').convert_alpha() card=pygame.transform.smoothscale(card,(130,182)) closedeckshirt=pygame.image.load('c:\users\shivangi\desktop\shivangi project\program\star.png').convert_alpha() setfps=30 zx=0 zy=0 done=false clock=pygame.time.clock() while done==false: clock.tick(setfps) event in pygame.event.get(): if event.type == pygame.quit: done=true if event.type == pygame.mousebuttondown: print('a') screen.blit(background_image, background_position) screen.blit(card,[zx,zy]) zx=zx+2 zy=zy+2 pygame.display.flip() pygame.quit ()

however, motion restricted 1 direction irrespective of move mouse. want image move forwards , have sideways motion controlled motion of mouse.

also, aim create game wings on water available @ orisinal.

you're close making work. i've made few little edits.

so, programme has 2 problems. first programme moving image on every pygame.event regardless of event -- you'll see hitting key, clicking mouse etc. sec problem you're moving image in fixed direction.

the thing changed while loop:

while done==false: clock.tick(setfps) event in pygame.event.get(): if event.type == pygame.quit: done=true if event.type == pygame.mousebuttondown: print('a') if event.type == pygame.mousemotion: # want move image when mouse moved. mouse_position = pygame.mouse.get_pos() # mouse at? screen.blit(background_image, background_position) screen.blit(card,[zx,zy]) zx=mouse_position[0] # mouse_position in form [x,y], want x part zy=mouse_position[1] pygame.display.flip()

as can see, pygame has mouse.get_pos() function gets location of mouse on screen. (x,y) coordinate.

python cursor pygame mouse movement

No comments:

Post a Comment