Zdravim,robim momentalne 2D hada podla tutorialu na YT,no narazil som na vec ktorej tak uplne nerozumiem kedze v tychto 2D hrach som pomerne zaciatocnik ...Cely kod :
import pygame
import random
import time
pygame.init() #init pygame-u
display_width=800 #sirka displeja
display_height=600 #vyska displeja
red=(255,0,0) #inicializacia farieb
black=(0,0,0) #inicializacia farieb
white=(255,255,255) #inicializacia farieb
green=(0,255,0)
FPS=30 #fps
stranaRect=10 #velkost strany stvorca
block_size=10 #o kolko sa pohne
pOkraj=display_width-stranaRect #ohranicenie
lOkraj=0 #ohranicenie
dOkraj=display_height-stranaRect #ohranicenie
hOkraj=0 #ohranicenie
font=pygame.font.SysFont(None,25)
clock=pygame.time.Clock() #kvoli nastavenu FPS
gameDisplay=pygame.display.set_mode((display_width,display_height)) #nastavenie velkosti okna
pygame.display.set_caption("Snakis") # nadpis okna
def snake(block_size,snakelist):
for XnY in snakelist: #snakelist[0]=X ,snakelist[1]=Y
pygame.draw.rect(gameDisplay,green,(XnY[0],XnY[1],stranaRect,stranaRect))
def message_to_screen(msg,color): #vypis textu
screen_text=font.render(msg,True,color)
gameDisplay.blit(screen_text,[display_width/2,display_height/2])
def gameLoop(): #hlavna LOOP
randAppleX=round(random.randrange(0,display_width-block_size)/10.0)*10.0 #zaokruhlenie na desiatky
randAppleY=round(random.randrange(0,display_height-block_size)/10.0)*10.0 #suradnice pre jablko
lead_x=display_width/2 #startovacie suradnice hada
lead_y=display_height/2 #startovacie suradnice hada
lead_x_change=0 #premenne na posuvanie hada aj bez drzania klavesy
lead_y_change=0
snakelist=[]
snakeLength=5
gameExit=False
gameOver=False
while not gameExit:
while gameOver==True:
gameDisplay.fill(white)
message_to_screen("Game over press C to play again or Q to quit",red)
pygame.display.update()
for event in pygame.event.get():
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_q: #ak stlaci Q vypne
gameExit=True
gameOver=False
if event.key==pygame.K_c: #ak stlaci C pokracuje odznova
gameLoop()
for event in pygame.event.get(): #loop eventami
if event.type==pygame.QUIT: #ak stlaci krizik
gameExit=True
if event.type==pygame.KEYDOWN: #ak stlaci klavesu
if event.key==pygame.K_LEFT: #ak je to sipka dolava etc.
lead_x_change =- block_size
lead_y_change=0 #aby sa nehybal diagonalne
elif event.key==pygame.K_RIGHT:
lead_x_change = block_size
lead_y_change=0 #aby sa nehybal diagonalne
elif event.key==pygame.K_UP:
lead_y_change = -block_size
lead_x_change=0 #aby sa nehybal diagonalne
elif event.key==pygame.K_DOWN:
lead_y_change=block_size
lead_x_change=0 #aby sa nehybal diagonalne
if lead_x>pOkraj or lead_x<0 or lead_y>dOkraj or lead_y<0: #kontroluje ohranicenie
gameOver=True
lead_x+=lead_x_change
lead_y+=lead_y_change
gameDisplay.fill(white) #nastav pozadie na bielo
pygame.draw.rect(gameDisplay,red,(randAppleX,randAppleY,block_size,block_size)) #kreslenie jablk
snakeHead=[]
snakeHead.append(lead_x)
snakeHead.append(lead_y)
snakelist.append(snakeHead)
print ("Len snake list ",len(snakelist))
print("Snakelist[0] ",snakelist[0])
print ("Snakelist ",snakelist)
if len(snakelist) > snakeLength:
del snakelist[0]
snake(block_size,snakelist) #vykreslenie hada
pygame.display.update()#update okna
if lead_x==randAppleX and lead_y==randAppleY: #ak had zje jablko
randAppleX=round(random.randrange(0,display_width-block_size)/10.0)*10.0
randAppleY=round(random.randrange(0,display_height-block_size)/10.0)*10.0
clock.tick(FPS) #nastavenie FPS
pygame.quit()
quit()
gameLoop()
a ide o ten pohyb toho hada ze ked s nim hybem tak ako je mozne ze ked zmenim smer tak sa proste tak pekne "zlomi" ako v normalnej hre ? :-) konkretne ide o
def snake(block_size,snakelist):
for XnY in snakelist: #snakelist[0]=X ,snakelist[1]=Y
pygame.draw.rect(gameDisplay,green,(XnY[0],XnY[1],stranaRect,stranaRect))
snakelist=[]
snakeLength=5
snakeHead=[]
snakeHead.append(lead_x)
snakeHead.append(lead_y)
snakelist.append(snakeHead)
if len(snakelist) > snakeLength:
del snakelist[0]
snake(block_size,snakelist) #vykreslenie hada
Ďakujem za pomoc :-))