Hello Everyone,
This is my first post here, but I plan on spending a lot of time here trying to learn (and help others).
I've dabbled in c++ off and on for the last few years, but I found myself struggling to find a project that wasn't too challenging (or too easy) and I stepped away for a bit.
Recently, I started teaching myself a bit about Python. Apparently, it's a simpler language (that can do a lot) and I figure I'll give it a shot, maybe I can accomplish something. So I set out the other day and now I'm here...
I've attempted to render a background image (or terrain) with another image on top that can move to the location of a mouse click. I'm having trouble understanding why the image won't move precisely when I click, every time. For instance, if I click in quick succession, the image may only change locations once every second or more.
I have a feeling like it has something to do with constantly having to redraw the images, perhaps slowing it down? If that's the case, I wouldn't understand why because I thought that the game loop would iterate many times a second.... Or maybe my loop isn't working right? Can I just "move" an image instead of the constant "blit-ing"? If not, then this kind of concerns me with what I plan on doing in the future. (When I click a point on the terrain, I want to move, or walk, the image to that point).
Obviously, this is a very simple task, especially for some of you, but I can't really tell if it's a problem with the way I'm coding it or if it's really how slow python/pygame is?
Here is the code:
import sys, pygame
pygame.init()
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 800
BLACK = (0, 0, 0)
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption("TTB")
screen.fill(BLACK)
#set cursor here somewhere
terrain = pygame.image.load("terrain.jpg")
terrainRect = terrain.get_rect()
terrain = pygame.transform.scale(terrain, (SCREEN_WIDTH, SCREEN_HEIGHT))
screen.blit(terrain, terrainRect)
oPC = pygame.image.load("amishdude.png")
oPC = pygame.transform.scale(oPC, (75, 75))
oPCrect = oPC.get_rect()
screen.blit(oPC, oPCrect)
pygame.display.flip()
running = True #initialize the main game loop
while running == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False #break loop and close
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
mouseRect = pygame.mouse.get_pos()
screen.fill(BLACK)
screen.blit(terrain, terrainRect)
screen.blit(oPC, mouseRect)
pygame.display.update()
pygame.quit()
#sys.exit()