Python - Problema con la ejecucion en pygame

 
Vista:

Problema con la ejecucion en pygame

Publicado por Richard Torres (2 intervenciones) el 23/10/2019 05:47:32
Estoy creando un juego en pygame (snake) pero al iniciar el programa solo es ejecutado si muevo y posiciono el cursor sobre la ventana
lo mismo pasa si inicio el juego la serpiente solo se mueve mientras muevo el cursor sobre la ventana
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Jaime
Val: 526
Bronce
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

Problema con la ejecucion en pygame

Publicado por Jaime (136 intervenciones) el 23/10/2019 12:12:15
Pero sin código no se puede ayudar!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Problema con la ejecucion en pygame

Publicado por richard torres (2 intervenciones) el 23/10/2019 14:15:39
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import pygame,time,random
 
from pygame.locals import *
 
 
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
#Colores
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
blanco=255,255,255
negro=0,0,0
rojo= 192,57,43
azul=41,128,185
verde= 26,188,156
amarillo=255,255,0
naranja= 243,156,18
gris= 93,109,126
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
#pantalla
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
tamaño_pantalla=ancho_pantalla,alto_pantalla=1000,600
fps=pygame.time.Clock()
pantalla=pygame.display.set_mode((tamaño_pantalla))
pygame.display.set_caption('Snake')
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
#botones
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
boton1=[400,250]
tamboton=[250,50]
boton2=[400,350]
boton3=[400,450]
colorboton=[gris,rojo,negro,blanco]
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
#serpiente
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
posicions=[500,300]
tamaño=15
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
#manzana
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
posicions=[500,300]
tamaño=15
velocidads=[0,0]
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
pygame.init()
 
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
#Fuente
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
letra_pequeña= pygame.font.Font("Greywall.ttf",20)
letra_media= pygame.font.Font("Greywall.ttf",40)
letra_grande= pygame.font.Font("Greywall.ttf",120)
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
 
 
 
def borde(superficie,color,pos,tam,grosor):
    cursor=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    if pos[0]+tam[0]>cursor[0] > tam[0] and pos[1]+tam[1]>cursor[1]>tam[1] and pos[1]+tam[1]<cursor[1]+tam[1]:
        marco=pygame.draw.lines(superficie,color[3],True,((pos[0],pos[1]),(pos[0]+tam[0],pos[1]),(pos[0]+tam[0],pos[1]+tam[1]),(pos[0],pos[1]+tam[1])),grosor)
    else:
        marco=pygame.draw.lines(superficie,color[2],True,((pos[0],pos[1]),(pos[0]+tam[0],pos[1]),(pos[0]+tam[0],pos[1]+tam[1]),(pos[0],pos[1]+tam[1])),grosor)
    return marco
 
def botones(texto,superficie,color,pos,tam,identidad= None):
    cursor=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    if pos[0]+tam[0]>cursor[0] > tam[0] and pos[1]+tam[1]>cursor[1]>tam[1] and pos[1]+tam[1]<cursor[1]+tam[1]:
        if click[0] == 1:
            if identidad == "jugar":
                gameloop()
            elif identidad == "config":
                opciones()
            elif identidad == "salir":
                quit()
        boton=pygame.draw.rect(superficie,color[0],(pos[0],pos[1],tam[0],tam[1]))
        textboton(texto,color[3],pos,tam)
    else:
        boton=pygame.draw.rect(superficie,color[1],(pos[0],pos[1],tam[0],tam[1]))
        textboton(texto,color[2],pos,tam)
    return boton
 
def textboton(msg,color,pos,tam,tamaño="pequeño"):
    texto_sup,textoRect=texto(msg,color,tamaño)
    textoRect.center = pos[0]+tam[0]//2,pos[1]+tam[1]//2
    pantalla.blit(texto_sup,textoRect)
 
 
 
def texto(texto,color,tamaño):
    if tamaño == "pequeño":
        texto_sup=letra_pequeña.render(texto,True,color)
    if tamaño == "mediano":
        texto_sup=letra_media.render(texto,True,color)
    if tamaño == "grande":
        texto_sup=letra_grande.render(texto,True,color)
    return texto_sup,texto_sup.get_rect()
 
 
