1. #1

    Question Pygame/Python Help Please

    I'm getting ready to go back to school for programming so I decided to brush back up on what I had learned my first semester. I'm trying to make a simple COD: Zombie like game. The player will be controlled by WASD for movement and aiming done by the mouse "Top Down" Style. I have all movement down but I cannot get the image to rotate towards the mouse. Any help would be appreciated.

    Code:
    import pygame
    import math
    import random
    
    # Window Attributes
    Win_H = 800
    Win_W = 1000
    
    def vec_zero(v):
    	'''set a 2D vector's components to zero'''
    	v[0] = 0
    	v[1] = 0
    
    def vec_length(v):
    	'''calculate and return the length of a 2D vector'''
    	return (v[0] ** 2 + v[1] ** 2) ** 0.5
    
    def vec_normalize(v):
    	'''normalize (make length equal 1) a 2D vector'''
    	tmpLength = vec_length(v)
    	if tmpLength != 0:
    		v[0] /= tmpLength
    		v[1] /= tmpLength
    
    def vec_add(v1, v2):
    	'''add two vectors, v1 and v2, together, storing result in v1'''
    	v1[0] += v2[0]
    	v1[1] += v2[1]
    
    def vec_mul(v, s):
    	'''multiply a 2D vector, v, by a scalar (a single number), s'''
    	v[0] *= s
    	v[1] *= s
    
    def load_images(character):
    	'''load the north, east, south and west animation frames for character into
    	a list, imgList, and return it'''
    	imgList = []
    	pathStrPre = character + '/walking '
    	
    	dirChar = 'neswqpzm'
    	for c in dirChar:
    		dirFrameList = []
    		for i in range(0, 9):
    			pathStr = pathStrPre + c + '000' + str(i) + '.bmp'
    			
    			img = pygame.image.load(pathStr).convert()
    			img.set_colorkey((88, 92, 56))
    			
    			dirFrameList.append(img)
    		
    		imgList.append(dirFrameList)
    	
    	return imgList
    
    # soldier's attributes
    soldier_pos = [Win_W / 2, Win_H / 2]
    soldier_vel = [0, 0]
    soldier_vel_mag = 10.0 / 100.0	# 10 pixels / .1 seconds
    #soldier_frames = load_images('soldier')
    soldier_curframe = 0
    SOLDIER_FRAME_DELAY = 1000.0 / 10.0		# delay in milliseconds at 10 FPS
    soldier_curframe_delay = SOLDIER_FRAME_DELAY
    soldier_curstate = 'STANDING'
    soldier_runstate = 0	
    
    soldier_surface = pygame.display.set_mode((30, 16))
    
    mouse_angle = 0
    	
    pygame.init()
    
    window = pygame.display.set_mode((Win_W, Win_H))
    clock = pygame.time.Clock()
    done = False
    
    keyPressed = []
    
    
    while not done:
    
    	
    	# check for events
    	evtList = pygame.event.get()
    	for evt in evtList:
    		if evt.type == pygame.QUIT:
    			done = True
    		elif evt.type == pygame.KEYDOWN:
    			if evt.key == pygame.K_ESCAPE:
    				done = True
    			else:
    				keyPressed.append(evt.key)
    		elif evt.type == pygame.KEYUP:
    			keyPressed.remove(evt.key)
    			
    	if pygame.K_w in keyPressed and pygame.K_a in keyPressed:
    		#soldier_curstate = 'RUNNING'	# set soldier's state if moving
    		#soldier_runstate = 4
    		soldier_vel[1] = -1
    		soldier_vel[0] = -1
    	elif pygame.K_w in keyPressed and pygame.K_d in keyPressed:
    		#soldier_curstate = 'RUNNING'
    		#soldier_runstate = 5
    		soldier_vel[1] = -1
    		soldier_vel[0] = 1
    	elif pygame.K_s in keyPressed and pygame.K_a in keyPressed:
    		#soldier_curstate = 'RUNNING'
    		#soldier_runstate = 6
    		soldier_vel[1] = 1
    		soldier_vel[0] = -1
    	elif pygame.K_s in keyPressed and pygame.K_d in keyPressed:
    		#soldier_curstate = 'RUNNING'
    		#soldier_runstate = 7
    		soldier_vel[1] = 1
    		soldier_vel[0] = 1
    	elif pygame.K_w in keyPressed:
    		#soldier_curstate = 'RUNNING'
    		#soldier_runstate = 0
    		soldier_vel[1] = -1
    	elif pygame.K_s in keyPressed:
    		#soldier_curstate = 'RUNNING'
    		#soldier_runstate = 2
    		soldier_vel[1] = 1
    	elif pygame.K_d in keyPressed:
    		#soldier_curstate = 'RUNNING'
    		#soldier_runstate = 1
    		soldier_vel[0] = 1	
    	elif pygame.K_a in keyPressed:
    		#soldier_curstate = 'RUNNING'
    		#soldier_runstate = 3
    		soldier_vel[0] = -1
    	
    	if pygame.K_w not in keyPressed and pygame.K_s not in keyPressed:
    		soldier_vel[1] = 0
    	if pygame.K_d not in keyPressed and pygame.K_a not in keyPressed:
    		soldier_vel[0] = 0
    	
    	
    	
    	dtime = clock.tick_busy_loop(100)
    	
    	vec_normalize(soldier_vel)
    	vec_mul(soldier_vel, soldier_vel_mag)
    	vec_mul(soldier_vel, dtime)
    	vec_add(soldier_pos, soldier_vel)
    	
    	mouseX, mouseY = pygame.mouse.get_pos()
    	mouse_angle = math.atan2(soldier_pos[0]-mouseX, soldier_pos[1]-mouseY)
    	
    	soldier_surface.blit(pygame.image.load("Soldier/MAN_MACHINEGUN_FIRE.gif").convert(),(soldier_pos))
    	
    	pygame.transform.rotate(soldier_surface, mouse_angle)
    	
    	#window.blit(pygame.image.load("background.jpg").convert(),(0,0))
    	
    	window.blit(soldier_surface,(soldier_pos))
    	
    	
    	pygame.display.flip()
    			
    pygame.quit()

  2. #2
    Herald of the Titans Cyrops's Avatar
    10+ Year Old Account
    Join Date
    Aug 2013
    Location
    Limbo
    Posts
    2,962
    I'm just curious, what is "image rotation towards the mouse"?
    PM me weird stuff :3

  3. #3
    Quote Originally Posted by Cyrops View Post
    I'm just curious, what is "image rotation towards the mouse"?
    So imagine you're playing this game. if you want to move up, press W, left press A so on and so forth. Then wherever your mouse is on the screen, that's the direction your player will be facing.

  4. #4
    Take a look at python documentation: http://pygame.org/docs/ref/transform...ansform.rotate
    Also this might be of help to you:

    Code:
    for event in pygame.event.get():
        if event.type == MOUSEMOTION:
            mousex, mousey = event.pos
            # build a vector between player position and mouse position
            moveVector = (mousex-playerx, mousey-playery)
    
            """
            compute the angle of moveVector from current vector that player is facing (faceVector).
            you should be keeping and updating this unit vector, with each mouse motion
            assume you have initial facing vector as (1,0) - facing East
            """
    
            # compute angle as in [1]
    
            # rotate the image to that angle and update faceVector
    Might need to look up finding an angle between two vectors if you haven't done it before.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •