3

I am novice in programming and gamedevelopment. So, I want to create very simple 2D RTS using Python and pygame, and first problem what I collide is camera. I need to create complicated camera under background. It should be able to zoom in / out and move around the keyboard keys. All lessons, that I find, move background or do something unusual without normal explanations.

Can I create this camera in pygame at all? And how? How move camera under background? (I planned to add objects to the background later) How realize zoom?

Now I have a simple code that load background image.

import math, random, sys
from PyQt5.QtWidgets import QDesktopWidget,QApplication
import pygame
app = QApplication(sys.argv)
q = QDesktopWidget().availableGeometry()

pygame.init()

clock = pygame.time.Clock()
FPS = 60
pygame.display.set_caption("New Game")
win = pygame.display.set_mode((q.width(), q.height())) #Creating window like size of screen


bg = pygame.image.load('C:\Py\Game\Image\Kordon3.jpg').convert() #Loading background image
run = True

H = 0
W = 0

while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    #This keys move background

    if keys[pygame.K_LEFT]:
        W += 5
    elif keys[pygame.K_RIGHT]:
        W -= 5
    elif keys[pygame.K_DOWN]:
        H -= 5
    elif keys[pygame.K_UP]:
        H += 5
    win.blit(bg, (W, H)) #Add/update coordinats of background
    pygame.display.update()
    clock.tick(FPS)
pygame.quit()

1 Answers1

3

This is pretty straight forward, I see you already understand the movement part. The zooming would be easy if pygame used a normal co-ordinate system because then we would just multiply the x and y by the zoom factor but it doesn't so we convert it to a normal co-ord, zoom, then convert back. I made this for rectangles, but too make it for images you would just say in graphics

screen.blit(bg,(topLeftX,topLeftY))

And also give the background a starterX and starterY, which you could just have as default 0,0

Controls: WASD for moving up and down arrow for zooming

import pygame 
x = 0
y = 0
running = True
width = 800
height = 600
zoom = 1
allRects = []

screen = pygame.display.set_mode((width,height))

class Shape:
    def __init__(self,x,y,w,h):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
def graphics(deltaX,deltaY):
    screen.fill((255,255,255))
    for rectangle in allRects:
        toDraw = True
        normalX =zoom*(-(width/2)+(rectangle.x - deltaX))
        normalY =zoom*((height/2) -(rectangle.y-deltaY))
        topLeftX = width/2+normalX
        topLeftY = height/2 - normalY
        if topLeftX + rectangle.w < 0 or topLeftX>width:
            toDraw= False
        elif topLeftY> height or topLeftY+rectangle.h<0:
            toDraw= False
        else:
            pygame.draw.rect(screen,(0,0,0),pygame.Rect(topLeftX,topLeftY,zoom*rectangle.w,zoom*rectangle.h))

allRects.append(Shape(100,30,142,14))
allRects.append(Shape(100,120,20,14))
allRects.append(Shape(0,30,14,174))
allRects.append(Shape(40,340,114,14))

while running:
    graphics(x,y)
    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    key = pygame.key.get_pressed()
    if key[pygame.K_UP]:
        zoom = zoom*1.01
    if key[pygame.K_DOWN]:
        zoom = zoom*0.99
    if zoom<0:
        zoom = 0

    if key[pygame.K_w]:
        y-=1/zoom * 2
    if key[pygame.K_s]:
        y+=1/zoom * 2
    if key[pygame.K_a]:
        x-=1/zoom * 2
    if key[pygame.K_d]:
        x+=1/zoom * 2       

    pygame.display.update()