def mensaje(msg,color,desplazamientoY=0,tamaño="pequeño"):
    texto_sup,textoRect=texto(msg,color,tamaño)
    textoRect.center = (ancho_pantalla//2),(alto_pantalla//2)+desplazamientoY
    pantalla.blit(texto_sup,textoRect)
 
 
 
 
def inicio():
    intjuego=True
 
    while intjuego:
        pantalla.fill(azul)
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                introjuego=False
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_ESCAPE:
                    quit()
            mensaje("¡Snake!",naranja,-200,tamaño="grande")
 
            botones("Iniciar juego",pantalla,colorboton,boton1,tamboton,identidad="jugar")
            borde(pantalla,colorboton,boton1,tamboton,2)
 
            botones("Opciones",pantalla,colorboton,boton2,tamboton,identidad="config")
            borde(pantalla,colorboton,boton2,tamboton,2)
 
            botones("Salir",pantalla,colorboton,boton3,tamboton,identidad="salir")
            borde(pantalla,colorboton,boton3,tamboton,2)
 
 
            pygame.display.update()
            fps.tick(10)
 
 
def opciones():
    retroceso=True
 
    while retroceso:
        pantalla.fill(blanco)
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
               retroceso=False
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_ESCAPE:
                    inicio()
                    retroceso=False
            mensaje("Opciones",verde,-200,tamaño="mediano")
            pygame.display.update()
            fps.tick(10)
 
 
 
def gameloop():
    salir=False
    global posicions
    global velocidads
    while not salir:
        pantalla.fill(negro)
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                salir=True
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_ESCAPE:
                    inicio()
                    salir=True
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_LEFT:
                    velocidads=(-4,0)
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_RIGHT:
                    velocidads=(4,0)
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_UP:
                    velocidads=(0,-4)
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_DOWN:
                    velocidads=(0,4)
            posicions[0] += velocidads[0]
            posicions[1] += velocidads[1]
            pygame.draw.circle(pantalla,verde,(posicions),tamaño,0)
            #pygame.draw.circle(pantalla,rojo,(random.randint(0,1000),random.randint(0,600)),tamaño,0)
            pygame.display.update()
            fps.tick(60)
 
 
 
inicio()
quit()
gameloop()
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 2.808
Oro
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

Problema con la ejecucion en pygame

Publicado por tincopasan (1082 intervenciones) el 24/10/2019 02:19:48
hola:
por ahora solo dos cosas:
1)
1
2
3
4
5
6
7
8
9
10
11
12
13
def inicio():
    intjuego=True
    while intjuego:
        pantalla.fill(azul)
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                introjuego=False                                      #supongo que está mal el nombre de la variable o es otra cosa?
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_ESCAPE:
                    quit()
            mensaje("¡Snake!",naranja,-200,tamaño="grande")          #desde acá esta mal la indentación, deben estar al nivel del for
            botones("Iniciar juego",pantalla,colorboton,boton1,tamboton,identidad="jugar")
            borde(pantalla,colorboton,boton1,tamboton,2)

si corriges esa identación ya verásel menú, te coca revisar las otras que también están mal

2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if event.type==pygame.KEYDOWN:
    if event.key==pygame.K_ESCAPE:
        inicio()
        salir=True
if event.type==pygame.KEYDOWN:
    if event.key==pygame.K_LEFT:
        velocidads=(-4,0)
if event.type==pygame.KEYDOWN:  #estás lineas se repiten innecesariamente
    if event.key==pygame.K_RIGHT:  #podrías continuar desde acá y eliminar la ya repetida 
        velocidads=(4,0)
if event.type==pygame.KEYDOWN:
    if event.key==pygame.K_UP:
        velocidads=(0,-4)
if event.type==pygame.KEYDOWN:
    if event.key==pygame.K_DOWN:
        velocidads=(0,4)

tampoco entiendo para que tantos espacios entre líneas, hay que hacer scroll a lo chancho para leer todo el código.
prueba eso, revisa el código y plantea nuevamente los problemas.
Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
-1
Comentar