Reputation: 96
Here's the code:
import pygame
import random
import sys
from pygame import display
pygame.init() #pokrecemo modul pygame
screen = pygame.display.set_mode((round(800),(600))) #varijabla koju smo nazvali screen #definisemo na rezoluciju 800x600
#pozadina
background = pygame.image.load('background.png') #<a href='https://pngtree.com/so/background'>background png from pngtree.com</a>
pygame.display.set_caption("Osvoji Svemir") # naziv prozora
icon = pygame.image.load('ufo.png') # definisemo ikonicu
pygame.display.set_icon(icon) #pozivamo ikonicu da se pojavi
pygame.display.update()
# Igrac
playerImg = pygame.image.load('spaceship1.png')
playerX = 370
playerY = 500
playerX_change = 0
# Neprijatelj
enemyImg = pygame.image.load('enemy.png')
enemyX = random.randint(0, 800) # ubacio sam random modul kako bi se neprijatelj pojavljivao na drugom mestu uvek
enemyY = random.randint(50, 150) # gornje kordinate su za X, a ove- donje za Y osu- da se na "random"- nasumicno pojavljuju
# defininisemo pozicije igraca naseg malog spejsatla
playerX_change = 0
playerY_change = 1
# definisemo pokrete neprijatelja, malih (u nasem slucaju- plavih)
enemyX_change = 0.4
enemyY_change = 40
def player(x,y):
screen.blit(playerImg, (x, y))
def enemy(x,y):
screen.blit(enemyImg, (x, y))
# Petlja igre (game loop)
running = True
while running:
screen.fill((0, 0, 0)) # prva i osnovna pozadina ekrana ako redom idemo to je (CRVENA,PLAVA, ZELENA) u kodu
#pozadina za igricu (interaktivna pozadina koja je velicine ekrana kojeg smo definisali (800x600) u pixelima
# screen.blit(background, (0, 0))
screen.blit(background, (0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Provera za pritisnute tastere na tastaturi
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -3
# print("Sistem je registrovao da je pritisnuta leva strelica")
if event.key == pygame.K_RIGHT:
playerX_change = 3
# print("Sistem je registrovao da je pritisnuta desna strelica")
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
# print("Sistem ceka unos sa tastature")
# <-- ovaj deo, i ispod definise nas brod i neprijatelje da ne izlaze van ekrana
playerX += playerX_change
if playerX <- 0:
playerX = 0
elif playerX >= 736:
playerX = 736
enemyX += enemyX_change
if enemyX <- 0:
enemyX_change = 0.4
enemyY += enemyY_change
elif enemyX >= 736:
enemyX_change = -0.4
enemyY += enemyY_change
player(playerX, playerY)
enemy(enemyX, enemyY)
pygame.display.update()
What am I so far doing wrong to get down of the window and up about 2cm of black strap... Comments are on Serbian so like IGRAC = PLAYER, NEPRIJATELJ = ENEMY , but the code is the same...
So far I don't want to move on if there is a bug or I don't know what. I've used 800x600 screen, with icon of 64px in 3 cases, icon for app, player icon and enemy. Also, sometimes enemy (neprijatelj) is moving very odd and slow, and I am getting ERROR MESSAGE WHEN COMPILING:
home/boris/Downloads/Python-3.8.5/python /home/boris/Desktop/OsvajacSvemira/main.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
/home/boris/Desktop/OsvajacSvemira/main.py:38: DeprecationWarning: an integer is required (got type float). Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
screen.blit(enemyImg, (x, y))
Process finished with exit code 0
I have new KINGSTONE 4GB of RAM and an i686 processor. So, I am not x64 or a cutting edge developer, I am a old-school guy at least regarding tech using, but, I am working with cutting edge technologies as long as they are x86.
Upvotes: 1
Views: 256
Reputation: 211248
The warning refers to the coordinate argument of blit()
. Floating point coordinates would mean that the origin of the Surface
is somewhere between the pixels in the window. This does not make much sense. The coordinates are automatically and implicitly truncated, and this is indicated by the warning.
round
the coordinates to integral values:
screen.blit(enemyImg, (round(x), round(y)))
Upvotes: 1